✅ 정답 코드
# 유기농 배추
from sys import stdin
from collections import deque
input = stdin.readline
def get_position():
for i in range(N):
for j in range(M):
if ground[i][j] == 1:
return (i, j)
return (-1, -1)
def bfs(value):
global ground
start = get_position()
queue = deque([start])
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
while queue:
x, y = queue.popleft()
ground[x][y] = value
for dx, dy in directions:
nx, ny = x + dx, y + dy
if 0 <= nx < N and 0 <= ny < M and ground[nx][ny] == 1:
ground[nx][ny] = value
queue.append((nx, ny))
if get_position() != (-1, -1):
bfs(value + 1)
T = int(input())
for _ in range(T):
M, N, B = map(int, input().split())
ground = [[0] * M for _ in range(N)]
for _ in range(B):
x, y = map(int, input().split())
ground[y][x] = 1
bfs(2)
max_value = -1
for g in ground:
max_value = max(max_value, max(g))
print(max_value - 1)
728x90
'Algorithm&CodingTest > Baekjoon' 카테고리의 다른 글
[Baekjoon][1010] 실버5 다리 놓기 - Python (0) | 2024.10.25 |
---|---|
[Beakjoon][2110] 골드4 공유기 설치 - Python (1) | 2024.10.09 |
[Baekjoon][14499] 골드4 주사위 굴리기 - Python (0) | 2024.10.08 |
[Beakjoon][14502] 골드4 연구소 - Python (0) | 2024.10.08 |
[Baekjoon][2206] 골드3 벽 부수고 이동하기 - Python (0) | 2024.10.08 |