๋ฌธ์
๋ฐฑ์ค ์จ๋ผ์ธ ์ ์ง - 17086๋ฒ
ํ์ด ๊ณผ์
์๊ธฐ ์์ด์ ๋๋ฌํ๋ ๊ฐ์ฅ ๊ฐ๊น์ด ๊ฑฐ๋ฆฌ๋ฅผ ์์ ๊ฑฐ๋ฆฌ
๋ผ๊ณ ํํํฉ๋๋ค.
์ด ์์ ๊ฑฐ๋ฆฌ
์ค์์ ๊ฐ์ฅ ํฐ ๊ฐ์ ์ฐพ์์ผํ๊ธฐ ๋๋ฌธ์ ์์ด๊ฐ ์๋ ๊ฐ ์นธ๋ง๋ค BFS
๋ฅผ ์ํํด์ ์์ ๊ฑฐ๋ฆฌ
๋ฅผ ๊ณ์ฐํ๋ฉด ๋ฉ๋๋ค.
์ฝ๋
import sys
from collections import deque
N, M = list(map(int, sys.stdin.readline().split()))
board = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
dx = [0, 0, 1, -1, -1, -1, 1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
def bfs(x, y):
visit = [[0]*M for _ in range(N)]
q = deque()
visit[x][y] = 1
q.append([x, y, 0])
while q:
x, y, cost = q.popleft()
if board[x][y]:
return cost
for i in range(8):
nx, ny = x + dx[i], y + dy[i]
if 0 <= nx < N and 0 <= ny < M:
if not visit[nx][ny]:
visit[nx][ny] = 1
q.append([nx, ny, cost + 1])
def solution():
answer = 0
for x in range(N):
for y in range(M):
if not board[x][y]:
answer = max(answer, bfs(x, y))
return answer
print(solution())
๋ฐ์ํ
'๐ algorithm > boj' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ 2688 - ์ค์ด๋ค์ง ์์ (0) | 2021.03.14 |
---|---|
BOJ 16472 - ๊ณ ๋ฅ์ด (0) | 2021.03.14 |
BOJ 13335 - ํธ๋ญ (0) | 2021.03.08 |
BOJ 6497 - ์ ๋ ฅ๋ (0) | 2021.03.08 |
BOJ 6156 - Cow Contest (0) | 2021.03.08 |
๐ฌ ๋๊ธ