https://www.acmicpc.net/problem/7576
7576번: 토마토
첫 줄에는 상자의 크기를 나타내는 두 정수 M,N이 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M,N ≤ 1,000 이다. 둘째 줄부터는 하나의 상자에 저장된 토마토�
www.acmicpc.net
from collections import deque
import sys
def solution(start_list):
queue = deque(start_list)
cnt = len(queue)
flag = 0
day = -1
drow = [0, 1, 0, -1]
dcol = [-1, 0, 1, 0]
while queue:
flag += 1
pos = queue.popleft()
for k in range(4):
crow = pos[0]+drow[k]
ccol = pos[1]+dcol[k]
if(0 <= crow and crow < N and 0 <= ccol and ccol < M):
if(matrix[crow][ccol] == 0):
matrix[crow][ccol] = 1
queue.append((crow, ccol))
if(flag == cnt):
flag = 0
cnt = len(queue)
day += 1
for i in range(N):
for j in range(M):
if(matrix[i][j] == 0):
day = -1
return day
M, N = map(int, sys.stdin.readline().rstrip().split())
matrix = [list(map(int, sys.stdin.readline().rstrip().split())) for _ in range(N)]
start = []
for i in range(N):
for j in range(M):
if(matrix[i][j] == 1):
start.append((i, j))
print(solution(start))
'PS > Python' 카테고리의 다른 글
2020 카카오 인턴십_수식 최대화(파이썬, Python) (0) | 2021.02.14 |
---|---|
[백준] 1012 - 유기농 배추(파이썬, Python) (0) | 2020.07.29 |
[백준] 2667 - 단지번호붙이기 (파이썬, Python) (0) | 2020.07.27 |
[백준] 2178 - 미로 탐색 (파이썬, Python) (0) | 2020.07.23 |
[백준] 1260 - DFS와 BFS (파이썬, Python) (0) | 2020.07.21 |