hczyy @ 2024-08-15 08:51:55
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 410;
struct node{
int x, y, step;
}q[MAXN * MAXN];
int n, m, x, y ,front = 1, vis[MAXN][MAXN] ,rear, d[8][2] = {{2,1},{-2,1},{-2,-1},{2,-1},{1,2},{1,-2},{-1,-2},{-1,2}};
void bfs(){
memset(vis, -1, sizeof(vis));
front = 1, rear = 1;
q[rear].x = x, q[rear].y = y,q[rear].step = 0;
vis[x][y] = 0;
while(front <= rear){
node u = q[front];
front++;
for(int i = 0; i < 8; i++){
node t;
t.x = u.x + d[i][0], t.y = u.y + d[i][1], t.step = u.step + 1;
if(t.x >= 1 && t.y >= 1 && t.x <= n && t.y <= m && vis[t.x][t.y] == -1){
vis[t.x][t.y] = u.step;
q[++rear] = t;
}
}
}
}
int main(){
cin >> n >> m >> x >> y;
bfs();
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
printf("%-5d",vis[i][j]);
}
cout << endl;
}
return 0;
}
by DEMAC @ 2024-08-15 08:56:37
ub 了?我这里样例都过不去。
by hczyy @ 2024-08-15 09:00:14
@DEMAC 但我这里是全过啊为什么
by DEMAC @ 2024-08-15 09:04:33
粘错代码/弄混答案和输出/宇宙射线击穿你的计算机都是可能的。
line 23应为: vis[t.x][t.y] = t.step;
以及你诡异的队列实现得改改/qq
by hczyy @ 2024-08-15 09:06:40
@DEMAC 改完ac了谢谢,至于队列是手写队列,关注了谢谢