[BOJ] 2638 치즈

Time Lapse :5min 2sec

2638.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
#include <stdio.h>
#include <queue>
using namespace std;
int cheese[100][100];
int dx[4] = { 0,1,0,-1 };
int dy[4] = { -1,0,1,0 };
int N, M;
int c;
void BFS() {
queue<pair<int, int>> q;
int visit[100][100] = { false, };
q.emplace(0, 0);
visit[0][0] = 1;
while (!q.empty()) {
int x = q.front().second;
int y = q.front().first;
q.pop();
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if (0 <= nx && nx < M && 0 <= ny && ny < N) {
if (!cheese[ny][nx] && !visit[ny][nx]) q.emplace(ny, nx);
++visit[ny][nx];
}
}
}
for(int i = 0; i < N; ++i)
for(int j = 0; j < M; ++j)
if (cheese[i][j] && visit[i][j]>=2) cheese[i][j]^=1,--c;
}
int main() {
scanf("%d %d", &N, &M);
for(int i = 0; i < N; ++i)
for (int j = 0; j < M; ++j) {
scanf("%d", &cheese[i][j]);
if (cheese[i][j]) ++c;
}
int time = 0;
while (c) {
BFS();
++time;
}
printf("%d", time);
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/30/PS/BOJ/2638/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.