zoukankan      html  css  js  c++  java
  • POJ 1915

    1. 简单的bfs,有用双向bfs的,但是为了赶进度,以后用双向bfs实现

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <algorithm>
    #include <cstring>
    #include <stack>
    #include <queue>
    
    using namespace std;
    int dir[8][2] = {{-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}};
    int flag[301][301], step[301][301];
    int n, len, i;
    struct Node
    {
        int x, y;
    } s, t;
    void bfs()
    {
        queue<Node> q;
        q.push(s);
        while (!q.empty())
        {
            Node temp = q.front(), nex;
            flag[temp.x][temp.y] = 1;
            if (temp.x == t.x && temp.y == t.y)
            {
                cout << step[temp.x][temp.y] << endl;
                return;
            }
            q.pop();
            for (i = 0; i < 8; i++)
            {
                nex.x = temp.x + dir[i][0];
                nex.y = temp.y + dir[i][1];
                if (nex.x < len && nex.x >= 0 && nex.y < len && nex.y >= 0 && !flag[nex.x][nex.y])
                {
                    q.push(nex);
                    step[nex.x][nex.y] = step[temp.x][temp.y] + 1;
                    flag[nex.x][nex.y] = 1;
                }
            }
        }
    }
    int main()
    {
        cin >> n;
        while (n--)
        {
            cin >> len >> s.x >> s.y >> t.x >> t.y;
            memset(flag, 0, sizeof(flag));
            memset(step, 0, sizeof(step));
            step[s.x][s.y] = 0;
            bfs();
        }
        return 0;
    }
    


  • 相关阅读:
    韦到头打印链表
    字符串替换
    二维数组中的查找
    字符串加解密
    简单密码破解
    vue中store模块化
    使用requests抓取网页内容
    PyQt中的一些小问题
    微信公众号之Token验证
    编译BOOST中的PYTHON模块
  • 原文地址:https://www.cnblogs.com/dollarzhaole/p/3188904.html
Copyright © 2011-2022 走看看