Post

๐Ÿข ๋น™์‚ฐ

1. ๋ฌธ์ œ ๋งํฌ

2573๋ฒˆ: ๋น™์‚ฐ


2. ์ฝ”๋“œ

Python3 33268KB 4020ms

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""
[2573] ๋น™์‚ฐ

๐Ÿ’› ๋ฌธ์ œ
์ง€๊ตฌ ์˜จ๋‚œํ™”๋กœ ์ธํ•˜์—ฌ ๋ถ๊ทน์˜ ๋น™์‚ฐ์ด ๋…น๊ณ  ์žˆ๋‹ค.
๋น™์‚ฐ์„ ๊ทธ๋ฆผ 1๊ณผ ๊ฐ™์ด 2์ฐจ์› ๋ฐฐ์—ด์— ํ‘œ์‹œํ•œ๋‹ค๊ณ  ํ•˜์ž.
๋น™์‚ฐ์˜ ๊ฐ ๋ถ€๋ถ„๋ณ„ ๋†’์ด ์ •๋ณด๋Š” ๋ฐฐ์—ด์˜ ๊ฐ ์นธ์— ์–‘์˜ ์ •์ˆ˜๋กœ ์ €์žฅ๋œ๋‹ค.
๋น™์‚ฐ ์ด์™ธ์˜ ๋ฐ”๋‹ค์— ํ•ด๋‹น๋˜๋Š” ์นธ์—๋Š” 0์ด ์ €์žฅ๋œ๋‹ค.
๊ทธ๋ฆผ 1์—์„œ ๋นˆ์นธ์€ ๋ชจ๋‘ 0์œผ๋กœ ์ฑ„์›Œ์ ธ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค.

 	2	4	5	3
 	3	 	2	5	2
 	7	6	2	4

<๊ทธ๋ฆผ 1. ํ–‰์˜ ๊ฐœ์ˆ˜๊ฐ€ 5์ด๊ณ  ์—ด์˜ ๊ฐœ์ˆ˜๊ฐ€ 7์ธ 2์ฐจ์› ๋ฐฐ์—ด์— ์ €์žฅ๋œ ๋น™์‚ฐ์˜ ๋†’์ด ์ •๋ณด>

๋น™์‚ฐ์˜ ๋†’์ด๋Š” ๋ฐ”๋‹ท๋ฌผ์— ๋งŽ์ด ์ ‘ํ•ด์žˆ๋Š” ๋ถ€๋ถ„์—์„œ ๋” ๋นจ๋ฆฌ ์ค„์–ด๋“ค๊ธฐ ๋•Œ๋ฌธ์—,
๋ฐฐ์—ด์—์„œ ๋น™์‚ฐ์˜ ๊ฐ ๋ถ€๋ถ„์— ํ•ด๋‹น๋˜๋Š” ์นธ์— ์žˆ๋Š” ๋†’์ด๋Š” ์ผ๋…„๋งˆ๋‹ค ๊ทธ ์นธ์— ๋™์„œ๋‚จ๋ถ ๋„ค ๋ฐฉํ–ฅ์œผ๋กœ ๋ถ™์–ด์žˆ๋Š” 0์ด ์ €์žฅ๋œ ์นธ์˜ ๊ฐœ์ˆ˜๋งŒํผ ์ค„์–ด๋“ ๋‹ค.
๋‹จ, ๊ฐ ์นธ์— ์ €์žฅ๋œ ๋†’์ด๋Š” 0๋ณด๋‹ค ๋” ์ค„์–ด๋“ค์ง€ ์•Š๋Š”๋‹ค.
๋ฐ”๋‹ท๋ฌผ์€ ํ˜ธ์ˆ˜์ฒ˜๋Ÿผ ๋น™์‚ฐ์— ๋‘˜๋Ÿฌ์‹ธ์—ฌ ์žˆ์„ ์ˆ˜๋„ ์žˆ๋‹ค.
๋”ฐ๋ผ์„œ ๊ทธ๋ฆผ 1์˜ ๋น™์‚ฐ์€ ์ผ๋…„ํ›„์— ๊ทธ๋ฆผ 2์™€ ๊ฐ™์ด ๋ณ€ํ˜•๋œ๋‹ค.

 	 	2	4	1
 	1	 	1	5
 	5	4	1	2

<๊ทธ๋ฆผ 2>

