Skip to content

Commit d900b07

Browse files
committed
[BOJ] 녹색 옷 입은 애가 젤다지? / 골드4 / 60분 힌트
https://www.acmicpc.net/problem/4485
1 parent 7da91b8 commit d900b07

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import sys
2+
input = sys.stdin.readline
3+
import heapq
4+
dx = [-1, 0 ,1 ,0]
5+
dy = [0, 1, 0, -1]
6+
inf = int(1e9)
7+
8+
def dijkstra(t, graph, dist):
9+
que = []
10+
dist[0][0] = graph[0][0]
11+
12+
heapq.heappush(que, (dist[0][0], 0, 0))
13+
14+
while que:
15+
cost, x, y = heapq.heappop(que)
16+
17+
if dist[x][y] < cost:
18+
continue
19+
20+
for i in range(4):
21+
nx = dx[i] + x
22+
ny = dy[i] + y
23+
if n > nx >= 0 and n > ny >= 0 :
24+
temp = cost + graph[nx][ny]
25+
if temp < dist[nx][ny]:
26+
dist[nx][ny] = temp
27+
heapq.heappush(que, (temp, nx, ny))
28+
29+
print('Problem {}: {}'.format(t, dist[n - 1][n - 1]))
30+
31+
32+
t = 1
33+
34+
while 1:
35+
n = int(input())
36+
graph = []
37+
if not n:
38+
break
39+
40+
for i in range(n):
41+
graph.append(list(map(int, input().split())))
42+
43+
dist = [[inf for i in range(n)] for j in range(n)]
44+
dijkstra(t, graph, dist)
45+
46+
t += 1

0 commit comments

Comments
 (0)