728x90
반응형
N=int(input())
map=[list(map(int,input()))for i in range(N)]
ans=[]
def dfs(x,y):
if x<=-1 or x>=N or y<=-1 or y>=N:
return False
if map[x][y]==1:
map[x][y]=2
dfs(x-1,y)
dfs(x,y-1)
dfs(x+1,y)
dfs(x,y+1)
return True
return False
prev=0
cnt=0
for i in range(N):
for j in range(N):
if dfs(i,j)==1:
now=0
cnt+=1
for k in range(N):
now+=map[k].count(2)
now-=prev
ans.append(now)
prev+=now
ans.sort()
print(len(ans))
for i in ans:
print(i)
참고를 좀 많이 했다 dfs까지는 괜찮아도 단지 개수를 세는 데에서 조금 생각해내기 어려웠다 dfs뭉텅이, 갯수 세기 뭉텅이 둘로 나눠서 푸는게 편할거같다 아파트를 2로 생각해서 리스트에 따로 갯수를 추가해주는게 아이디어
반응형
'Algorithm > BOJ' 카테고리의 다른 글
백준 4796번 캠핑(파이썬) (0) | 2021.07.11 |
---|