- Notifications
You must be signed in to change notification settings - Fork 846
/
Copy path10.py
34 lines (30 loc) · 1 KB
/
10.py
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
# N, M을 공백을 기준으로 구분하여 입력 받기
n, m=map(int, input().split())
# 2차원 리스트의 맵 정보 입력 받기
graph= []
foriinrange(n):
graph.append(list(map(int, input())))
# DFS로 특정한 노드를 방문한 뒤에 연결된 모든 노드들도 방문
defdfs(x, y):
# 주어진 범위를 벗어나는 경우에는 즉시 종료
ifx<=-1orx>=nory<=-1ory>=m:
returnFalse
# 현재 노드를 아직 방문하지 않았다면
ifgraph[x][y] ==0:
# 해당 노드 방문 처리
graph[x][y] =1
# 상, 하, 좌, 우의 위치들도 모두 재귀적으로 호출
dfs(x-1, y)
dfs(x, y-1)
dfs(x+1, y)
dfs(x, y+1)
returnTrue
returnFalse
# 모든 노드(위치)에 대하여 음료수 채우기
result=0
foriinrange(n):
forjinrange(m):
# 현재 위치에서 DFS 수행
ifdfs(i, j) ==True:
result+=1
print(result) # 정답 출력