[알고리즘] 녹색 옷 입은 애가 젤다지?

김동연·2024년 1월 29일

알고리즘

목록 보기
5/12

녹색 옷 입은 애가 젤다지? (백준 4485번)

https://www.acmicpc.net/problem/4485

공부하다보니 갑자기 다익스트라가 기억이 안나 복기할겸 풀어보았다

최단경로 알고리즘

최단 경로를 찾는 알고리즘은 크게 두가지가 있는데

  • 폴로이드 알고리즘
  • 다익스트라 알고리즘

근데 여기선 다익스트라만 알아보겠다
둘의 쓰임세만 보자면 폴로이드는 모든 경우의 수의 최단경로를 찾는 법이고 다익스트라는 한 노드에서 다른 모든 노드로 가는 최단 경로를 전부 찾는 법이다.
이 문제에선 다익스트라가 쓰인다

과정

  1. 방의 코스트의 오름차순의 우선순위 큐에 첫번째 방 (0,0)를 위치와 코스트의 배열로 집어 넣는다.
  2. 우선순위 큐에서 값을 뽑는다.그 값에서 상하좌우의 방을 조회하여 이전에 방문했던 경우의 수와 이번 경우의 수 (방의 코스트 + 이전 방까지 오는데 필요한 코스트)를 비교한다.
    2.1 이번 경우의 수가 작을 경우 그 방의 방문 경우의 수를 바꾸어 주고 우선순위큐에 해당 정보를 집어넣는다
    2.2 아닐 경우 무시
  3. 우선순위 큐가 빌때까지 2.를 반복한다.
  4. 마지막 방을 출력한다.

순서도

코드

import java.io.*;
import java.util.*;

public class Main {
    private static int N;
    private static int[][] costs;
    private static int[][] arr;
    private static final int[] X_DIR = {1,0,-1,0};
    private static final int[] Y_DIR = {0,1,0,-1};

    public static void main(final String[] args) throws IOException {
        problem(new InputStreamReader(System.in));
    }

    public static void problem(final InputStreamReader isr) throws IOException {
        final BufferedReader br = new BufferedReader(isr);
        N = Integer.parseInt(br.readLine());
        int i = 1;
        while(N != 0){
            init(br);
            System.out.println("Problem " + i + ": " + dijkstra());
            N = Integer.parseInt(br.readLine());
            i++;
        }
        init(new BufferedReader(isr));
    }

    private static void init(final BufferedReader br) throws IOException {
        costs = new int[N][N];
        arr = new int[N][N];

        for (int i = 0; i < N; i++) {
            final String[] line = br.readLine().split(" ");
            for (int j = 0; j < N; j++) {
                arr[i][j] = Integer.parseInt(line[j]);
                costs[i][j] = Integer.MAX_VALUE;
            }
        }
    }

    private static int dijkstra(){
        final PriorityQueue<int[]> pq = new PriorityQueue<>((o1, o2)->o1[2] - o2[2]);
        pq.offer(new int[]{0,0,arr[0][0]});
        costs[0][0] = arr[0][0];
        while(!pq.isEmpty()){
            final int[] node = pq.poll();
            for (int dir = 0; dir < 4; dir++) {
                final int nextX = node[0] + X_DIR[dir];
                final int nextY = node[1] + Y_DIR[dir];
                if (nextX > N-1 || nextX < 0 || nextY > N-1 || nextY < 0){
                    continue;
                }
                final int value = node[2] + arr[nextX][nextY];
                if (node[2] + arr[nextX][nextY] < costs[nextX][nextY]){
                    costs[nextX][nextY] = value;
                    pq.offer(new int[]{nextX,nextY,value});
                }
            }
        }
        return costs[N-1][N-1];
    }
}

0개의 댓글