-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path251215_2638_치즈_G3_DFS
More file actions
78 lines (62 loc) · 1.38 KB
/
251215_2638_치즈_G3_DFS
File metadata and controls
78 lines (62 loc) · 1.38 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
//O((NM)^2)
//dfs이용 별거 없음. 바깥에 있는 치즈를 찾기 위해 바깥쪽만 쭉 돌면서 dfs로 바깥 치즈를 찾음.
#include <iostream>
#include <vector>
using namespace std;
int N, M;
int result = 0;
vector<vector<int>> arr;
vector<vector<int>> visited;
int dirx[] = { 0,0,-1,1 };
int diry[] = { 1,-1,0,0 };
void dfs(int x,int y) {
visited[x][y] = -1;
for (int i = 0; i < 4; i++) {
int forx = dirx[i] + x;
int fory = diry[i] + y;
if (forx < 0 or forx >= N or fory < 0 or fory >= M) continue;
if (visited[forx][fory] == 0 and arr[forx][fory] == 0) dfs(forx, fory);
if (arr[forx][fory] == 1) visited[forx][fory]++;
}
}
void melt_cheese() {
visited.assign(N, vector<int>(M, 0));
for (int i = 0; i < N; i++) {
if (visited[i][0] == 0)
dfs(i, 0);
if (visited[i][M - 1] == 0)
dfs(i, N - 1);
}
for (int i = 0; i < M; i++) {
if (visited[0][i] == 0)
dfs(i, 0);
if (visited[N-1][i] == 0)
dfs(i, N - 1);
}
int cnt = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (visited[i][j] >= 2) {
arr[i][j] = 0;
cnt++;
}
}
}
if (cnt > 0) {
result++;
melt_cheese();
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
arr.assign(N, vector<int>(M, 0));
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> arr[i][j];
}
}
melt_cheese();
cout << result;
}