https://www.acmicpc.net/problem/2638
앞에서 풀었던 2636번 치즈 (풀이 링크)문제와 비슷한 문제이다.
다른 점은 4 변 중 2 변 이상이 공기에 접촉해야 치즈가 놓는다.
답도 시간만 출력하면 된다.
앞의 문제와 마찬가지로 테두리에는 치즈가 없으므로
(0,0)을 먼저 큐에 넣어 바깥 영역을 먼저 bfs로 구하고
치즈가 두번 이상 바깥 영역(공기 부분)과 접촉해있다면 치즈가 녹는다(0이 된다).
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
int r, c;
int board[100][100];
bool check[100][100];
int dx[] = { 0,0,1,-1 };
int dy[] = { 1,-1,0,0 };
//치즈수를 카운트
int countCheese() {
int cnt = 0;
for (int i = 1; i < r - 1; i++) {
for (int j = 1; j < c - 1; j++) {
if (board[i][j] == 1) {
cnt++;
}
}
}
return cnt;
}
void solve() {
//bfs로 바깥 영역을 체크
queue<pair<int, int>> q;
memset(check, false, sizeof(check));
int x = 0;
int y = 0;
q.push(make_pair(x,y));
check[x][y] = true;
while (!q.empty()) {
x = q.front().first;
y = q.front().second;
q.pop();
for (int k = 0; k < 4; k++) {
int nx = x + dx[k];
int ny = y + dy[k];
if (nx < 0 || ny < 0 || nx >= r || ny >= c) continue;
if (check[nx][ny]) continue;
if (board[nx][ny] == 1) continue;
q.push(make_pair(nx, ny));
check[nx][ny] = true;;
}
}
for (int i = 1; i < r-1; i++) {
for (int j = 1; j < c-1; j++) {
if (board[i][j] == 0) continue;
int cnt = 0;
//네방향을 검사
for (int k = 0; k < 4; k++) {
int nx = i + dx[k];
int ny = j + dy[k];
if (check[nx][ny]) {
//공기에 접촉하는 횟수
cnt++;
}
}
//두 변 이상 공기에 접촉하면 녹는다.
if (cnt >= 2) {
board[i][j] = 0;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> r >> c;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> board[i][j];
}
}
int time = 0;
while (true) {
int tmp = countCheese();
//치즈가 모두 녹으면 break
if (tmp == 0) {
break;
}
solve();
time++;
}
cout << time << '\n';
return 0;
}
Colored by Color Scripter
|
'BOJ' 카테고리의 다른 글
[BOJ] 1987. 알파벳 (0) | 2019.07.11 |
---|---|
[BOJ] 7562. 나이트의 이동 (0) | 2019.07.09 |
[BOJ] 2636. 치즈 (0) | 2019.07.08 |
[BOJ] 1347. 미로 만들기 (0) | 2019.07.06 |
[BOJ] 14620. 꽃길 (0) | 2019.07.06 |