Skip to content

Commit 7784513

Browse files
authored
[week4] sumin
1 parent 6b02451 commit 7784513

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

week4/sumin/week4_sumin.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
### 백준 2573 빙산 문제 정리
2+
3+
📌 BFS <br>
4+
📌 녹은 빙산을 바로 반영하면 주변 빙산에 영향이 가기 때문에 따로 리스트에 저장한 후, 모든 빙산의 탐색이 끝났을 때 한꺼번에 빙산을 녹인다.
5+
6+
7+
8+
```python
9+
import collections
10+
11+
n, m = map(int, input().split())
12+
graph = [list(map(int,input().split())) for _ in range(n)]
13+
dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]
14+
queue = collections.deque()
15+
day = 0
16+
check = False
17+
18+
def bfs(a,b):
19+
queue.append((a,b))
20+
while queue:
21+
x,y = queue.popleft()
22+
visited[x][y] = True
23+
for i in range(4):
24+
nx = x + dx[i]
25+
ny = y + dy[i]
26+
if 0 <= nx < n and 0 <= ny < m:
27+
if graph[nx][ny] != 0 and visited[nx][ny] == False:
28+
visited[nx][ny] = True
29+
queue.append((nx,ny))
30+
elif graph[nx][ny] == 0:
31+
count[x][y] += 1
32+
return 1
33+
34+
# 빙산이 분리될때까지 돌아줌
35+
while True:
36+
visited = [[False]*m for _ in range(n)]
37+
count = [[0]*m for _ in range(n)]
38+
result = []
39+
for i in range(n):
40+
for j in range(m):
41+
if graph[i][j] != 0 and visited[i][j] == False:
42+
result.append(bfs(i,j))
43+
# 빙산을 깍아줌
44+
for i in range(n):
45+
for j in range(m):
46+
graph[i][j] -= count[i][j]
47+
if graph[i][j] < 0:
48+
graph[i][j] = 0
49+
50+
if len(result) == 0: # 빙산이 다없어질때까지 분리가 되지않으면 break
51+
break
52+
if len(result) >= 2: # 빙산이 분리되면 break
53+
check = True
54+
break
55+
day += 1
56+
57+
if check:
58+
print(day)
59+
else:
60+
print(0)
61+
```
62+
63+
<br>
64+
65+
`bsf(x, y)`
66+
67+
빙산의 위치(x, y)를 기준으로 주변 바다의 개수를 구한다.
68+
> 빙산을 바로 녹이지 않고, 연결된 빙산을 모두 탐색한 후에 빙산을 녹여야 한다!
69+
>> 빙산을 바로 녹이면 연결된 다음 빙산이 현재 위치의 빙산을 바다로 인식하기 때문

0 commit comments

Comments
 (0)