Last active
October 16, 2019 06:20
-
-
Save sehajyang/d9f4e44578cbba74002324875da82866 to your computer and use it in GitHub Desktop.
단지번호붙이기 : https://www.acmicpc.net/problem/2667
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dfs(i, j, danji_num): | |
# 단지 0 아닐때마다 dict 카운트 올림 | |
if danji_num in danji_dict: | |
danji_dict[danji_num] += 1 | |
else: | |
danji_dict[danji_num] = 1 | |
danji_list[i][j] = 0 | |
visited[i][j] = True | |
for k in range(4): | |
ix, iy = i + x[k], j + y[k] | |
if -1 < ix < danji_size and -1 < iy < danji_size: | |
if danji_list[ix][iy] > 0 and not visited[ix][iy]: | |
dfs(ix, iy, danji_num) | |
if __name__ == '__main__': | |
danji_size = int(input()) | |
danji_list = [[int(i) for i in input()] for _ in range(danji_size)] | |
visited = [[False for _ in range(danji_size)] for _1 in range(danji_size)] | |
x = [0, 1, -1, 0] | |
y = [1, 0, 0, -1] | |
danji_dict = {} | |
danji_num = 0 | |
for i in range(danji_size): | |
for j in range(danji_size): | |
if danji_list[i][j] > 0 and not visited[i][j]: | |
dfs(i, j, danji_num) | |
danji_num += 1 | |
print(len(danji_dict.keys())) | |
[print(i) for i in sorted(danji_dict.values())] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment