Skip to content

Commit d6e7532

Browse files
authored
Update 10.py
1 parent 22ce6e6 commit d6e7532

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

5/10.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
11
# N, M을 공백을 기준으로 구분하여 입력 받기
22
n, m = map(int, input().split())
3+
34
# 2차원 리스트의 맵 정보 입력 받기
4-
array = []
5+
graph = []
56
for i in range(n):
6-
array.append(list(map(int, input())))
7+
graph.append(list(map(int, input())))
78

89
# DFS로 특정한 노드를 방문한 뒤에 연결된 모든 노드들도 방문
910
def dfs(x, y):
1011
# 주어진 범위를 벗어나는 경우에는 즉시 종료
1112
if x <= -1 or x >= n or y <= -1 or y >= m:
1213
return 0
13-
if array[x][y] == 0:
14+
# 현재 노드를 아직 방문하지 않았다면
15+
if graph[x][y] == 0:
1416
# 해당 노드 방문 처리
15-
array[x][y] = 1
16-
# 상, 하, 좌, 우의 위치들도 모두 방문 처리
17+
graph[x][y] = 1
18+
# 상, 하, 좌, 우의 위치들도 모두 재귀적으로 호출
1719
dfs(x - 1, y)
1820
dfs(x, y - 1)
1921
dfs(x + 1, y)
2022
dfs(x, y + 1)
2123
return 1
2224
return 0
2325

24-
# 모든 노드에 대하여 음료수 채우기
26+
# 모든 노드(위치)에 대하여 음료수 채우기
2527
result = 0
2628
for i in range(n):
2729
for j in range(m):
30+
# 현재 위치에서 DFS 수행
2831
if dfs(i, j) == 1:
2932
result += 1
3033

0 commit comments

Comments
 (0)