๊ทธ๋ฆผ 3์€ ๊ทธ๋ฆผ 1์˜ ๋น™์‚ฐ์ด 2๋…„ ํ›„์— ๋ณ€ํ•œ ๋ชจ์Šต์„ ๋ณด์—ฌ์ค€๋‹ค.

 	 	 	3
 	 	 	 	4
 	3	2

<๊ทธ๋ฆผ 3>

2์ฐจ์› ๋ฐฐ์—ด์—์„œ ๋™์„œ๋‚จ๋ถ ๋ฐฉํ–ฅ์œผ๋กœ ๋ถ™์–ด์žˆ๋Š” ์นธ๋“ค์€ ์„œ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๊ณ  ๋งํ•œ๋‹ค.
๋”ฐ๋ผ์„œ ๊ทธ๋ฆผ 2์˜ ๋น™์‚ฐ์€ ํ•œ ๋ฉ์–ด๋ฆฌ์ด์ง€๋งŒ, ๊ทธ๋ฆผ 3์˜ ๋น™์‚ฐ์€ ์„ธ ๋ฉ์–ด๋ฆฌ๋กœ ๋ถ„๋ฆฌ๋˜์–ด ์žˆ๋‹ค

ํ•œ ๋ฉ์–ด๋ฆฌ์˜ ๋น™์‚ฐ์ด ์ฃผ์–ด์งˆ ๋•Œ, ์ด ๋น™์‚ฐ์ด ๋‘ ๋ฉ์–ด๋ฆฌ ์ด์ƒ์œผ๋กœ ๋ถ„๋ฆฌ๋˜๋Š” ์ตœ์ดˆ์˜ ์‹œ๊ฐ„(๋…„)์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.
๊ทธ๋ฆผ 1์˜ ๋น™์‚ฐ์— ๋Œ€ํ•ด์„œ๋Š” 2๊ฐ€ ๋‹ต์ด๋‹ค. ๋งŒ์ผ ์ „๋ถ€ ๋‹ค ๋…น์„ ๋•Œ๊นŒ์ง€ ๋‘ ๋ฉ์–ด๋ฆฌ ์ด์ƒ์œผ๋กœ ๋ถ„๋ฆฌ๋˜์ง€ ์•Š์œผ๋ฉด ํ”„๋กœ๊ทธ๋žจ์€ 0์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ’š ์ž…๋ ฅ
์ฒซ ์ค„์—๋Š” ์ด์ฐจ์› ๋ฐฐ์—ด์˜ ํ–‰์˜ ๊ฐœ์ˆ˜์™€ ์—ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ์ •์ˆ˜ N๊ณผ M์ด ํ•œ ๊ฐœ์˜ ๋นˆ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค.
N๊ณผ M์€ 3 ์ด์ƒ 300 ์ดํ•˜์ด๋‹ค.
๊ทธ ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ์ค„๋งˆ๋‹ค ๋ฐฐ์—ด์˜ ๊ฐ ํ–‰์„ ๋‚˜ํƒ€๋‚ด๋Š” M๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ํ•œ ๊ฐœ์˜ ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค.
๊ฐ ์นธ์— ๋“ค์–ด๊ฐ€๋Š” ๊ฐ’์€ 0 ์ด์ƒ 10 ์ดํ•˜์ด๋‹ค.
๋ฐฐ์—ด์—์„œ ๋น™์‚ฐ์ด ์ฐจ์ง€ํ•˜๋Š” ์นธ์˜ ๊ฐœ์ˆ˜, ์ฆ‰, 1 ์ด์ƒ์˜ ์ •์ˆ˜๊ฐ€ ๋“ค์–ด๊ฐ€๋Š” ์นธ์˜ ๊ฐœ์ˆ˜๋Š” 10,000 ๊ฐœ ์ดํ•˜์ด๋‹ค.
๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ํ–‰๊ณผ ์—ด, ๋งˆ์ง€๋ง‰ ํ–‰๊ณผ ์—ด์—๋Š” ํ•ญ์ƒ 0์œผ๋กœ ์ฑ„์›Œ์ง„๋‹ค.

