Algorithm/백준

[백준]16985 : Maaaaaaaaaze(java)

코딩너굴 2022. 7. 17. 21:16
728x90

문제

평화롭게 문제를 경작하며 생활하는 BOJ 마을 사람들은 더 이상 2차원 미로에 흥미를 느끼지 않는다. 2차원 미로는 너무나 쉽게 탈출이 가능하기 때문이다. 미로를 이 세상 그 누구보다 사랑하는 준현이는 이런 상황을 매우 안타깝게 여겨 아주 큰 상금을 걸고 BOJ 마을 사람들의 관심을 확 끌 수 있는 3차원 미로 탈출 대회를 개최하기로 했다.

대회의 규칙은 아래와 같다.

  • 5×5 크기의 판이 5개 주어진다. 이중 일부 칸은 참가자가 들어갈 수 있고 일부 칸은 참가자가 들어갈 수 없다. 그림에서 하얀 칸은 참가자가 들어갈 수 있는 칸을, 검은 칸은 참가자가 들어갈 수 없는 칸을 의미한다.
  • 참가자는 주어진 판들을 시계 방향, 혹은 반시계 방향으로 자유롭게 회전할 수 있다. 그러나 판을 뒤집을 수는 없다.
  • 회전을 완료한 후 참가자는 판 5개를 쌓는다. 판을 쌓는 순서는 참가자가 자유롭게 정할 수 있다. 이렇게 판 5개를 쌓아 만들어진 5×5×5 크기의 큐브가 바로 참가자를 위한 미로이다. 이 때 큐브의 입구는 정육면체에서 참가자가 임의로 선택한 꼭짓점에 위치한 칸이고 출구는 입구와 면을 공유하지 않는 꼭짓점에 위치한 칸이다.
  • 참가자는 현재 위치한 칸에서 면으로 인접한 칸이 참가자가 들어갈 수 있는 칸인 경우 그 칸으로 이동할 수 있다.
  • 참가자 중에서 본인이 설계한 미로를 가장 적은 이동 횟수로 탈출한 사람이 우승한다. 만약 미로의 입구 혹은 출구가 막혀있거나, 입구에서 출구에 도달할 수 있는 방법이 존재하지 않을 경우에는 탈출이 불가능한 것으로 간주한다.

이 대회에서 우승하기 위해서는 미로를 잘 빠져나올 수 있기 위한 담력 증진과 체력 훈련, 그리고 적절한 운이 제일 중요하지만, 가장 적은 이동 횟수로 출구에 도달할 수 있게끔 미로를 만드는 능력 또한 없어서는 안 된다. 주어진 판에서 가장 적은 이동 횟수로 출구에 도달할 수 있게끔 미로를 만들었을 때 몇 번 이동을 해야하는지 구해보자. 

입력

첫째 줄부터 25줄에 걸쳐 판이 주어진다. 각 판은 5줄에 걸쳐 주어지며 각 줄에는 5개의 숫자가 빈칸을 사이에 두고 주어진다. 0은 참가자가 들어갈 수 없는 칸, 1은 참가자가 들어갈 수 있는 칸을 의미한다.

출력

첫째 줄에 주어진 판으로 설계된 미로를 탈출하는 가장 적은 이동 횟수를 출력한다. 단, 어떻게 설계하더라도 탈출이 불가능할 경우에는 -1을 출력한다.

예제 입력 1 

1 1 1 1 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 1 1 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 1 1 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 1 1 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 1 1 1
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

예제 출력 1 

12

풀이

1) 순열로 쌓는 순서를 정합니다.

2) 순열로 회전 회수를 정합니다.

3) 0, 0, 0 > 4, 4, 4가 이동할 수 있는지 확인합니다.

를 잘 구현하면 되는 문제였습니다.

1) 순열로 쌓는 순서 정하기

public static void getOrder(int cnt, int[] order, boolean[] isOrder) {
    if(cnt == 5) {
        getRotate(0, new int[5], order);
        return;
    }

    for(int i=0; i<5; i++) {
        if(isOrder[i]) continue;
        isOrder[i] = true;
        order[cnt] = i;
        getOrder(cnt+1, order, isOrder);
        isOrder[i] = false;
    }
}

2) 회전 회수를 정합니다.

public static void getRotate(int cnt, int[] rotate, int[] order) {
    if(cnt == 5) {
        int[][][] tmp = rotateMap(order, rotate);
        //입구와 출구가 막혀있다.
        if(tmp[0][0][0] == 0 || tmp[4][4][4] == 0) return;
        bfs(tmp);
        return;
    }

    for(int i=0; i<4; i++) {
        rotate[cnt] = i;
        getRotate(cnt+1, rotate, order);
    }
}

3) 정해진 회전 수 만큼 회전시킵니다.

