๋ฌธ์
https://www.acmicpc.net/problem/2636
ํ์ด
# ๋ฐฑ์ค 2636๋ฒ ๋ฌธ์ - ์น์ฆ
from collections import deque
dx, dy = [0,0,-1,1], [1,-1,0,0]
n, m = map(int, input().split())
graph = [list(map(int, input().split())) for _ in range(n)]
def bfs():
visited = [[0]*m for _ in range(n)]
q = deque()
q.append((0,0))
visited[0][0] = 1
cnt = 0
while q:
x, y = q.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < m and not visited[nx][ny]:
if graph[nx][ny] == 0:
visited[nx][ny] = 1
q.append((nx, ny))
elif graph[nx][ny] == 1:
graph[nx][ny] = 0
cnt += 1
visited[nx][ny] = 1
cheese.append(cnt)
return cnt
cheese = []
time = 0
while True:
time += 1
cnt = bfs()
if cnt == 0:
break
print(time-1)
print(cheese[-2])
1์ ์ฐพ์์ BFS๋ฅผ ํ๋ ๊ฒ์ด ์๋ 0์์ BFS๋ฅผ ์คํํด์ผ ํ๋ค.
๊ฑฐ๊พธ๋ก ์๊ฐํ๋ฉด ์ฌ์ด ๋ฌธ์ ์๋๋ฐ ๋ฌธ์ ๋ฅผ ์๋ชป ์ดํดํด์ ์ค๋ ๊ฑธ๋ ธ๋ค.
'๐๏ธ Algorithm > ๐ฉ ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฉ [๋ฐฑ์ค] [Python] [Gold4] 1806๋ฒ_๋ถ๋ถํฉ (0) | 2023.04.01 |
---|---|
๐ฉ [๋ฐฑ์ค] [Python] [Silver2] 11722๋ฒ_๊ฐ์ฅ ๊ธด ๊ฐ์ํ๋ ๋ถ๋ถ ์์ด (0) | 2023.03.31 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold4] 1744๋ฒ_์ ๋ฌถ๊ธฐ (0) | 2023.03.31 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold2] 1202๋ฒ_๋ณด์ ๋๋ (0) | 2023.03.30 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold4] 1715๋ฒ_์นด๋ ์ ๋ ฌํ๊ธฐ (0) | 2023.03.28 |