-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJ_7576_토마토.cpp
More file actions
69 lines (66 loc) · 1.57 KB
/
BJ_7576_토마토.cpp
File metadata and controls
69 lines (66 loc) · 1.57 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
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int farm[1000][1000] = { 0 };
bool visited[1000][1000] = { false };
int main() {
int i, j, status;
int M, N;
int days = 0, ripped = 0, unripped = 0;
queue<pair<pair<int, int>,int> > tomato;
// 1. ÀÔ·Â
cin >> M >> N;
for (i = 0; i < N; i++) {
for (j = 0; j < M; j++) {
cin >> status;
farm[i][j] = status;
if (status == 0)
unripped++;
else if (status == 1) {
ripped++;
tomato.push(make_pair(make_pair(i, j),0));
visited[i][j] = true;
}
}
}
// 2. ã±â.
if (unripped == 0) days = 0;
else {
while (!tomato.empty()) {
int x = tomato.front().first.first;
int y = tomato.front().first.second;
int c = tomato.front().second;
tomato.pop();
// ÃÖ°í³¯Â¥
if (days < c) days = c;
if (x - 1 >= 0 && !visited[x - 1][y] && farm[x - 1][y] == 0) {
unripped--;
ripped++;
tomato.push(make_pair(make_pair(x-1, y), c+1));
visited[x-1][y] = true;
}
if (x + 1 < N && !visited[x + 1][y] && farm[x + 1][y] == 0) {
unripped--;
ripped++;
tomato.push(make_pair(make_pair(x + 1, y), c + 1));
visited[x + 1][y] = true;
}
if (y - 1 >= 0 && !visited[x][y-1] && farm[x][y-1] == 0) {
unripped--;
ripped++;
tomato.push(make_pair(make_pair(x, y-1), c + 1));
visited[x][y-1] = true;
}
if (y + 1 < M && !visited[x][y+1] && farm[x][y+1] == 0) {
unripped--;
ripped++;
tomato.push(make_pair(make_pair(x, y+1), c + 1));
visited[x][y+1] = true;
}
}
if (unripped > 0) days = -1;
}
// 3. Ãâ·Â
cout << days;
}