@[dessert](/space/show?uid=61268)
用广搜
by 御坂13558号 @ 2019-01-23 09:14:16
@[璀璨天狼](/space/show?uid=144520) 想用深搜尝试一下捏
by dessert @ 2019-01-23 09:23:47
用广搜,不要用深搜
by A_tourist @ 2019-01-23 09:40:57
```cpp
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int m,n,a[401][401],x,y;
queue<int>qx;
queue<int>qy;
int fx[9]={0,-2,-2,-1,1,2,2,1,-1};
int fy[9]={0,-1,1,2,2,1,-1,-2,-2};
void bfs(int x,int y)
{
qx.push(x);
qy.push(y);
while(!qx.empty())
{
int nowx=qx.front();
int nowy=qy.front();
qx.pop();
qy.pop();
for(int i=1;i<=8;i++)
{
int xx=nowx+fx[i];
int yy=nowy+fy[i];
if(xx<=n&&xx>=1&&yy<=m&&yy>=1&&a[xx][yy]==-1)
{
qx.push(xx);
qy.push(yy);
a[xx][yy]=a[nowx][nowy]+1;
}
}
}
}
int main()
{
memset(a,-1,sizeof(a));
scanf("%d%d%d%d",&n,&m,&x,&y);
a[x][y]=0;
bfs(x,y);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
printf("%-5d",a[i][j]);
}
cout<<endl;
}
return 0;
}
```
by A_tourist @ 2019-01-23 09:41:40