λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
πŸ”₯ PS(Problem Solving) πŸ”₯/BOJ

[BOJ] #2573 λΉ™μ‚°

by dar0m! 2019. 9. 4.
μ‹œκ°„ μ œν•œ λ©”λͺ¨λ¦¬ μ œν•œ μ •λ‹΅ λΉ„μœ¨
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 = 0break;
        }
        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, 0sizeof(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, 0sizeof(chk));
    }
    printf("%d", answer);
    return 0;
}
cs

 

λŒ“κΈ€