Skip to content

Commit

Permalink
Create 토마토 kyuhyun.py
Browse files Browse the repository at this point in the history
  • Loading branch information
lalabulla authored Oct 8, 2023
1 parent dee0c4c commit da6c3f2
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Baekjoon - 문제풀이/토마토/kyuhyun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from collections import deque
m, n = map(int, input().split(" "))

tomato = []
q = deque()
flag = 0

for _ in range(n) :
tmp = list(map(int, input().split(" ")))
tomato.append(tmp)

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

def bfs():
global res
while q:
time, y, x = q.popleft()
res = time

for i in range(4):
nx = x + dx[i]
ny = y + dy[i]

if nx < 0 or nx >= m or ny < 0 or ny >= n:
continue
if tomato[ny][nx] == 1 or tomato[ny][nx] == -1:
continue
q.append([time + 1, ny, nx])
tomato[ny][nx] = 1

for i in range(n):
for j in range(m):
if tomato[i][j] == 1:
q.append([0, i, j])
bfs()
for i in range(n):
if 0 in tomato[i]:
flag = 1
break

if flag == 1:
print(-1)
else:
print(res)

0 comments on commit da6c3f2

Please sign in to comment.