洛谷P1747

这个题被坑麻了,题目居然不给棋盘的范围,评论区居然有人说棋盘是无限大的,我想说的是如果真是这样那么第9个点答案应该是2而不是3,这个棋盘绝对是有大小的。

#include<iostream>
#include<utility>
#include<queue>
#include<unordered_map>
using namespace std;
typedef long long ll;
#define fi(i,a,b) for(int i = a; i <= b; ++i)
#define fr(i,a,b) for(int i = a; i >= b; --i)
#define x first
#define y second
#define sz(x) ((int)(x).size())
#define pb push_back
using pii = pair<int,int>;
bool vis[1000][1000];
bool vis2[1000][1000];
//#define DEBUG
struct point{
    int x,y,step;
};
int dx[12] = {1,1,-1,-1,2,2,-2,-2,2,2,-2,-2};
int dy[12] = {2,-2,2,-2,1,-1,1,-1,2,-2,2,-2};
queue<point> black;
queue<point> white;
void bfs1(){
    while(!black.empty()){
        point temp1 = black.front();
        black.pop();
        fi(i,0,11){
            int p = temp1.x + dx[i];
            int q = temp1.y + dy[i];
            int step = temp1.step + 1;
            if(p <= 0 || q <= 0 || p >= 1000 || q >= 1000 || vis[p][q]) continue;
            vis[p][q] = true;
            if(p == 1 && q == 1){
                cout << step << endl;
                return;
            }
            else{
                black.push({p,q,step});
            }
        }
    }
}
void bfs2(){
    while(!white.empty()){
        point temp1 = white.front();
        white.pop();
        fi(i,0,11){
            int p = temp1.x + dx[i];
            int q = temp1.y + dy[i];
            int step = temp1.step + 1;
            if(p <= 0 || q <= 0 || p >= 1000 || q >= 1000 || vis2[p][q]) continue;
            vis2[p][q] = true;
            if(p == 1 && q == 1){
                cout << step << endl;
                return;
            }
            else{
                white.push({p,q,step});
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int x1,y1,x2,y2;
    cin >> x1 >> y1 >> x2 >> y2;
    black.push({x1,y1,0});
    vis[x1][y1] = true;
    bfs1();
    white.push({x2,y2,0});
    vis2[x2][y2] = true;
    bfs2();
    // cout << 0 << endl;
#ifdef DEBUG
    //freopen(D:\in.txt,r,stdin);
#endif
    return 0;
}
posted @ 2022-01-28 17:00  Sun-Wind  阅读(33)  评论(0编辑  收藏  举报