728x90

 

파이썬

 

실1 토마토

 

https://www.acmicpc.net/problem/7576

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sys
from collections import deque
input = sys.stdin.readline
dx = [-1100]
dy = [00-11]
 
M, N = map(int, input().split())
box = []
for i in range(N):
    box.append(list(map(int, input().split())))
 
queue = deque()
for i in range(N):
    for j in range(M):
        if box[i][j] == 1:
            queue.append((i, j))
 
while queue:
    x, y = queue.popleft()
    for i in range(4):
        nx = x + dx[i]
        ny = y + dy[i]
 
        if nx >= N or ny >= M or nx < 0 or ny < 0 or box[nx][ny] == -1:
            continue
        if box[nx][ny] == 0:
            box[nx][ny] = box[x][y] + 1
            queue.append((nx, ny))
 
includeZero = False
for i in range(N):
    if 0 in box[i]:
        includeZero = True
        print(-1)
        break
            
if not includeZero:
    print(max(map(max, box))-1)
cs

 

728x90
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기