Skip to content

Commit 3b3f8a0

Browse files
committed
[BOJ]#4485.녹색 옷 입은 애가 젤다지?/골드4/실패
https://www.acmicpc.net/problem/4484
1 parent 2502f76 commit 3b3f8a0

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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))

0 commit comments

Comments
 (0)