-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7576.cpp
More file actions
67 lines (61 loc) · 1.41 KB
/
Copy path7576.cpp
File metadata and controls
67 lines (61 loc) · 1.41 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
#include<iostream>
#include<queue>
using namespace std;
queue<pair<int, int> > q;
int map[1001][1001] = { 0 };
bool visit[1001][1001] = { 0 };
int dx[] = { -1,1,0,0 };
int dy[] = { 0,0,-1,1 };
int main()
{
int n, m;
cin >> m >> n;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> map[i][j];
if (map[i][j] == 1)
{
q.push(make_pair(i, j));
visit[i][j] = true;
}
else if (map[i][j] == -1)visit[i][j] = true;
}
}
int ans = -1;
while (!q.empty())
{
int size = q.size();
ans++;
while (size--)
{
pair<int, int> now = q.front(); q.pop();
for (int k = 0; k < 4; k++)
{
int qx = now.second + dx[k];
int qy = now.first + dy[k];
if (qx >= 0 && qx < m && qy >= 0 && qy < n)
{
if (!visit[qy][qx] && !map[qy][qx])
{
visit[qy][qx] = 1;
q.push(make_pair(qy, qx));
}
}
}
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (!visit[i][j]){
cout << -1;
return 0;
}
}
}
cout << ans;
return 0;
}