-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7576.cpp
More file actions
89 lines (80 loc) · 1.76 KB
/
Copy path7576.cpp
File metadata and controls
89 lines (80 loc) · 1.76 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
struct Point{
int x;
int y;
};
int dxdy[4][2] = { {-1,0}, {1,0}, {0,-1}, {0,1}};
queue <Point> q;
Point p;
int visited[1005][1005] = {-1};
int map[1005][1005] = {-1};
int run(){
int dx, dy, x, y, cnt=0, qsize;
while(!q.empty()){
qsize = q.size();
cnt++;
for(int i=0; i<qsize; i++){
x = q.front().x;
y = q.front().y;
q.pop();
for(int i=0; i<4; i++){
dx = x + dxdy[i][0];
dy = y + dxdy[i][1];
if(map[dx][dy]==0){
map[dx][dy] = 1;
p.x = dx;
p.y = dy;
q.push(p);
}
}
}
}
return cnt;
}
void mapReset(int n, int m){
for(int i=0; i<=n; i++){
for(int j=0; j<=m; j++){
map[i][j]=-1;
visited[i][j]=0;
}
}
}
int main() {
int n,m, cnt, k;
bool flag = false;
scanf("%d %d\n", &m, &n);
mapReset(n+2,m+2);
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
scanf("%d", &k);
map[i][j] = k;
if(k==1){
p.x = i;
p.y = j;
q.push(p);
visited[i][j]=1;
}
}
}
cnt = run();
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(map[i][j]==0){
flag=true;
}
}
}
if(flag){
printf("-1");
}
else if(cnt==1){
printf("0");
}
else{
printf("%d\n", cnt-1);
}
return 0;
}