[๋ฐฑ์ค-2667] ๋จ์ง๋ฒํธ๋ถ์ด๊ธฐ / Python
๐ Problem Solving/Baekjoon
2667๋ฒ: ๋จ์ง๋ฒํธ๋ถ์ด๊ธฐ
<๊ทธ๋ฆผ 1>๊ณผ ๊ฐ์ด ์ ์ฌ๊ฐํ ๋ชจ์์ ์ง๋๊ฐ ์๋ค. 1์ ์ง์ด ์๋ ๊ณณ์, 0์ ์ง์ด ์๋ ๊ณณ์ ๋ํ๋ธ๋ค. ์ฒ ์๋ ์ด ์ง๋๋ฅผ ๊ฐ์ง๊ณ ์ฐ๊ฒฐ๋ ์ง์ ๋ชจ์์ธ ๋จ์ง๋ฅผ ์ ์ํ๊ณ , ๋จ์ง์ ๋ฒํธ๋ฅผ ๋ถ์ด๋ ค ํ๋ค. ์ฌ
www.acmicpc.net
from collections import deque
n = int(input())
graph = []
for _ in range(n):
graph.append(list(map(int, input())))
answer = []
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
def bfs(x, y):
queue = deque()
queue.append((x, y))
graph[x][y] = 0
cnt = 0
while queue:
x, y = queue.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if nx < 0 or ny < 0 or nx >= n or ny >= len(graph[0]):
continue
if graph[nx][ny] == 0:
continue
if graph[nx][ny] == 1:
graph[nx][ny] = 0
cnt += 1
queue.append((nx, ny))
answer.append(cnt + 1)
total = 0
for i in range(n):
for j in range(len(graph[0])):
if graph[i][j] == 1:
total += 1
bfs(i, j)
print(total)
answer.sort()
for cnt in answer:
print(cnt)
ํด์ค
๋น๊ต์ ์ฌ์ด BFS ๋ฌธ์ ์๋ค. ํ์ํ๋ฉด์ ๊ฐ์ ์ธ์ด์ฃผ๊ธฐ!
'๐ Problem Solving > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค-6064] ์นด์ ๋ฌ๋ ฅ / Python (0) | 2021.05.27 |
---|---|
[๋ฐฑ์ค-5525] IOIOI / Python (0) | 2021.05.26 |
[๋ฐฑ์ค-2630] ์์ข ์ด ๋ง๋ค๊ธฐ / Python (0) | 2021.05.26 |
[๋ฐฑ์ค-1992] ์ฟผ๋ํธ๋ฆฌ / Python (0) | 2021.05.25 |
[๋ฐฑ์ค-1927] ์ต์ ํ / Python (0) | 2021.05.25 |