erty
2024-08-10 19:36:41
发布于:广东
#include<bits/stdc++.h>
using namespace std;
int ans[401][401];
int vis[401][401];
int xy[8][2] = {{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
int n,m,kx,ky;
bool flag;
struct node{
int x,y;
int step;
};
queue <node> q;
void bfs(){
while(!q.empty()){
node t = q.front();
q.pop();
int cx = t.x,cy = t.y;
ans[cx][cy] = t.step;
for(int i = 0;i < 8;i++){
int tx = xy[i][0] + t.x;
int ty = xy[i][1] + t.y;
if(tx >= 1 && tx <= n && ty >= 1 && ty <= m && !vis[tx][ty]){
q.push({tx,ty,t.step+1});
vis[tx][ty] = 1;
}
}
}
return;
}
int main(){
memset(ans,-1,sizeof(ans));
cin >> n >> m >> kx >> ky;
q.push({kx,ky,0});
vis[kx][ky] = 1;
bfs();
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
cout << ans[i][j] << " ";
}
cout << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个