반응형
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
79
|
#include <queue>
#include <iostream>
using namespace std;
int M, N;
// M:가로(x), N:세로(y)
int tmt[1002][1002] = { 0, };
int visit[1002][1002] = { 0,};
int dx[4] = { 0,0,1,-1 };
int dy[4] = { 1,-1,0,0 };
int day = -1;
queue< pair<int, int> >q;
// 1: 익은 토마토
// 0: 안익은 토마토
// -1: 토마토 x
int main() {
cin >> M >> N;
for (int num_n = 0; num_n < N; num_n++)
for (int num_m = 0; num_m < M; num_m++)
cin >> tmt[num_n][num_m];
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++) {
if (tmt[i][j] == 1) {
q.push(make_pair(i, j));
visit[i][j] = 1;
}
}
//BFS
while (!q.empty()) {
//날짜 계산을 위한 조건
int qsize = q.size();
for (int k = 0; k < qsize; k++) {
int sy = q.front().first;
int sx = q.front().second;
q.pop();
//인접 조사
for (int i = 0; i < 4; i++) {
int nx = sx + dx[i];
int ny = sy + dy[i];
if (nx >= 0 && nx < M && ny >= 0 && ny < N) {
if (visit[ny][nx] != 1 && tmt[ny][nx] == 0) {
q.push(make_pair(ny, nx));
visit[ny][nx] = 1;
tmt[ny][nx] = 1;
}
}
}
}
++day;
}
//토마토가 모두 익지는 못하는 상황
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (tmt[i][j] == 0)day = -1;
}
}
cout << day << endl;
}
|
cs |
반응형
'category > 백준 알고리즘 c++' 카테고리의 다른 글
백준 알고리즘 1920번 수 찾기 (0) | 2023.02.14 |
---|---|
백준 알고리즘 2447번 별찍기 - 10 (0) | 2023.02.14 |
백준 알고리즘 24479번 알고리즘 수업 - 깊이 우선 탐색 1 (0) | 2023.02.14 |
백준 알고리즘 1002번 터렛 (0) | 2023.02.14 |
백준 알고리즘 4344번 평균은 넘겠지 c++ (0) | 2021.01.20 |
백준 알고리즘 8958번 OX퀴즈 c++ (0) | 2021.01.20 |
백준 알고리즘 1546번 평균 c++ (0) | 2020.12.13 |
백준 알고리즘 3052번 나머지 c++ (0) | 2020.12.13 |
댓글