공유메모장
[백준] 4485번 - 녹색 옷 입은 애가 젤다지? JAVA 풀이 본문
문제
https://www.acmicpc.net/problem/4485
4485번: 녹색 옷 입은 애가 젤다지?
젤다의 전설 게임에서 화폐의 단위는 루피(rupee)다. 그런데 간혹 '도둑루피'라 불리는 검정색 루피도 존재하는데, 이걸 획득하면 오히려 소지한 루피가 감소하게 된다! 젤다의 전설 시리즈의 주
www.acmicpc.net
문제 유형
다익스트라(dijkstra)
문제 풀이
추가예정
전체코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.PriorityQueue;
class Node implements Comparable<Node>{
int x, y, distance;
public Node(int y, int x, int distance){
this.x= x;
this.y= y;
this.distance = distance;
}
@Override
public int compareTo(Node other) {
if (this.distance < other.distance) {
return -1;
}
return 1;
}
}
public class Main {
static int N;
static int[][] map;
static int[][] d;
static int[] dx = {1,-1,0,0};
static int[] dy = {0,0,1,-1};
static final int INF = 1000000000;
static int count = 0;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while(true){
count++;
N = Integer.parseInt(br.readLine());
if(N==0){
break;
}
map = new int[N][N];
d = new int[N][N];
for (int i = 0; i < N; i++) {
String[] input = br.readLine().split(" ");
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(input[j]);
d[i][j] = INF;
}
}
dijkstra();
//d_print();
System.out.println("Problem "+count+": "+d[N-1][N-1]);
}
}
private static void d_print() {
for(int i=0; i<N; i++){
Arrays.stream(d[i]).forEach(j-> System.out.print(j+" "));
System.out.println();
}
}
public static void dijkstra() {
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.offer(new Node(0,0,map[0][0])); //start x,y
d[0][0] = map[0][0]; //start d[y][x]
while (!pq.isEmpty()) {
Node node = pq.poll();
for (int i = 0; i < dx.length; i++) {
int next_x = node.x + dx[i];
int next_y = node.y + dy[i];
if (next_x >= N || next_y >= N || next_y < 0 || next_x < 0) {
continue;
}
if(d[node.y][node.x] < node.distance){ continue; }
int cost = map[next_y][next_x] + d[node.y][node.x];
if (cost < d[next_y][next_x]) {
d[next_y][next_x] = cost;
pq.offer(new Node(next_y, next_x, map[next_y][next_x]));
}
}
}
}
}
'코딩테스트' 카테고리의 다른 글
[백준] 1753번 최단경로 JAVA 풀이 (0) | 2023.09.12 |
---|---|
[백준] 10282번- 해킹 JAVA 풀이 (0) | 2023.09.11 |
[백준] 18428번 - 감시 피하기 JAVA 풀이 (0) | 2023.09.07 |
[백준] 2174번 - 로봇 시뮬레이션 JAVA 풀이 (0) | 2023.09.06 |
[백준] 23843번 - 콘센트 JAVA 풀이 (0) | 2023.09.05 |