Lined Notebook

[Gold 5] [Java] 주사위 굴리기 (114499 번)

by HeshAlgo

Java Code

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.baekjoon.java;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
 
public class Main {
    static int[] dx = {000-11};
    static int[] dy = {01-100};
 
    static int N, M, x, y, k;
    static int[][] map;
    static int[] dice;
    static List<Integer> answer;
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] input; 
        input = br.readLine().split(" ");
        N = Integer.parseInt(input[0]);     // 세로 크기
        M = Integer.parseInt(input[1]);        // 가로 크기
        x = Integer.parseInt(input[2]);        // 주사위를 놓은 곳의 x좌표
        y = Integer.parseInt(input[3]);        // 주사위를 놓은 곳의 y좌표
        k = Integer.parseInt(input[4]);        // 명령의 개수
        map = new int[N][M];
        dice = new int[7];
        
        for (int row = 0; row < N; row++) {
            input = br.readLine().split(" ");
            for (int col = 0; col < M; col++) {
                map[row][col] = Integer.parseInt(input[col]);
            }
        }
        
        answer = new ArrayList<Integer>();
        input = br.readLine().split(" ");
        int index = 0;
   
        while (k-- > 0) {
            // 입력 받은 방향 정보
            int dir = Integer.parseInt(input[index++]);
            // 다음 방향으로 이동할 좌표
            int nx = x + dx[dir];
            int ny = y + dy[dir];
            
            if (0 <= nx && nx < N && 0 <= ny && ny < M) {
                // 주사위가 이동한 경로의 위치를 바꿔주기
                change(dir);
                
                // 다음 방향으로 이동했을 때 맵에 쓰여있는 수가 0인 경우
                if (map[nx][ny] == 0) {
                    map[nx][ny] = dice[6];
                    
                } 
                // 다음 방향으로 이동했을 때 맵에 쓰여있는 수가 0이 아닌 경우
                else if (map[nx][ny] != 0) {
                    dice[6= map[nx][ny];
                    map[nx][ny] = 0;
                }
                x = nx;
                y = ny;
                answer.add(dice[1]);
            }
        
        
        }    // End of while
        
        for (int i = 0; i < answer.size(); i++) {
            System.out.println(answer.get(i));
        }
        
        
    }
 
    private static void change(int dir) {
        int[] temp = dice.clone();
        
        switch (dir) {
        // 오른쪽
        case 1:
            dice[1= temp[4];
            dice[3= temp[1];
            dice[6= temp[3];
            dice[4= temp[6];
            break;
        // 왼쪽
        case 2:
            dice[6= temp[4];
            dice[4= temp[1];
            dice[1= temp[3];
            dice[3= temp[6];
            break;
        // 위
        case 3:
            dice[2= temp[1];
            dice[6= temp[2];
            dice[1= temp[5];
            dice[5= temp[6];
            break;
        // 아래
        case 4:
            dice[5= temp[1];
            dice[1= temp[2];
            dice[6= temp[5];
            dice[2= temp[6];
            break;
 
        }
        
        
    }
    
}
 
cs

블로그의 정보

꾸준히 공부하는 개발 노트

HeshAlgo

활동하기