간단한 bfs문제
import sys
input = sys.stdin.readline
from collections import deque
def bfs(i,j):
q = deque()
q.append((i,j))
visited[i][j] = 1
while q:
x, y = q.popleft()
for d in delta:
nx, ny = x + d[0], y + d[1]
if 0 <= nx < n and 0 <= ny < m and not visited[nx][ny] and not island[nx][ny]=='1':
visited[nx][ny] = visited[x][y] + 1
q.append((nx,ny))
if island[nx][ny] in goal:
print('TAK')
print(visited[x][y])
exit()
print('NIE')
exit()
n, m = map(int, input().split())
island = [list(input().rstrip()) for _ in range(n)]
visited = [[0]*m for _ in range(n)]
delta = ((1,0),(0,1),(-1,0),(0,-1))
goal = {'3', '4', '5'}
for i in range(n):
for j in range(m):
if island[i][j]=='2':
bfs(i,j)
'practivceAlgorithm > 백준' 카테고리의 다른 글
[백준][Python] 16938 캠프준비 (0) | 2021.09.20 |
---|---|
[백준][Python] 1918 후위표기식 (0) | 2021.09.19 |
[백준][Python] 21318 피아노체조 (0) | 2021.09.19 |
[백준][Python] 17266 어두운 굴다리 (0) | 2021.09.19 |
[백준][Python] 15683 감시 (0) | 2021.09.19 |