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 = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
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
'Python' 카테고리의 다른 글
| 211213 백준 코딩연습 파이썬 2667 단지번호붙이기 (0) | 2021.12.13 |
|---|---|
| 211210 백준 코딩연습 파이썬 2606 바이러스 (0) | 2021.12.10 |
| 211209 백준 코딩연습 파이썬 10451 순열 사이클 (0) | 2021.12.10 |
| 211208 백준 코딩연습 파이썬 2750 수 정렬하기 (0) | 2021.12.08 |
| 211207 파이썬 코딩테스트 수업 5일차, 백준 코딩연습 파이썬 2331 반복수열, 컴프리헨션 (0) | 2021.12.07 |





최근댓글