-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7576.cpp
More file actions
78 lines (59 loc) · 1.29 KB
/
7576.cpp
File metadata and controls
78 lines (59 loc) · 1.29 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
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <queue>
using namespace std;
struct position {
int row;
int col;
position(int r, int c) : row(r), col(c) {}
};
queue<position> Queue;
int M, N;
int cnt = 0; // 토마토의 개수
int day = 0; // 일수
int endNumber;
int map[1000][1000] = { 0, };
int answer_map[1000][1000] = { 0, };
int visit[1000][1000] = { 0, };
// 동 서 남 북
int row_dir[4] = { 0,0,1,-1 };
int col_dir[4] = { 1,-1,0,0 };
void BFS(int row, int col) {
visit[row][col] = 1;
for (int i = 0; i < 4; i++) {
// 범위 안에 있으면 queue에 넣자.
if (row + row_dir[i] >= 0 && row + row_dir[i] < N && col + col_dir[i] >= 0 && col + col_dir[i] < M) {
Queue.push(position(row + row_dir[i], col + col_dir[i]));
}
}
while (!Queue.empty()) {
position temp = Queue.front();
Queue.pop();
if (!visit[temp.row][temp.col]) {
BFS(temp.row, temp.col);
}
}
if (cnt == endNumber) {
return;
}
cnt++;
}
int main(void) {
// row : N, col : M
scanf("%d %d", &M, &N);
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
scanf("%d", &map[i][j]);
}
}
endNumber = M * N;
// 본 적 없는 토마토면 탐색
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (map[i][j] && !visit[i][j]) {
BFS(i, j);
}
}
}
printf("%d\n", day);
return 0;
}