-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14940.cpp
More file actions
65 lines (60 loc) · 1.59 KB
/
14940.cpp
File metadata and controls
65 lines (60 loc) · 1.59 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
57
58
59
60
61
62
63
64
65
#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m;
cin>>n>>m;
vector<vector<int>> board(n, vector<int>(m));
pair<int,int> start;
vector<vector<int>> time(n, vector<int>(m,-1));
vector<vector<bool>> visited(n, vector<bool>(m, false));
queue<pair<int,int>> q;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cin>>board[i][j];
if(board[i][j]==2){
start={i,j};
time[i][j]=0;
q.push({i,j});
visited[i][j]=true;
}
if(board[i][j]==0){
time[i][j]=0;
}
}
}
int cnt=0;
int dx[4]={1,0,-1,0};
int dy[4]={0,-1,0,1};
while(!q.empty()){
int size = q.size();
cnt++;
for(int i=0; i<size; i++){
start=q.front();
q.pop();
for(int j=0; j<4; j++){
int nx = start.second+dx[j];
int ny = start.first+dy[j];
if(nx<0||ny<0||nx>=m||ny>=n){
continue;
}
if(!visited[ny][nx]){
if(board[ny][nx]==1){
q.push({ny,nx});
visited[ny][nx]=true;
time[ny][nx]=cnt;
}
visited[ny][nx]=true;
}
}
}
}
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
cout<<time[i][j]<<" ";
}
cout<<"\n";
}
}