DFS/BFS
๋ค ๋ฒ์งธ ๋ฌธ์ !
๋๋น๋ ์๊ณ ๋ฆฌ์ฆ ๊ฐ์๋ฅผ ๋ฃ๊ณ ํ์๋ค.
๊ฐ์์ ๋์จ ๋ฌธ์ ์ ํ๊ณผ ๋์ผํด์ ๊ฐ๋จํ๊ฒ ํ์๋ค.
def dfs(x, y):
if x < 0 or x >= N or y < 0 or y >= N:
return 0
if graph[x][y] == 1:
graph[x][y] = 0
return 1 + dfs(x-1, y) + dfs(x+1, y) + dfs(x, y-1) + dfs(x, y+1)
return 0
N = int(input())
graph, result = [], []
for _ in range(N):
graph.append(list(map(int, input())))
for i in range(N):
for j in range(N):
if graph[i][j] == 1:
result.append(dfs(i, j))
print(len(result))
for x in sorted(result):
print(x)
dfs
๋ฅผ ์ฌ๊ท๋ก ๊ตฌํํ์ฌ ํ์๋ค.
graph
ํํ๋ double-list
์ด๋ค.