SZC_1v99 @ 2024-10-08 21:16:40
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct coord{
int xx, yy;
};
queue<coord> q;
int ans[410][410];
int dir[8][2] = {
2, 1,
1, 2,
-1, 2,
-2, 1,
-2, -1,
-1, -2,
1, -2,
2, -1
};
int n, m, x, y;
signed main(){
cin >> n >> m >> x >> y;
memset(ans, -1, sizeof(ans));
coord tmp = {x, y};
q.push(tmp);
ans[x][y] = 0;
while(!q.empty()){
coord u = q.front();
int ux = u.xx, uy = u.yy;
q.pop();
for(int i = 0;i < 8;i++){
int sx = ux + dir[i][0], sy = uy + dir[i][1];
int b = ans[ux][uy];
if(x < 1 || x > n || y < 1 || y > m || ans[sx][sy] != -1) continue;
ans[sx][sy] = b + 1;
coord tmp = {sx, sy};
q.push(tmp);
}
}
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
printf("%-5d", ans[i][j]);
}
printf("\n");
}
return 0;
}
by SZC_1v99 @ 2024-10-08 21:20:58
现在A了,但还是不懂这个代码哪里有问题
by Lisuyang @ 2024-10-08 21:22:26
x和sx,y和sy傻傻分不清 @SZC_1v99
if(x < 1 || x > n || y < 1 || y > m || ans[sx][sy] != -1) continue;
by gkhf @ 2024-10-08 21:30:18
@SZC_1v99 改成这样
if(sx < 1 || sx > n || sy < 1 || sy > m || ans[sx][sy] != -1) continue;
by SZC_1v99 @ 2024-10-08 22:50:32
@Lisuyang 谢谢
by SZC_1v99 @ 2024-10-08 22:50:47
@gkhf 谢谢