알고리즘 문제 풀이/백준

[백준] 주사위 굴리기 - 14499번

h982 2021. 6. 29. 20:20

문제설명

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

 

14499번: 주사위 굴리기

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

www.acmicpc.net

 

기본아이디어

결국 이동이 일어났을 때 주사위 상태가 어떻게 변하는지를 계속해서 바꾸어 주면 된다고 생각했습니다.

그래서 주사위가 이동했을 때 이전의 주사위에서 값이 변한 위치를 업데이트 시켜주는 것으로 문제를 해결했습니다.

 

 

구현코드

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
    public static void main(String[] args) throws IOException {
        StringTokenizer st = new StringTokenizer(input.readLine());
        N = Integer.parseInt(st.nextToken());
        M = Integer.parseInt(st.nextToken());
        int x = Integer.parseInt(st.nextToken());
        int y = Integer.parseInt(st.nextToken());
        int k = Integer.parseInt(st.nextToken());
        map = new int[N][];
        for(int i = 0; i < N; i++){
            st = new StringTokenizer(input.readLine());
            int[] temp = new int[M];
            for(int j = 0; j < M; j++){
                temp[j] = Integer.parseInt(st.nextToken());
            }
            map[i] = temp;
        }
        operations = new int[k];
        st = new StringTokenizer(input.readLine());
        for(int i = 0; i < k; i++){
            operations[i] = Integer.parseInt(st.nextToken());
        }
        dice = new int[6];
        rollDice(x, y, k);
        System.out.print(output);
    }
    static int N, M;
    static int[][] map;
    static int[] operations;
    static int[] dice;
    static int[] dr = {000-11};
    static int[] dc = {01-100};
    static void rollDice(int x, int y, int k){
        for(int i = 0; i < k; i++){
            int oper = operations[i];
            int nr = x + dr[oper];
            int nc = y + dc[oper];
            if(!isIn(nr, nc))
                continue;
            int temp = 0;
            if(oper == 1){
                temp = dice[2];
                dice[2= dice[5];
                dice[5= dice[1];
                dice[1= dice[0];
            }else if(oper == 2){
                temp = dice[1];
                dice[1= dice[5];
                dice[5= dice[2];
                dice[2= dice[0];
            }else if(oper == 3){
                temp = dice[3];
                dice[3= dice[5];
                dice[5= dice[4];
                dice[4= dice[0];
            }else if(oper == 4){
                temp = dice[4];
                dice[4= dice[5];
                dice[5= dice[3];
                dice[3= dice[0];
            }
            if(map[nr][nc] == 0){
                dice[0= temp;
                map[nr][nc] = temp;
            }else{
                dice[0= map[nr][nc];
                map[nr][nc] = 0;
            }
            output.append(dice[5+ "\n");
            x = nr;
            y = nc;
        }
    }
    static boolean isIn(int r, int c){
        return r >= 0 && r < N && c >= 0 && c < M;
    }
 
cs

 

 

정리, 기억할 내용

1. 시뮬레이션 문제는 머리로 안될거 같으면 빠르게 그려보자.