BOJ_4485_녹색 옷 입은 애가 젤다지? (Java)
[Gold IV] 녹색 옷 입은 애가 젤다지? - 4485 Add commentMore actions
성능 요약
메모리: 20540 KB, 시간: 192 ms
분류
그래프 이론, 그래프 탐색, 최단 경로, 데이크스트라, 격자 그래프
제출 일자
2025년 6월 29일 16:16:25
문제 설명
젤다의 전설 게임에서 화폐의 단위는 루피(rupee)다. 그런데 간혹 '도둑루피'라 불리는 검정색 루피도 존재하는데, 이걸 획득하면 오히려 소지한 루피가 감소하게 된다!
젤다의 전설 시리즈의 주인공, 링크는 지금 도둑루피만 가득한 N x N 크기의 동굴의 제일 왼쪽 위에 있다. [0][0]번 칸이기도 하다. 왜 이런 곳에 들어왔냐고 묻는다면 밖에서 사람들이 자꾸 "젤다의 전설에 나오는 녹색 애가 젤다지?"라고 물어봤기 때문이다. 링크가 녹색 옷을 입은 주인공이고 젤다는 그냥 잡혀있는 공주인데, 게임 타이틀에 젤다가 나와있다고 자꾸 사람들이 이렇게 착각하니까 정신병에 걸릴 위기에 놓인 것이다.
하여튼 젤다...아니 링크는 이 동굴의 반대편 출구, 제일 오른쪽 아래 칸인 [N-1][N-1]까지 이동해야 한다. 동굴의 각 칸마다 도둑루피가 있는데, 이 칸을 지나면 해당 도둑루피의 크기만큼 소지금을 잃게 된다. 링크는 잃는 금액을 최소로 하여 동굴 건너편까지 이동해야 하며, 한 번에 상하좌우 인접한 곳으로 1칸씩 이동할 수 있다.
링크가 잃을 수밖에 없는 최소 금액은 얼마일까?
입력
입력은 여러 개의 테스트 케이스로 이루어져 있다.
각 테스트 케이스의 첫째 줄에는 동굴의 크기를 나타내는 정수 N이 주어진다. (2 ≤ N ≤ 125) N = 0인 입력이 주어지면 전체 입력이 종료된다.
이어서 N개의 줄에 걸쳐 동굴의 각 칸에 있는 도둑루피의 크기가 공백으로 구분되어 차례대로 주어진다. 도둑루피의 크기가 k면 이 칸을 지나면 k루피를 잃는다는 뜻이다. 여기서 주어지는 모든 정수는 0 이상 9 이하인 한 자리 수다.
출력
각 테스트 케이스마다 한 줄에 걸쳐 정답을 형식에 맞춰서 출력한다. 형식은 예제 출력을 참고하시오.
문제 풀이
코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**Add commentMore actions
* Author: nowalex322, Kim HyeonJae
*/
import java.io.*;
import java.util.*;
public class Main {
class Cell implements Comparable<Cell> {
int r, c, cost;
public Cell(int r, int c, int cost) {
this.r = r;
this.c = c;
this.cost = cost;
}
@Override
public int compareTo(Cell o) {
return this.cost - o.cost;
}
}
static BufferedReader br;
static BufferedWriter bw;
static StringTokenizer st;
static int tc, N;
static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1};
static int[][] board, minDist;
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws Exception {
new Main().solution();
}
public void solution() throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream("src/main/java/BOJ_4485_녹색옷입은애가젤다지/input.txt")));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
while (true) {
tc++;
N = Integer.parseInt(br.readLine());
if (N == 0) break;
else {
int res = tour();
makeAns(res);
}
}
bw.write(sb.toString());
bw.flush();
bw.close();
br.close();
}
private int tour() throws IOException {
board = new int[N][N];
minDist = new int[N][N];
for (int i = 0; i < N; i++) {
Arrays.fill(minDist[i], Integer.MAX_VALUE);
}
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
board[i][j] = Integer.parseInt(st.nextToken());
}
}
int cnt = 0;
PriorityQueue<Cell> pq = new PriorityQueue<>();
pq.offer(new Cell(0, 0, board[0][0]));
minDist[0][0] = board[0][0];
while (!pq.isEmpty()) {
Cell curr = pq.poll();
if (curr.cost > minDist[curr.r][curr.c]) continue;
for (int k = 0; k < 4; k++) {
int nr = curr.r + dr[k];
int nc = curr.c + dc[k];
if (isValid(nr, nc) && curr.cost + board[nr][nc] < minDist[nr][nc]) {
minDist[nr][nc] = curr.cost + board[nr][nc];
pq.offer(new Cell(nr, nc, minDist[nr][nc]));
}
}
}
return minDist[N - 1][N - 1];
}
private boolean isValid(int r, int c) {
return r >= 0 && r < N && c >= 0 && c < N;
}
private void makeAns(int res) {
sb.append("Problem ").append(tc).append(": ").append(res).append("\n");
}
}Add comment