1 #include <bits/stdc++.h> 2 #define _for(i,a,b) for(int i = a;i < b;i ++) 3 #define maxn 402 4 #define INF 0x3f3f3f3f 5 using namespace std; 6 struct P 7 { 8 int x; 9 int y; 10 P(int nx,int ny):x(nx),y(ny) {} 11 P() {} 12 }; 13 int m,n; 14 P st; 15 int mp[maxn][maxn]; 16 int vis[maxn][maxn]; 17 int dx[] = {-2,-2,-1,-1,1,1,2,2}; 18 int dy[] = {-1,1,-2,2,-2,2,-1,1}; 19 bool valid(int x,int y) 20 { 21 if(x>=0 && y>=0 && x<m && y<n && vis[x][y]==0) 22 return true; 23 return false; 24 } 25 void bfs() 26 { 27 queue<pair<P,int>> q; 28 q.push({st,0}); 29 vis[st.x][st.y] = 1; 30 while(!q.empty()) 31 { 32 P pos = q.front().first; 33 int times = q.front().second; 34 q.pop(); 35 mp[pos.x][pos.y] = times; 36 37 _for(i,0,8) 38 { 39 int nx = pos.x+dx[i]; 40 int ny = pos.y+dy[i]; 41 if(valid(nx,ny)) 42 { 43 q.push({P(nx,ny),times+1}); 44 vis[nx][ny] = 1; 45 } 46 } 47 } 48 } 49 int main() 50 { 51 scanf("%d%d%d%d",&m,&n,&st.x,&st.y); 52 memset(mp,-1,sizeof(mp)); 53 memset(vis,0,sizeof(vis)); 54 st.x --,st.y --; 55 bfs(); 56 57 _for(i,0,m) 58 _for(j,0,n) 59 printf("%-5d",mp[i][j]); 60 printf(" "); 61 return 0; 62 }