๐Ÿ’™ ์ถœ๋ ฅ
์ฒซ ์ค„์— ๋น™์‚ฐ์ด ๋ถ„๋ฆฌ๋˜๋Š” ์ตœ์ดˆ์˜ ์‹œ๊ฐ„(๋…„)์„ ์ถœ๋ ฅํ•œ๋‹ค.
๋งŒ์ผ ๋น™์‚ฐ์ด ๋‹ค ๋…น์„ ๋•Œ๊นŒ์ง€ ๋ถ„๋ฆฌ๋˜์ง€ ์•Š์œผ๋ฉด 0์„ ์ถœ๋ ฅํ•œ๋‹ค.
"""

# 1) BFS
from collections import deque

def bfs(x, y) :
    # ๋น™์‚ฐ ์œ„์น˜
    q = deque()
    q.append((x, y))
    # ์ฃผ๋ณ€์— ๋ฐ”๋‹ค๊ฐ€ ์žˆ๋Š” ๋น™์‚ฐ์„ (x, y, ๋ฐ”๋‹ค ๊ฐฏ์ˆ˜)
    visited[x][y] = 1
    seaList = []

    while q :
        x, y = q.popleft()
        sea = 0

        for i in range(4) :
            nx = x + dx[i]
            ny = y + dy[i]

            if 0 <= nx < N and 0 <= ny < M :
                if not graph[nx][ny] :
                    # ๋น™์‚ฐ ์ฃผ๋ณ€์˜ ๋ฐ”๋‹ค ๊ฐฏ์ˆ˜
                    sea += 1
                elif graph[nx][ny] and not visited[nx][ny] :
                    q.append((nx, ny))
                    visited[nx][ny] = 1

        if sea > 0 :
            seaList.append((x, y, sea))
    # ๋น™์‚ฐ ๋…น์ด๊ธฐ
    for x, y, sea in seaList :
        graph[x][y] = max(0, graph[x][y] - sea)

    # ํ•˜๋‚˜์˜ ๊ทธ๋ฃน์„ ํƒ์ƒ‰
    return 1


N, M = map(int, input().split())
graph = []

for i in range(N) :
    graph.append(list(map(int, input().split())))

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
year = 0
ice = []

for i in range(N) :
    for j in range(M) :
        if graph[i][j] :
            # ๋น™์‚ฐ์˜ ์œ„์น˜๋ฅผ (i, j)
            ice.append((i, j))

while ice :
    visited = [[0] * (M) for _ in range(N)]
    delList = []

    # bfs() ์—์„œ ๋ฐ›์•„์˜จ ๋น™์‚ฐ ๊ทธ๋ฃน์˜ ๊ฐœ์ˆ˜
    result = 0

    for i, j in ice :
        # ๋น™์‚ฐ ๊ฐœ์ˆ˜ ๋”ํ•˜๊ธฐ
        if graph[i][j] and not visited[i][j] :
            result += bfs(i, j)
        # ๋น™์‚ฐ์ด ์—†๋‹ค๋ฉด
        if graph[i][j] == 0 :
            delList.append((i, j))

    # ๋น™์‚ฐ ๊ทธ๋ฃน์ด 2๊ฐœ ์ด์ƒ์ด๋ฉด
    if result > 1 :
        print(year)
        break

    # ๋‹ค ๋…น์€ ๋น™์‚ฐ ์ œ๊ฑฐ
    ice = sorted(list(set(ice) - set(delList)))
    year += 1

if result < 2 :
    print(0)

# input
# 5 7
# 0 0 0 0 0 0 0
# 0 2 4 5 3 0 0
# 0 3 0 2 5 2 0
# 0 7 6 2 4 0 0
# 0 0 0 0 0 0 0

# output
# 2


3. ํ•ด์„ค

์‹œ๊ฐ„์ดˆ๊ณผ๋กœ ๊ณ ์ƒํ•œ ๋ฌธ์ œ..

bfs๋ฅผ ํ†ตํ•ด ์—ฐ๊ฒฐ๋œ ๋น™์‚ฐ์„ ์ฐพ๊ณ , ๋น™์‚ฐ์˜ ๊ฐœ์ˆ˜๋ฅผ ์„ผ๋‹ค.

์‹œ๊ฐ„์ดˆ๊ณผ ๋‚œ ๋ถ€๋ถ„์€ ๋น™์‚ฐ์ด ๋…น๋Š” ๊ฒƒ์„ ๋ฐ˜์˜ํ•  ๋•Œ ๋ฐœ์ƒํ–ˆ๊ณ , ํ•ด๋‹น ๋ถ€๋ถ„์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด bfs๋ฅผ ์‹คํ–‰ํ•  ๋•Œ ๋น™์‚ฐ์˜ ์œ„์น˜์™€ ๋…น๋Š” ์–‘์„ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•ด์ฃผ์—ˆ๋‹ค.

This post is licensed under CC BY 4.0 by the author.