ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [백준]2665: 미로만들기 - JAVA
    문제풀이/백준 2021. 4. 11. 15:44

    [백준]2665: 미로만들기

    www.acmicpc.net/problem/2665

     

    2665번: 미로만들기

    첫 줄에는 한 줄에 들어가는 방의 수 n(1≤n≤50)이 주어지고, 다음 n개의 줄의 각 줄마다 0과 1이 이루어진 길이가 n인 수열이 주어진다. 0은 검은 방, 1은 흰 방을 나타낸다.

    www.acmicpc.net

    풀이

    BFS + 다익스트라를 활용하여 풀었다. 

     

    첫 노드에서 오른쪽 아래 노드로 가는 최단 거리 중에서 검은색 벽을 가장 적게 바꾸는 방법의 횟수를 출력하는 문제로 이전에 풀었던 '벽 부수고 이동하기'와 유사한 문제였다.

     

    검은색 벽을 흰색 벽으로 바꾸는 최소 횟수를 누적하여 계산하며 탐색하기 위해 다익스트라 방법을 사용하였다. dist배열을 사용하여 현재 위치의 가장 최소 횟수를 저장할 수 있도록 하였다.

     

    현재 거리 보다 더 적은 횟수로 이동할 수 있는 방법이 있다면 그 방향으로 이동시켜 주었다. 이때 board가 1이면 횟수를 더해주지 않았고, board가 0이면 흰색으로 변경해 주어야 하므로 횟수를 누적하여 더해주었다.

     

    코드

    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
    import java.util.*;
     
    public class Main {
        
        static int n;
        static int[][] board;
        static int[] dx = {010-1};
        static int[] dy = {10-10};
        static int[][] dist;
        
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
        
            n = scan.nextInt();
            scan.nextLine();
            board = new int[n][n];
            dist = new int[n][n];
            for(int i = 0; i < n; i++) {
                String temp = scan.nextLine();
                for(int j = 0; j < n; j++) {
                    board[i][j] = temp.charAt(j) - '0';
                    dist[i][j] = Integer.MAX_VALUE;
                }
            }
            
            bfs();
            System.out.println(dist[n - 1][n - 1]);
        }
        
        public static void bfs() {
            Queue<Node> q = new LinkedList<>();
            q.offer(new Node(00));
            dist[0][0= 0;
            
            while(!q.isEmpty()) {
                Node current = q.poll();
                
                for(int i = 0; i < 4; i++) {
                    int nx = current.x + dx[i];
                    int ny = current.y + dy[i];
                    if(nx >= 0 && ny >= 0 && nx < n && ny < n && dist[nx][ny] > dist[current.x][current.y]) {
                        if(board[nx][ny] == 1) dist[nx][ny] = dist[current.x][current.y];
                        else dist[nx][ny] = dist[current.x][current.y] + 1;
                        q.offer(new Node(nx, ny));
                    }
                }
            }
        }
        
        public static class Node {
            int x;
            int y;
            
            public Node(int x, int y) {
                this.x = x;
                this.y = y;
            }
        }
    }
    cs

    댓글

Designed by Tistory.