💡문제접근
- 문제를 잘못 읽어서 가로, 세로의 값을 받아오는 과정에서
IndexError
를 계속 범했다.
(문제 좀 잘 읽자..)
💡코드(메모리 : 98536KB, 시간 : 1468ms)
from collections import deque
import sys
input = sys.stdin.readline
M, N = map(int, input().strip().split())
tomato = [list(map(int, input().strip().split())) for _ in range(N)]
queue = deque()
day = 0
def BFS():
while queue:
x, y = queue.popleft()
dx = [0, 1, 0, -1]
dy = [-1, 0, 1, 0]
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < N and 0 <= ny < M and tomato[nx][ny] == 0:
queue.append((nx, ny))
tomato[nx][ny] = tomato[x][y] + 1
return tomato
for i in range(N):
for j in range(M):
if tomato[i][j] == 1:
queue.append((i, j))
BFS()
for i in tomato:
for j in i:
if j == 0:
print(-1)
sys.exit(0)
day = max(day, max(i))
print(day-1)
💡소요시간 : 40m