其他分享
首页 > 其他分享> > P1747 好奇怪的游戏

P1747 好奇怪的游戏

作者:互联网

题目链接

https://www.luogu.com.cn/problem/P1747

题目思路

还是经典的bfs模板题,只是多了四个方向

题目代码

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>

using namespace std;
typedef pair<int, int> PII;
const int N = 510;
int dist[N][N];
bool st[N][N];

int bfs(int x, int y)
{
    memset(dist, -1, sizeof dist);
    memset(st, false, sizeof st);
    queue<PII> q;
    q.push({x, y});
    dist[x][y] = 0;
    st[x][y] = true;
    while(q.size())
    {
        auto t = q.front();
        q.pop();
        int dx[12] = {-1, -2, -2, -1, 1, 2, 2, 1, 2, -2, 2, -2}, dy[12] = {2, 1, -1, -2, 2, 1, -1, -2, 2, -2, -2, 2};
        for(int i = 0; i < 12; i ++ )
        {
            int a = t.first + dx[i], b = t.second + dy[i];
            if(a >= 1 && a < N && b >= 1 && b < N && !st[a][b])
            {
                dist[a][b] = dist[t.first][t.second] + 1;
                if(a == 1 && b == 1) return dist[a][b];
                q.push({a, b});
                st[a][b] = true;
            }
        }
    }
}

int main()
{
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    cout << bfs(x1, y1) << endl << bfs(x2, y2) << endl;
    return 0;
}

标签:12,dist,游戏,int,P1747,st,&&,include,奇怪
来源: https://www.cnblogs.com/vacilie/p/16063679.html