File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed
Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ [BOJ]_4485 녹색 옷 입은 애가 젤다지?_Gold4
3+ https://solved.ac/problems/tags/dijkstra?sort=solved&direction=desc&page=1
4+ #유형 : Dijkstra
5+ #제출날짜 : 01.27.2025
6+
7+ https://velog.io/@victoriapasta/BOJ-4485-%EB%85%B9%EC%83%89-%EC%98%B7-%EC%9E%85%EC%9D%80-%EC%95%A0%EA%B0%80-%EC%A0%A4%EB%8B%A4%EC%A7%80
8+ """
9+ """
10+
11+
12+ """
13+ import sys
14+ import heapq
15+
16+ input = sys .stdin .readline
17+
18+ n = int (input ())
19+ dx = [0 , 1 , - 1 , 0 ]
20+ dy = [1 , 0 , 0 , - 1 ]
21+ # 정답 출력을 위한 cnt 변수
22+ cnt = 0
23+ # n이 0이면 종료
24+ while n != 0 :
25+ cnt += 1
26+ # 입력
27+ board = [list (map (int , input ().split ())) for _ in range (n )]
28+ # 최소 비용으로 정렬해 줄 heap queue (이것을 기준으로 탐색)
29+ heap = []
30+ # 가중치를 저장해줄 distance 테이블
31+ dist = [[1e9 ] * n for _ in range (n )]
32+ # [0][0]부터 시작
33+ dist [0 ][0 ] = board [0 ][0 ]
34+ heapq .heappush (heap , (board [0 ][0 ], 0 , 0 ))
35+
36+ while heap :
37+ distance , y , x = heapq .heappop (heap )
38+
39+ #최소 가중치를 먼저 가기 때문에 [N-1][N-1]에 도착하면 바로 리턴
40+ if y == n - 1 and x == n - 1 :
41+ print ("Problem" , str (cnt )+ ":" , distance )
42+ # n 변수 다시 받고 break
43+ n = int (input ())
44+ break
45+ # 상하좌우 방향 탐색
46+ for i in range (4 ):
47+ ny = y + dy [i ]
48+ nx = x + dx [i ]
49+
50+ if 0 <= ny < n and 0 <= nx < n :
51+ cost = distance + board [ny ][nx ]
52+ # 이미 저장된 가중치보다 현재 가중치가 낮으면 업데이트
53+ if dist [ny ][nx ] > cost :
54+ dist [ny ][nx ] = distance + board [ny ][nx ]
55+ heapq .heappush (heap , (distance + board [ny ][nx ], ny , nx ))
You can’t perform that action at this time.
0 commit comments