ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [BOJ] 녹색 옷 입은 애가 젤다지?
    Algorithm/BOJ 2020. 10. 19. 19:56

    [4485] 녹색 옷 입은 애가 젤다지?

    www.acmicpc.net/problem/4485

    Solution

    • 각 동굴의 칸을 정점으로 정의하고, 다익스트라를 활용했다.
    • map의 (0, 0)을 다익스트라의 시작점으로 정하고 (n-1, n-1)칸 까지의 최단 거리를 구했다.

     

     

    소스코드

    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
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.*;
     
    public class Main {
        static int n;
        static Node[][] map;
        static int[] distance, visited;
        static int[] dx = {1-100};
        static int[] dy = {001-1};
        static List<List<Node>> graph;
     
        static class Node implements Comparable<Node> {
            private int idx;
            private int value;
     
            public Node(int idx, int value) {
                this.idx = idx;
                this.value = value;
            }
     
            @Override
            public int compareTo(Node o) {
                return this.value - o.value;
            }
     
            @Override
            public String toString() {
                return "Node{" +
                        "idx=" + idx +
                        ", value=" + value +
                        '}';
            }
        }
     
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     
            int tc = 1;
            while (true) {
                n = stoi(br.readLine());
     
                if (n == 0break;
     
                map = new Node[n][n];
                distance = new int[n * n];
                visited = new int[n * n];
     
                graph = new ArrayList<>();
     
                for (int i = 0; i < n * n; i++) {
                    graph.add(new ArrayList<>());
                }
     
                int idx = 0;
                StringTokenizer st;
                for (int i = 0; i < n; i++) {
                    st = new StringTokenizer(br.readLine());
                    for (int j = 0; j < n; j++) {
                        map[i][j] = new Node(idx++, stoi(st.nextToken()));
                    }
                }
     
                // 각 정점에 연결된 간선 추가
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        // 2차원 배열이기 때문에 4방향 탐색
                        for (int k = 0; k < 4; k++) {
                            int nx = i + dx[k];
                            int ny = j + dy[k];
     
                            if (nx >= 0 && ny >= 0 && nx < n && ny < n) {
                                graph.get(map[i][j].idx).add(new Node(map[nx][ny].idx, map[nx][ny].value));
                            }
                        }
                    }
                }
     
                dijkstra(0);
     
                System.out.println("Problem " + tc++ + ": " + (map[0][0].value + distance[n * n - 1]));
            }
        }
     
        static void dijkstra(int k) {
            PriorityQueue<Node> pq = new PriorityQueue<>();
            pq.add(new Node(k, 0));
     
            Arrays.fill(distance, Integer.MAX_VALUE);
            distance[k] = 0;
     
            while (!pq.isEmpty()) {
                int cur = pq.poll().idx;
     
                if (visited[cur] == 1continue;
                visited[cur] = 1;
     
                for (Node node : graph.get(cur)) {
                    if (distance[node.idx] > distance[cur] + node.value) {
                        distance[node.idx] = distance[cur] + node.value;
     
                        pq.add(new Node(node.idx, distance[node.idx]));
                    }
                }
            }
        }
     
        private static int stoi(String input) {
            return Integer.parseInt(input);
        }
    }
    cs

    'Algorithm > BOJ' 카테고리의 다른 글

    [BOJ] 복도 뚫기  (0) 2020.10.21
    [BOJ] 도시 분할 계획  (0) 2020.10.19
    [BOJ] 치즈  (0) 2020.10.19
    [BOJ] 후보 추천하기  (0) 2020.10.04
    [BOJ] 다리 만들기 2  (0) 2020.09.08

    댓글

Designed by Tistory.