广搜模板题
2024-02-27 20:32:39
发布于:广东
35阅读
0回复
0点赞
#include <bits/stdc++.h>
#include<queue>
using namespace std;
struct node{
int x,y;
int step;
};
int dx[] = {-2,-2,-1,-1,1,2,2,1};
int dy[] = {1,-1,2,-2,-2,-1,1,2};
int vis[1001][1001];
int n;
int stx, sty, endx, endy;
void bfs(){
queue<node>q;
node t;
t.x = stx;
t.y = sty;
t.step = 0;
q.push(t);
while(!q.empty())
{
node e=q.front();
q.pop();
if(e.x == endx and e.y == endy){
cout << e.step<<endl;
break;
}
for(int i = 0; i < 8; i++){
int nx = e.x + dx[i];
int ny = e.y + dy[i];
if(nx >= 0 and nx < n and ny >= 0 and ny < n and vis[nx][ny] == 0){
vis[nx][ny] = 1;
node d;
d.x = nx;
d.y = ny;
d.step = e.step + 1;
q.push(d);
}
}
}
}
int main()
{
int t;
cin >> t;
while(t--){
memset(vis, 0, sizeof(vis));
cin >> n;
cin >> stx >> sty;
cin >> endx >> endy;
bfs();
}
return 0;
}
全部评论 2
少螺宝宝~~
2024-02-27 来自 广东
1666,少螺哥哥好帅啊
2024-02-27 来自 广东
1
有帮助,赞一个