Skip to content

Commit d620195

Browse files
committed
[22.02.25] 2667 - 너비우선탐색 풀이
1 parent d1cdc36 commit d620195

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Diff for: Python_BOJ_2022/2667.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 단지번호붙이기
2+
n = int(input())
3+
graph = [list(map(int, input())) for _ in range(n)]
4+
visited = [[0] * n for _ in range(n)]
5+
dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]
6+
7+
cnt = 0
8+
ans = []
9+
10+
def recursive(x, y):
11+
global cnt
12+
visited[x][y] = 1
13+
if graph[x][y] == 1:
14+
cnt += 1
15+
for i in range(4):
16+
nx, ny = x + dx[i], y + dy[i]
17+
if 0 <= nx < n and 0 <= ny <n:
18+
if not visited[nx][ny] and graph[nx][ny] == 1:
19+
recursive(nx, ny)
20+
21+
22+
for i in range(n):
23+
for j in range(n):
24+
if graph[i][j] == 1 and not visited[i][j]:
25+
recursive(i, j)
26+
ans.append(cnt)
27+
cnt = 0
28+
29+
print(len(ans))
30+
ans.sort()
31+
for i in ans:
32+
print(i)

0 commit comments

Comments
 (0)