-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5-10.py
More file actions
55 lines (40 loc) · 1.01 KB
/
5-10.py
File metadata and controls
55 lines (40 loc) · 1.01 KB
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
#CH5 BFS&DFS
#예제 5-10
# 음료수 얼려 먹기
# N*M
# 구멍 뚫려 있는 부분 0 , 칸막이가 존재하는 부분은 1
# 구멍 뚫려 있는 부분 끼리 상,하,좌,우로 붙어 있는 경우 서로 연결된걸로 간주
#생성되는 총 아이스크림의 개수를 구하는 프로그램
#입력
N,M = map(int,input().split())
graph = [[] for _ in range(N)]
#방문값 초기화
visited = [[False]*M for _ in range(N)]
sum = 0
#그래프 입력
for i in range(N):
temp = list(map(int,input()))
graph[i]=temp
#dfs
def dfs(row,col):
cnt=0
#예외 처리
if row<0 or col<0 or row>=N or col>=M:
pass
#뚜껑 or 방문함
elif graph[row][col]==1 or visited[row][col]:
pass
#방문 가즈아
else:
visited[row][col]=True
cnt=1
dfs(row-1,col)
dfs(row,col-1)
dfs(row+1,col)
dfs(row,col+1)
return cnt
#탐색
for i in range(N):
for j in range(M):
sum+=dfs(i,j)
print(sum)