[BOJ] 17836

seonhyeongpak·2022년 1월 2일
0

python

목록 보기
5/7
import sys
from collections import deque
sys.stdin.readline
sys.setrecursionlimit(10**6)


n,m,t=map(int,input().split())
board=[list(map(int,input().split()))for _ in range(n)]
check= [[0 for i in range(m)] for j in range(n)]
dx=[1,-1,0,0]
dy=[0,0,-1,1]
dis=987654321

def bfs():
    global dis
    queue=deque()
    queue.append((0,0))
    check[0][0]=1
    while(queue):
        x,y=queue.popleft()
        if(board[x][y]==2):
            dis=(n-1-x)+(m-1-y)+check[x][y]-1
        if(x==n-1 and y==m-1):
            return min(check[x][y]-1,dis)
        for i in range(4):
            nx=x+dx[i]
            ny=y+dy[i]
            if(0<=nx<n and 0<=ny<m):
                if not check[nx][ny]:
                    if(board[nx][ny]!=1):
                        check[nx][ny]=check[x][y]+1
                        queue.append((nx,ny))

    return dis

result=bfs()
if(result>t):
    print("Fail")
else:
    print(result)
profile
모든 생각을 구현해내기 위해 노력하는 개발자

0개의 댓글