백준 14499

Genie·2021년 11월 22일
0

백준

목록 보기
12/12

백준 14499 주사위 굴리기

🔍 문제 설명

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

✔ 입력

첫째 줄에 지도의 세로 크기 N, 가로 크기 M (1 ≤ N, M ≤ 20), 주사위를 놓은 곳의 좌표 x, y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1), 그리고 명령의 개수 K (1 ≤ K ≤ 1,000)가 주어진다.

둘째 줄부터 N개의 줄에 지도에 쓰여 있는 수가 북쪽부터 남쪽으로, 각 줄은 서쪽부터 동쪽 순서대로 주어진다. 주사위를 놓은 칸에 쓰여 있는 수는 항상 0이다. 지도의 각 칸에 쓰여 있는 수는 10 미만의 자연수 또는 0이다.

마지막 줄에는 이동하는 명령이 순서대로 주어진다. 동쪽은 1, 서쪽은 2, 북쪽은 3, 남쪽은 4로 주어진다.

✔ 출력

이동할 때마다 주사위의 윗 면에 쓰여 있는 수를 출력한다. 만약 바깥으로 이동시키려고 하는 경우에는 해당 명령을 무시해야 하며, 출력도 하면 안 된다.

💡 풀이

  • 주사위를 0으로 초기화 시켜주지 않아서 왜 틀렸는지 보고있었다. 항상 변수에 대한 값을 초기화 시킬 때에는 문제를 잘 읽고 문제대로 초기화하는 습관을 들여야겠다.
  • "뭐가 이상하지? 라고 생각이 될 땐, 문제를 다시 읽어보자"
  • 주사위를 동,서,남,북 으로 굴릴 때 전개도에 대한 배열을 만들어서 1번지는 어디로가고, 2번지는 어디로간다를 미리 정해두자.

📝 소스코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    static FastReader scan = new FastReader();
    static StringBuilder sb = new StringBuilder();

    static int N, M, K;
    static int x, y;
    static int[][] map;
    static int[] square = new int[7]; // 주사위, 1~6 index
    static int[] instructions;
    static int[][] directions = { { 0, 0 }, { 0, 1 }, { 0, -1 }, { -1, 0 }, { 1, 0 } }; // 차례대로 동, 서, 북, 남

    static void input() {
        N = scan.nextInt(); // 세로 크기, r 과 관련 있음
        M = scan.nextInt(); // 가로 크기, c 와 관련 있음
        x = scan.nextInt();
        y = scan.nextInt();
        K = scan.nextInt(); // 총 횟수
        map = new int[N][M]; // 0,0 부터 N-1,M-1 까지
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                map[i][j] = scan.nextInt();
            }
        }

        instructions = new int[K];
        for (int i = 0; i < K; i++) {
            instructions[i] = scan.nextInt();
        }
    }

    static void rotate(int direction) {

        int[] temp = new int[7];
        for (int i = 1; i < 7; i++) {
            temp[i] = square[i];
        }
        switch (direction) {
        case 1: // 동쪽
            square[3] = temp[2];
            square[4] = temp[3];
            square[6] = temp[4];
            square[2] = temp[6];
            break;
        case 2: // 서쪽
            square[6] = temp[2];
            square[2] = temp[3];
            square[3] = temp[4];
            square[4] = temp[6];
            break;
        case 3: // 북쪽
            square[6] = temp[1];
            square[1] = temp[3];
            square[3] = temp[5];
            square[5] = temp[6];
            break;
        case 4: // 남쪽
            square[3] = temp[1];
            square[5] = temp[3];
            square[6] = temp[5];
            square[1] = temp[6];
            break;
        }
    }

    static boolean move(int direction) {
        // 방향에 따른 좌표 계산하기
        int nx = x + directions[direction][0]; // r
        int ny = y + directions[direction][1]; // c

        // 바깥으로 이동하려는 경우에는 무시한다.
        if (nx >= N || ny >= M || nx < 0 || ny < 0) {
            return false; // 이동 불가
        }

        x = nx;
        y = ny;
        return true;
    }

    static void pro() {
        for (int i = 0; i < K; i++) {
            // map 의 좌표 이동
            if (move(instructions[i])) {
                rotate(instructions[i]); // 주사위만 회전
                if (map[x][y] == 0) {
                    map[x][y] = square[6];
                } else {
                    square[6] = map[x][y];
                    map[x][y] = 0;
                }
                sb.append(square[3]).append('\n');
            }
        }
        System.out.println(sb.toString());
    }

    public static void main(String[] args) {
        input();
        pro();
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            try {
                while (st == null || !st.hasMoreTokens()) {
                    st = new StringTokenizer(br.readLine());
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return st.nextToken();
        }

        Integer nextInt() {
            return Integer.parseInt(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return str;
        }
    }
}
profile
차근차근

0개의 댓글