public static int[][][] rotateMap(int[] order, int[] rotate) {
    int[][][]tmp = new int[5][5][5];

    for(int o=0; o<5; o++) {
        int now = order[o];
        int rCnt = rotate[now];

        for(int i=0; i<5; i++) {
            for(int j=0; j<5; j++) {
                if(rCnt == 0)   tmp[o][i][j] = map[now][i][j];
                else if(rCnt == 1)   tmp[o][j][4-i] = map[now][i][j];
                else if(rCnt == 2)   tmp[o][4-i][4-j] = map[now][i][j];
                else if(rCnt == 3)   tmp[o][4-j][i] = map[now][i][j];
            }
        }
    }

    return tmp;
}

4) bfs로 도착할 수 있는지 확인합니다.

public static void bfs(int[][][] cube) {
    Queue<Point> q = new LinkedList<>();
    boolean[][][] visited = new boolean[5][5][5];
    q.add(new Point(0, 0, 0));
    visited[0][0][0] = true;
    int time = 0;

    while(!q.isEmpty()) {
        int len = q.size();

        for(int l=0; l<len; l++) {
            Point now = q.poll();
            if(now.x == 4 && now.y == 4 && now.z == 4) {
                ans = Math.min(time, ans);
                return;
            }
            for(int i=0; i<6; i++) {
                int nx = now.x + dist[i][0];
                int ny = now.y + dist[i][1];
                int nz = now.z + dist[i][2];

                if(!isIn(nx, ny, nz) || visited[nz][nx][ny] || cube[nz][nx][ny] == 0) continue;
                q.add(new Point(nx, ny, nz));
                visited[nz][nx][ny] = true;
            }
        }
        time++;
    }
}

전체 코드

package 백준;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class BJ_G3_16985_Maaaaaaaaaze {
    static class Point{
        int x;
        int y;
        int z;
        Point(int x, int y, int z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }
    }
    static int[][][] map = new int[5][5][5];
    static int[][] dist = {{1, 0, 0}, {-1, 0, 0}, {0, 1, 0}, {0, -1, 0}, {0, 0, 1}, {0, 0, -1}};
    static int ans = Integer.MAX_VALUE;

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        for(int i=0; i<5; i++) {
            for(int r=0; r<5; r++) {
                st = new StringTokenizer(br.readLine());
                for(int c=0; c<5; c++) {
                    map[i][r][c] = Integer.parseInt(st.nextToken());
                }
            }
        }
        getOrder(0, new int[5], new boolean[5]);
        System.out.println(ans == Integer.MAX_VALUE ? -1 : ans);
    }

    public static void bfs(int[][][] cube) {
        Queue<Point> q = new LinkedList<>();
        boolean[][][] visited = new boolean[5][5][5];
        q.add(new Point(0, 0, 0));
        visited[0][0][0] = true;
        int time = 0;

        while(!q.isEmpty()) {
            int len = q.size();

            for(int l=0; l<len; l++) {
                Point now = q.poll();
                if(now.x == 4 && now.y == 4 && now.z == 4) {
                    ans = Math.min(time, ans);
                    return;
                }
                for(int i=0; i<6; i++) {
                    int nx = now.x + dist[i][0];
                    int ny = now.y + dist[i][1];
                    int nz = now.z + dist[i][2];

                    if(!isIn(nx, ny, nz) || visited[nz][nx][ny] || cube[nz][nx][ny] == 0) continue;
                    q.add(new Point(nx, ny, nz));
                    visited[nz][nx][ny] = true;
                }
            }
            time++;
        }
    }

    public static int[][][] rotateMap(int[] order, int[] rotate) {
        int[][][]tmp = new int[5][5][5];

        for(int o=0; o<5; o++) {
            int now = order[o];
            int rCnt = rotate[now];

            for(int i=0; i<5; i++) {
                for(int j=0; j<5; j++) {
                    if(rCnt == 0)   tmp[o][i][j] = map[now][i][j];
                    else if(rCnt == 1)   tmp[o][j][4-i] = map[now][i][j];
                    else if(rCnt == 2)   tmp[o][4-i][4-j] = map[now][i][j];
                    else if(rCnt == 3)   tmp[o][4-j][i] = map[now][i][j];
                }
            }
        }

        return tmp;
    }

    public static void getRotate(int cnt, int[] rotate, int[] order) {
        if(cnt == 5) {
            int[][][] tmp = rotateMap(order, rotate);
            //입구와 출구가 막혀있다.
            if(tmp[0][0][0] == 0 || tmp[4][4][4] == 0) return;
            bfs(tmp);
            return;
        }

        for(int i=0; i<4; i++) {
            rotate[cnt] = i;
            getRotate(cnt+1, rotate, order);
        }
    }

    public static void getOrder(int cnt, int[] order, boolean[] isOrder) {
        if(cnt == 5) {
            getRotate(0, new int[5], order);
            return;
        }

        for(int i=0; i<5; i++) {
            if(isOrder[i]) continue;
            isOrder[i] = true;
            order[cnt] = i;
            getOrder(cnt+1, order, isOrder);
            isOrder[i] = false;
        }
    }

    public static boolean isIn(int x, int y, int z) {
        return 0<=x && x<5 && 0<=y && y<5 && 0<=z && z<5;
    }
}
728x90