蒟蒻用的BFS只拿90分,第十个测试点没过,求调!
#include <iostream>
#include <queue>
using namespace std;
const int N = 4e2 + 10;
int n,m,x,y,ans[N][N];
bool vis[N][N];
struct Bit
{
int x,y,v;
};
queue <Bit> q;
void bfs()
{
q.push({x,y,0});
int _x = x,_y = y;
while (!q.empty())
{
Bit tot = q.front();
q.pop();
int x = tot.x,y = tot.y,v = tot.v;
if (vis[x][y] || x > n || x < 1 || y > m || y < 1) continue;
vis[x][y] = 1;
ans[x][y] = v;
q.push({x + 2,y + 1,v + 1});
q.push({x + 1,y + 2,v + 1});
q.push({x - 2,y + 1,v + 1});
q.push({x - 1,y + 2,v + 1});
q.push({x + 2,y - 1,v + 1});
q.push({x + 1,y - 2,v + 1});
q.push({x - 1,y - 2,v + 1});
q.push({x - 2,y - 1,v + 1});
}
for(int i = 1;i <= n;i ++)
for(int j = 1;j <= m;j ++)
if (ans[i][j] == 0 && i != _x && j != _y)
ans[i][j] = -1;
}
int main()
{
cin >> n >> m >> x >> y;
bfs();
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= m;j ++)
printf("%d ",ans[i][j]);
printf("\n");
}
return 0;
}