You will be fine

<Algorithm> 22. 1261번 알고스팟(벽1 부수기)

by BFine
반응형

1. 1261번 알고스팟(벽1 부수기)

  • Dequeue 와 우선순위 Queue 를 이용하여 풀 수 있다. -> 우선순위가 2가지 일경우에 Dequeue 사용

  • 벽을 부수지 않는 경우를 먼저 탐색하여 방문하지 않은 곳을 방문한 뒤 벽을 부순 경우를 탐색한다. 

  • 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
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.ArrayDeque;
    import java.util.StringTokenizer;
     
     
    public class Main {
        public static void main(String[] args) throws IOException {
            
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            StringTokenizer st = new StringTokenizer(br.readLine());
            
            int M = Integer.parseInt(st.nextToken());
            int N = Integer.parseInt(st.nextToken());
            int[][] graph =new int[N][M];
            int[][] dist = new int[N][M]; // 벽 1을 부순 횟수
            
            for(int i = 0; i < N; i++){
                for(int j = 0; j < M; j++) {
                    dist[i][j] = -1;
                }
            }
                    
            for(int i = 0; i < N ; i++) {
                st = new StringTokenizer(br.readLine());
                String temp = st.nextToken();
                for(int j = 0; j < M; j++) {
                    graph[i][j] = temp.charAt(j) - '0';
                }
            }
            
            
            int[] dx = {001-1};
            int[] dy = {1-100};
            ArrayDeque<Location> aDeque = new ArrayDeque<>();
            
            dist[0][0= 0;
            
            aDeque.add(new Location(00));
            
            while(!aDeque.isEmpty()) {
                Location loc = aDeque.poll();
                int x = loc.x;
                int y = loc.y;
                
                for(int k = 0; k < 4; k++) {
                    int nx = x + dx[k];  // 상하좌우
                    int ny = y + dy[k];
                
                    if(nx >= && ny >= && nx < N && ny < M) {
                        if(dist[nx][ny] == -1) { // 방문하지 않았을때
                            if(graph[nx][ny] == 0) { 
                                dist[nx][ny] = dist[x][y];
                                aDeque.addFirst(new Location(nx, ny));
                                //우선순위를 두는것 !! 0은 벽 1을 부수지 않기 때문에 앞에 추가
                            }else {
                                dist[nx][ny] = dist[x][y] + 1;
                                aDeque.addLast(new Location(nx, ny));
                                //벽을 부쉇기 때문에 우선순위가 아래
                            }
                        }
                    }    
                }
            }
            
            
            System.out.println(dist[N-1][M-1]);
        
        }
     
    }
     
    class Location{
        int x;
        int y;
        public Location(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }
    cs

반응형

블로그의 정보

57개월 BackEnd

BFine

활동하기