1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#include <bits/stdc++.h>
using namespace std;
struct node{
int x, y, t;
};
queue<node> q;
int m,n,sx,sy;
int ans[401][401];
int dir[][2] = {{1,2},{1,-2},{2,1},{2,-1},{-1,2},{-1,-2},{-2,1},{-2,-1}};
bool vis[401][401];
void bfs(int sx,int sy){
q.push((node){sx,sy,0});
vis[sx][sy] = 1;
ans[sx][sy] = 0;
while(!q.empty()){
node now = q.front();
q.pop();
for(int i = 0 ;i < 8;i++){
int nx = now.x + dir[i][0];
int ny = now.y + dir[i][1];
if(vis[nx][ny] || nx < 1||ny < 1||nx > n||ny > m)continue;
vis[nx][ny] = 1;
ans[nx][ny] = ans[now.x][now.y] + 1;
q.push( (node){nx , ny , now.t+1} );
}
}
}
int main(){
cin >> n >> m >> sx >> sy;
bfs(sx,sy);
for(int i = 1 ; i <= n ; i++){
for(int j = 1 ; j <= m ; j++){
if(vis[i][j])printf("%-5d",ans[i][j]);
else cout << "-1 ";
}cout<<endl;
}
return 0;
}
|