forked from tony9402/baekjoon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
60 lines (58 loc) · 1.46 KB
/
main.cpp
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
// Authored by : seastar105
// Co-authored by : -
// Link : http://boj.kr/b632f242053b45c4bc98739905f01d18
#include<bits/stdc++.h>
using namespace std;
int N, M;
int arr[1005][1005];
int dist[1005][1005];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
bool valid(int x, int y) {
return 0<=x && x<N && 0<=y && y<M;
}
int main() {
cin.tie(nullptr); ios::sync_with_stdio(false);
cin >> M >> N;
int tot = 0, done = 0;
for(int i=0;i<N;++i) {
for(int j=0;j<M;++j) {
cin >> arr[i][j];
if(arr[i][j] >= 0) ++tot;
if(arr[i][j] == 1) ++done;
}
}
if(tot == done) {
cout << 0 << '\n';
return 0;
}
memset(dist, -1, sizeof(dist));
queue<pair<int,int>> q;
for(int i=0;i<N;++i) {
for(int j=0;j<M;++j) {
if(arr[i][j] == 1) {
dist[i][j] = 0;
q.emplace(i, j);
}
}
}
int ans = 0;
while(!q.empty()) {
int x = q.front().first;
int y = q.front().second;
q.pop();
ans = max(ans, dist[x][y]);
for(int i=0;i<4;++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if(!valid(nx, ny)) continue;
if(arr[nx][ny] == -1 || dist[nx][ny] != -1) continue;
dist[nx][ny] = dist[x][y]+1;
++done;
q.emplace(nx, ny);
}
}
if(tot == done) cout << ans << '\n';
else cout << -1 << '\n';
return 0;
}