求大佬救救 555
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 500;
int n, m;
int X1, Y1;//起点坐标
int dx[] = { -1,-2,-2,-1,1,2,2,1 };
int dy[] = { 2,1,-1,-2,2,1,-1,-2 };
bool vis[N][N];
int f[N][N];//步数数组
struct ma
{
int x; int y;
};
void bfs(ma a)
{
queue <ma>q; q.push(a);
while (!q.empty())
{
int x = q.front().x;
int y = q.front().y;
q.pop();
for (int i = 0; i <= 7; i++)
{
int xx = x + dx[i]; int yy = y + dy[i];
if (xx > 0 && yy > 0 && xx <= n && yy <= m && vis[xx][yy] == 0)//在地图内 且未被遍历过
{
vis[xx][yy] = 1;
f[xx][yy] = f[x][y] + 1;
q.push({ xx,yy });
}
}
}
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
memset(f, -1, sizeof(f));
cin >> n >> m;
cin >> X1 >> Y1;
ma a = { X1,Y1 };
f[X1][Y1] = 0;
vis[X1][Y1] = 1;
bfs(a);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
printf("%-5d", f[i][j]);
}
cout << '\n';
}
return 0;
}