๋ฐ์ํ
๐ป ๋ฐฑ์ค 2667๋ฒ [๋จ์ง๋ฒํธ๋ถ์ด๊ธฐ]
ํ์ด)
n = int(input())
map_ = [list(map(int,input())) for _ in range(n)]
visited = [[0]*n for _ in range(n)]
cnt_lst = []
cnt = 0
def dfs(x,y) :
global cnt
# 1. ๋ฐฉ๋ฌธํ ๋
ธ๋์ธ์ง ํ์ธ
if not visited[x][y] :
# ๋ฐฉ๋ฌธ์ฒ๋ฆฌ
visited[x][y] = 1
if map_[x][y] == 0 : return # !!!!!!!!!!!!!!!!!!!!!!!!!!!์ด๊ฑฐ ํ์ค ์ํด์ ํ๋ ธ์์
# 2. ์ธ์ ๋
ธ๋ ์ํ
for [i,j] in [[x+1,y],[x,y+1],[x-1,y],[x,y-1]] :
if 0 <= i <= n-1 and 0 <= j <= n-1 :
if map_[i][j] == 1 and not visited[i][j]:
cnt += 1
dfs(i,j)
elif map_[i][j] == 0 :
visited[i][j] = 1
return
for i in range(n) :
for j in range(n) :
if not visited[i][j] :
if map_[i][j] == 1 : cnt = 1
else : cnt = 0
dfs(i,j)
if cnt != 0 : cnt_lst.append(cnt)
print(len(cnt_lst))
print(*sorted(cnt_lst),sep='\n')
๋ฉ๋ชจ๋ฆฌ | ์๊ฐ | ์ธ์ด | ์ฝ๋๊ธธ์ด |
29396KB | 68ms | Python 3 | 919B |
728x90
๋ฐ์ํ
'๋ฐฑ์ค & ํ๋ก๊ทธ๋๋จธ์ค > DFS & BFS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] DFS/BFS level2 ํ๊ฒ๋๋ฒ (0) | 2022.03.24 |
---|---|
[๋ฐฑ์ค 1012] [dfs/bfs] ์ ๊ธฐ๋ ๋ฐฐ์ถ (0) | 2021.04.29 |
[๋ฐฑ์ค 1012] [dfs/bfs] ์ ๊ธฐ๋ ๋ฐฐ์ถ (0) | 2021.04.24 |
[๋ฐฑ์ค 2178] [dfs/bfs] ๋ฏธ๋กํ์ (0) | 2021.04.23 |
[๋ฐฑ์ค 1260] [dfs/bfs] BFS์ DFS โ (0) | 2021.04.23 |
[๋ฐฑ์ค 2606] [dfs/bfs] ๋ฐ์ด๋ฌ์ค (0) | 2021.04.22 |
[๋ฐฑ์ค 2331] [dfs] ๋ฐ๋ณต์์ด (0) | 2021.04.22 |