-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path[백준 알고리즘] 빙산.txt
97 lines (80 loc) · 1.69 KB
/
[백준 알고리즘] 빙산.txt
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
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef struct loc {
int r, c;
int minor;
}Loc;
int dr[4] = {-1,0,1,0};
int dc[4] = { 0,1,0,-1 };
int result;
int whole;
int n, m;
int arr[303][303];
bool visit[303][303];
vector <Loc> root;
queue <Loc> visitCheck;
void dfs(Loc temp) {
for (int dir = 0; dir < 4; dir++) {
int r = temp.r + dr[dir];
int c = temp.c + dc[dir];
if (r >= 0 && r < n && c >= 0 && c < m && arr[r][c] != 0 && visit[r][c] == false) {
whole+=1;
visit[r][c] = true;
dfs({ r,c });
}
}
}
int main() {
result = 0;
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &arr[i][j]);
if (arr[i][j] != 0) {
root.push_back({ i,j });
}
}
}
for (int time = 1; ; time++) {
//printf("%d\n", time);
for (int i = 0; i < root.size(); i++) { //순차접근시의 문제
int count = 0;
for (int dir = 0; dir < 4; dir++) {
if (arr[root[i].r + dr[dir]][root[i].c + dc[dir]] == 0) {
count++;
}
}
root[i].minor = count;
}
for (int i = 0; i < root.size(); i++) { //순차접근시의 문제
arr[root[i].r][root[i].c] -=root[i].minor;
if (arr[root[i].r][root[i].c] <= 0) {
arr[root[i].r][root[i].c] = 0;
root.erase(root.begin() + i);
i--;
}
}
if (root.empty()) {break;}
if (!root.empty()) {
whole++;
visit[root[0].r][root[0].c] = true;
dfs(root[0]);
}
//printf("whole:%d rootsize:%d\n", whole, root.size());
if (whole != root.size()) {
result = time;
break;
}
whole = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
visit[i][j] = false;
}
}
}
printf("%d", result);
return 0;
}