7576: 토마토

ewillwin·2023년 5월 13일
0

Problem Solving (BOJ)

목록 보기
51/230

  • 최소 날짜 -> bfs
  • Map에 1이 두 개 이상인 경우가 있으므로, 먼저 first_one list에 1의 좌표를 넣어줌
    +) 후에 deque에도 할당
  • Map[nx][ny]가 0인 경우에 Map[x][y] + 1의 값을 할당해줌
  • bfs() 종료 후, 0이 있다면 print(-1) 해주고 프로세스 종료
  • Map의 원소 중 최댓값 - 1이 구하고자 하는 최소 날짜임
import sys
from collections import deque

dx = [0, 0, -1, 1]
dy = [-1, 1, 0, 0]

def bfs():
    queue = deque([])
    for _ in range(len(first_one)):
        queue.append(first_one[_])

    while queue:
        x, y = map(int, 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 >= M:
                continue
            if Map[nx][ny] == 0:
                Map[nx][ny] = Map[x][y] + 1
                queue.append([nx, ny])

M, N = map(int, sys.stdin.readline()[:-1].split(' '))
Map = []
for _ in range(N):
    tmp = list(map(int, sys.stdin.readline()[:-1].split(' ')))
    Map.append(tmp)

first_one = []
for x in range(N):
    for y in range(M): #1에서부터 동시에 bfs를 시작해야함
        if Map[x][y] == 1:
            first_one.append([x, y])

bfs(); date = 0
for i in range(N):
    for j in range(M):
        if Map[i][j] == 0:
            print(-1); exit()
    date = max(max(Map[i]), date)
print(date-1)
profile
Software Engineer @ LG Electronics

0개의 댓글