-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14940.cpp
More file actions
56 lines (53 loc) · 1.18 KB
/
14940.cpp
File metadata and controls
56 lines (53 loc) · 1.18 KB
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
56
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#define io ios::sync_with_stdio(false); cin.tie(0);
using namespace std;
int N,M;
int graph[1001][1001];
int visited[1001][1001];
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};
queue<pair<int,int>> q;
void bfs(int y,int x){
visited[y][x]=1;
q.push(make_pair(y,x));
while(!q.empty()){
auto u=q.front();
q.pop();
for(int i=0;i<4;i++){
int gy=u.first+dy[i],gx=u.second+dx[i];
if(gy<1||gy>N||gx>M||gx<1||graph[gy][gx]==0) continue;
if(visited[gy][gx]==0){
visited[gy][gx]=visited[u.first][u.second]+1;
q.push(make_pair(gy,gx));
}
}
}
}
int main()
{
io;
int x,y;
cin>>N>>M;
for(int i=1;i<=N;i++){
for(int j=1;j<=M;j++){
cin>>graph[i][j];
if(graph[i][j]==2) {
y=i;
x=j;
}
else if(graph[i][j]==0)
visited[i][j]=1;
}
}
bfs(y,x);
for(int i=1;i<=N;i++){
for(int j=1;j<=M;j++){
cout<<visited[i][j]-1<<' ';
}
cout<<'\n';
}
return 0;
}