n = int(input())
graph = [list(map(int, input())) for _ inrange(n)]
# dfs 정의defdfs(x, y, cnt):if x < 0or x >= n or y < 0or y >= n:
return cnt
if graph[x][y] == 1:
graph[x][y] = -1
cnt += 1
cnt = dfs(x - 1, y, cnt)
cnt = dfs(x + 1, y, cnt)
cnt = dfs(x, y - 1, cnt)
cnt = dfs(x, y + 1, cnt)
return cnt
return cnt
h_cnt = 0# 단지 개수
h_list = [] # 각 단지의 집의 개수for i inrange(n):
for j inrange(n):
cnt = dfs(i, j, 0)
if cnt:
h_cnt += 1# 단지 수 세기
h_list.append(cnt) # 단지 내 집의 개수 저장print(h_cnt)
h_list.sort() # 각 단지의 집 개수를 오름차순으로 정렬for i in h_list:
print(i)
내 풀이 2 - cnt를 전역변수로 사용
n = int(input())
graph = [list(map(int, input())) for _ inrange(n)]
# dfs 정의defdfs(x, y):global cnt
if x < 0or x >= n or y < 0or y >= n:
returnFalseif graph[x][y] == 1:
graph[x][y] = -1
cnt += 1
dfs(x - 1, y)
dfs(x + 1, y)
dfs(x, y - 1)
dfs(x, y + 1)
returnTruereturnFalse
h_cnt = 0# 단지 개수
h_list = [] # 각 단지의 집의 개수for i inrange(n):
for j inrange(n):
cnt = 0if dfs(i, j):
h_cnt += 1# 단지 수 세기
h_list.append(cnt) # 단지 내 집의 개수 저장print(h_cnt)
h_list.sort() # 각 단지의 집 개수를 오름차순으로 정렬for i in h_list:
print(i)