-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path210217_BOJ_7576.cpp
More file actions
68 lines (50 loc) · 1.03 KB
/
Copy path210217_BOJ_7576.cpp
File metadata and controls
68 lines (50 loc) · 1.03 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
#include <iostream>
#include <queue>
using namespace std;
const int EMPTY = -1;
int N = 0;
int M = 0;
int v[1000][1000];
queue<pair<int, int>> q;
int main() {
ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
cin >> M >> N;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
cin >> v[i][j];
if (v[i][j] == 1)
q.push({ i, j });
}
}
int dir[4][2]{ {-1, 0}, {0, 1}, {1, 0}, {0, -1} };
while (!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int i = 0; i < 4; ++i) {
int n_x = x + dir[i][0];
int n_y = y + dir[i][1];
if (!(n_x >= 0 && n_x < N && n_y >= 0 && n_y < M))
continue;
if (v[n_x][n_y] != 0)
continue;
v[n_x][n_y] = v[x][y] + 1;
q.push({n_x, n_y});
}
}
int Max = 0;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
if (v[i][j] == 0) {
cout << "-1\n";
return 0;
}
if (v[i][j] == EMPTY)
continue;
if (v[i][j] > Max)
Max = v[i][j];
}
}
cout << Max - 1 << "\n";
return 0;
}