P1443
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
#define maxn 310
struct node
{
int x,y;
};
queue<node> Q;
int a[maxn][maxn];
int horse[8][2]={{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}};
int main()
{
int n,m,b,c;
cin>>n>>m>>b>>c;
memset(a,-1,sizeof(a));
node s={b,c};
Q.push(s);
a[b][c]=0;
while(!Q.empty())
{
node h=Q.front();
int hx=h.x;
int hy=h.y;
Q.pop();
for(int i=0;i<8;i++)
{
int x=hx+horse[i][0];
int y=hy+horse[i][1];
int v=a[hx][hy];
if(x>n||x<1||y>m||y<1||a[x][y]!=-1)
{
continue;
}
a[x][y]=v+1;
node s={x,y};
Q.push(s);
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]==-1)
{
cout<<a[i][j]<<" ";
}
else
{
cout<<a[i][j]<<" ";
}
}
cout<<endl;
}
return 0;
}
哎