๐๏ธ Algorithm/๐ฉ ๋ฐฑ์ค
๐ฉ [๋ฐฑ์ค] [Python] [Gold5] 13219๋ฒ_Trains
Dbswnstjd
2023. 5. 14. 22:37
๋ฌธ์
https://www.acmicpc.net/short/status/13219/1
13219๋ฒ ์์ฝ๋ฉ - 1 ํ์ด์ง
๋ชจ๋ ์ธ์ดC++JavaPythonCRustC++17Java 8Python 3C11PyPy3C99C++98C++11C++14Java 8 (OpenJDK)Java 11C++20RubyKotlin (JVM)SwiftTextC#node.jsGoGo (gccgo)Java 15DD (LDC)PHPRust 2015Rust 2018Rust 2021PascalScalaLuaPerlF#Visual BasicObjective-CObjective-C++C99 (
www.acmicpc.net
ํ์ด
# ๋ฐฑ์ค 13219๋ฒ ๋ฌธ์ - trains
from heapq import heappush, heappop
import sys
input = sys.stdin.readline
INF = 1e9
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
n = int(input())
ay, ax, by, bx = map(int, input().split())
ax -= 1
ay -= 1
bx -= 1
by -= 1
graph = [list(map(int, input().split())) for _ in range(n)]
visited = [[INF for _ in range(n)] for _ in range(n)]
q = []
heappush(q, (graph[ax][ay], ax, ay))
visited[ax][ay] = graph[ax][ay]
while q:
cost, x, y = heappop(q)
if graph[x][y] == -1:
break
if cost > visited[x][y]:
continue
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < n:
if graph[nx][ny] != -1:
next_cost = cost + graph[nx][ny]
if visited[nx][ny] > next_cost:
visited[nx][ny] = next_cost
heappush(q, (next_cost, nx, ny))
if visited[bx][by] == INF:
print(-1)
else:
print(visited[bx][by])
๋ค์ต์คํธ๋ผ์ BFS๋ฅผ ์ด์ฉํ ๋ฌธ์