-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBITMAP.cpp
More file actions
52 lines (50 loc) · 1012 Bytes
/
Copy pathBITMAP.cpp
File metadata and controls
52 lines (50 loc) · 1012 Bytes
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
#include <iostream>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
vector <vector<int>> bfs(string s[], int n, int m){
int i, j, k, x, y, dx[4]={0, 1, 0, -1}, dy[4]={1, 0, -1, 0};
vector <vector<int>> dist(n, vector<int>(m, -1));
queue <pair<int, int>> q;
for(i=0; i<n; i++){
for(j=0; j<m; j++){
if(s[i][j]=='1'){
dist[i][j]=0;
q.push(make_pair(i, j));
}
}
}
while(!q.empty()){
i=q.front().first;
j=q.front().second;
for(k=0; k<4; k++){
x=i+dx[k];
y=j+dy[k];
if(x>=0 && x<n && y>=0 && y<m && dist[x][y]==-1){
dist[x][y]=dist[i][j]+1;
q.push(make_pair(x, y));
}
}
q.pop();
}
return dist;
}
int main(){
int t, n, m, i, j;
vector <vector<int>> dist;
cin>>t;
while(t--){
cin>>n>>m;
string bitmap[n];
for(i=0; i<n; i++)
cin>>bitmap[i];
dist=bfs(bitmap, n, m);
for(i=0; i<n; i++){
for(j=0; j<m; j++)
cout<<dist[i][j]<<" ";
cout<<"\n";
}
}
return 0;
}