o DFS 유형

n, m = map(int, input().split())

 

graph = []

for i in range(n):

 graph.append(list(map(int, input())))

 

res = 0

 

dx = [0, 0, -1, 1]

dy = [1, -1, 0, 0]

 

def dfs(x, y):

  if(x <= -1 or x >= n or y <= -1 or y >= m):

   return False

 

  ifgraph[x][y] == 0:

   graph[x][y] = 1

 

   for i in range(4):

     dfs(x+dx[i],y+dy[i])

   return True

 

 return False

 

for i in range(n):

 for j in range(m):

   if dfs(i, j) == True:

     res += 1

 

print(res)

 

예제) 4 5

00110

00011

11111

00000  

 #3

+ Recent posts