|
- #include <cstdio>
- #include <cstring>
- using namespace std;
- const int N = 410;
- int m,n,xx,yy, a[N][N], vis[N][N];
- int p[N * N][2],top,tail=1;
- int dx[8]={-2,-1,1,2,+2,+1,-1,-2};
- int dy[8]={+1,+2,2,1,-1,-2,-2,-1};
- void bfs(int x,int y)
- {
- for(int i=0;i<=7;i++)
- {
- int nowx=x+dx[i];
- int nowy=y+dy[i];
- if(nowx<1 || nowy<1 || nowx>n || nowy>m) continue;
- if(vis[nowx][nowy]) continue;
- vis[nowx][nowy]=1;
- a[nowx][nowy]=a[x][y]+1;
- top++;
- p[top][0]=nowx;
- p[top][1] = nowy;
- }
- if(tail>top) return;
- tail++;
- bfs(p[tail-1][0],p[tail-1][1]);
- return;
- }
- int main()
- {
- memset (a,-1,sizeof(a));
- scanf ("%d%d%d%d",&n,&m,&xx,&yy);
- vis[xx][yy]=1;
- a[xx][yy]=0;
- bfs(xx,yy);
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=m;j++) printf("%-5d",a[i][j]);
- printf("\n");
- }
- return 0;
- }
复制代码 |
|