반응형
https://www.acmicpc.net/problem/1261
import heapq
y_lim, x_lim = map(int, input().split())
graph = []
visit = []
for i in range(x_lim):
graph.append([x for x in input()])
visit.append([0]*y_lim)
dx = [-1,1,0,0]
dy = [0,0,-1,1]
q = []
heapq.heappush(q, [0,0,0]) # crush,x,y
while q:
crush, x, y = heapq.heappop(q)
if x == x_lim-1 and y == y_lim-1: # finish
print(crush)
break
if visit[x][y]==0:
visit[x][y]=1
for i in range(4):
nx = x+dx[i]
ny = y+dy[i]
if 0<=nx<x_lim and 0<=ny<y_lim and visit[nx][ny]==0:
if graph[nx][ny]=='1':
heapq.heappush(q, [crush+1,nx,ny])
else:
heapq.heappush(q, [crush,nx,ny])
지도 탐색(BFS) + 최소 벽 깨기(우선순위 큐) => 다익스트라!!
아직은 최단경로 문제가 익숙하지 않아서 다익스트라에 익숙해지도록 해야겠다.
댓글