μκ° μ ν | λ©λͺ¨λ¦¬ μ ν | μ λ΅ λΉμ¨ |
1 μ΄ | 256 MB | 26.092 % |
2573λ²: λΉμ°
첫 μ€μλ μ΄μ°¨μ λ°°μ΄μ νμ κ°μμ μ΄μ κ°μλ₯Ό λνλ΄λ λ μ μ Nκ³Ό Mμ΄ ν κ°μ λΉμΉΈμ μ¬μ΄μ λκ³ μ£Όμ΄μ§λ€. Nκ³Ό Mμ 3 μ΄μ 300 μ΄νμ΄λ€. κ·Έ λ€μ Nκ°μ μ€μλ κ° μ€λ§λ€ λ°°μ΄μ κ° νμ λνλ΄λ Mκ°μ μ μκ° ν κ°μ λΉ μΉΈμ μ¬μ΄μ λκ³ μ£Όμ΄μ§λ€. κ° μΉΈμ λ€μ΄κ°λ κ°μ 0 μ΄μ 10 μ΄νμ΄λ€. λ°°μ΄μμ λΉμ°μ΄ μ°¨μ§νλ μΉΈμ κ°μ, μ¦, 1 μ΄μμ μ μκ° λ€μ΄κ°λ μΉΈμ κ°μλ 10,000 κ° μ΄νμ΄λ€. λ°°μ΄μ 첫 λ²μ§Έ νκ³Ό μ΄, λ§μ§
www.acmicpc.net
ν λ©μ΄λ¦¬μ λΉμ°μ΄ μ£Όμ΄μ§ λ, μ΄ λΉμ°μ΄ λ λ©μ΄λ¦¬ μ΄μμΌλ‘ λΆλ¦¬λλ μ΅μ΄μ μκ°(λ )μ ꡬνλ νλ‘κ·Έλ¨μ μμ±νμμ€.
λμλ¨λΆ λ€ λ°©ν₯μΌλ‘ μΈμ ν 0μ κ°μλ§νΌ ν μΉΈμ μλ λΉμ°μ ν¬κΈ°λ μ€μ΄λ λ€.
BFS queue μ¬μ©. = 1λ ν λΉμ° λͺ¨μ΅. DFS λ©μ΄λ¦¬ λͺ κ°μΈμ§ νμΈ. λ©μ΄λ¦¬κ° 1κ° μ΄νλΌλ©΄ μμ κ³Όμ λ°λ³΅. while
μ²μμ 무쑰건 1λ©μ΄λ¦¬ μ΄λ―λ‘ BFS λ¨Όμ λλκ² λ§μ.
μ£Όμν μ μ λ¨Όμ λΉμ°μ ν¬κΈ°λ₯Ό μ€μ¬λ²λ¦¬λ©΄ μΈμ ν λΉμ°μμλ³Όλλ μλ μμλ λΉμ°μΈλ° 0μΌλ‘ μΈμν μ μμΌλ―λ‘ chk λ°°μ΄μ νμ©νμ¬ κ·Έ λΆλΆμ λ°λ·λ¬Όμ΄ μλκ² μΈμνλλ‘ ν¨.
- testcase 1
5 7
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 10 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 - testcase 2
5 7
0 0 0 0 0 0 0
0 3 6 3 6 7 0
0 3 0 0 0 10 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
2 - testcase 3
5 7
0 0 0 0 0 0 0
0 9 8 3 8 9 0
0 9 8 0 8 9 0
0 9 8 9 8 9 0
0 0 0 0 0 0 0
5 - testcase 4
5 5
0 0 0 0 0
0 6 9 6 0
0 3 3 9 0
0 6 1 6 0
0 0 0 0 0
4
λ©λͺ¨λ¦¬ | μκ° |
2876 KB | 72 ms |
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
|
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int di[] = { 0,0,1,-1 };
int dj[] = { 1,-1,0,0 };
int n, m, cnt = -1, answer;
int arr[301][301];
int chk[301][301];
void dfs(int r, int c) {
chk[r][c] = 1;
for (int i = 0; i < 4; i++) {
int nextr = r + di[i];
int nextc = c + dj[i];
if (nextr >= 0 && nextr < n && nextc >= 0 && nextc < m) {
if (arr[nextr][nextc] && !chk[nextr][nextc]) {
dfs(nextr, nextc);
}
}
}
}
void dfs_nextyear(int r, int c) {
chk[r][c] = 1;
for (int i = 0; i < 4; i++) {
int nextr = r + di[i];
int nextc = c + dj[i];
if (nextr >= 0 && nextr < n && nextc >= 0 && nextc < m) {
if (!arr[nextr][nextc] && !chk[nextr][nextc]) {
if (arr[r][c] > 0) arr[r][c]--;
}
}
}
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &arr[i][j]);
}
}
while (cnt < 2) {
if (cnt == 0) {
answer = 0; break;
}
cnt = 0;
// 1λ
ν λ³ν λͺ¨μ΅
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr[i][j]) {
dfs_nextyear(i, j);
}
}
}
answer++;
memset(chk, 0, sizeof(chk));
// λ©μ΄λ¦¬ κ°μ νμΈ
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr[i][j] && !chk[i][j]) {
dfs(i, j);
cnt++;
}
}
}
memset(chk, 0, sizeof(chk));
}
printf("%d", answer);
return 0;
}
|
cs |
'π₯ PS(Problem Solving) π₯ > BOJ' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[BOJ] #1946 μ μ μ¬μ (0) | 2019.09.04 |
---|---|
[BOJ] #1541 μμ΄λ²λ¦° κ΄νΈ (0) | 2019.09.04 |
[BOJ] #2667 λ¨μ§λ²νΈλΆμ΄κΈ° (0) | 2019.09.04 |
[BOJ] #2644 μ΄μκ³μ° (0) | 2019.09.04 |
[BOJ] #6603 λ‘λ (0) | 2019.09.04 |
λκΈ