-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7576_토마토.cpp
70 lines (55 loc) · 1.32 KB
/
7576_토마토.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
61
62
63
64
65
66
67
68
69
70
#include <bits/stdc++.h>
using namespace std;
int arr[1000][1000];
int darr[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int M, N;
cin >> M >> N;
queue<pair<int, int>> q;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
cin >> arr[i][j];
if (arr[i][j] == 1)
q.push({j, i});
}
}
while (!q.empty())
{
auto [x, y] = q.front();
q.pop();
for (auto [dx, dy] : darr)
{
int nx = x + dx, ny = y + dy;
if (nx < 0 || nx >= M || ny < 0 || ny >= N)
continue;
if (arr[ny][nx])
continue;
arr[ny][nx] = arr[y][x] + 1;
q.push({nx, ny});
}
}
int answer = 1;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
if (arr[i][j] == -1)
continue;
if (arr[i][j] == 0)
{
answer = 0;
break;
}
answer = max(answer, arr[i][j]);
}
if (answer == 0)
break;
}
cout << answer - 1 << '\n';
return 0;
}