몇일전에 모양만 따놨는데 안되서 고통받다가..
어떻게 풀었는데 시간초과 떠서
고통받다가...
필요없는 프로세스 싹다 지우고 코드 간략화해서 성공...
ㅠㅠ 점점 어려워진다.. 아닌가.. 아니 뭔가 알꺼같은데 어렵다.. 근데
막상 풀고 나면 쉽다..
# 7576번
from collections import deque
import sys
x, y = map(int, input().split())
direction = [[-1, 0], [1, 0], [0, 1], [0, -1]]
mapData = [[0] * x for _ in range(y)]
def bfs(tQ):
counter = -1
while tQ:
counter += 1
for _ in range(len(tQ)):
tx, ty = tQ.popleft()
for i in range(len(direction)):
nx = tx + direction[i][0]
ny = ty + direction[i][1]
if 0 <= nx < y and 0<= ny < x:
if mapData[nx][ny] == 0:
mapData[nx][ny] = 1
tQ.append([nx, ny])
return counter
tempQue = deque()
for i in range(y):
temp = sys.stdin.readline().split()
for j in range(x):
tempVal = int(temp[j])
mapData[i][j] = tempVal
if tempVal == 1:
tempQue.append([i, j])
result = bfs(tempQue)
for i in range(y):
if mapData[i].count(0) >= 1:
result = -1
break
print(result)
'Algorithm > BFS, DFS' 카테고리의 다른 글
[백준] 1967번 숨바꼭질 (0) | 2020.09.15 |
---|---|
[백준] 4963번 섬의 개수 (0) | 2020.09.14 |
[ 백준] 2606번 바이러스 (0) | 2020.09.09 |
[백준] 1012번 유기농 배추 (0) | 2020.09.09 |
[백준] 2644번 촌수계산 (0) | 2020.09.08 |
댓글