[BOJ] 주사위 굴리기

문제

크기가 N×M인 지도가 존재한다. 지도의 오른쪽은 동쪽, 위쪽은 북쪽이다. 이 지도의 위에 주사위가 하나 놓여져 있으며, 주사위의 전개도는 아래와 같다. 지도의 좌표는 (r, c)로 나타내며, r는 북쪽으로부터 떨어진 칸의 개수, c는 서쪽으로부터 떨어진 칸의 개수이다.

1
2
3
4
  2
4 1 3
  5
  6

주사위는 지도 위에 윗 면이 1이고, 동쪽을 바라보는 방향이 3인 상태로 놓여져 있으며, 놓여져 있는 곳의 좌표는 (x, y) 이다. 가장 처음에 주사위에는 모든 면에 0이 적혀져 있다.

지도의 각 칸에는 정수가 하나씩 쓰여져 있다. 주사위를 굴렸을 때, 이동한 칸에 쓰여 있는 수가 0이면, 주사위의 바닥면에 쓰여 있는 수가 칸에 복사된다. 0이 아닌 경우에는 칸에 쓰여 있는 수가 주사위의 바닥면으로 복사되며, 칸에 쓰여 있는 수는 0이 된다.

주사위를 놓은 곳의 좌표와 이동시키는 명령이 주어졌을 때, 주사위가 이동했을 때 마다 상단에 쓰여 있는 값을 구하는 프로그램을 작성하시오.

주사위는 지도의 바깥으로 이동시킬 수 없다. 만약 바깥으로 이동시키려고 하는 경우에는 해당 명령을 무시해야 하며, 출력도 하면 안 된다.

제한사항

첫째 줄에 지도의 세로 크기 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로 주어진다.

첫번째 생각

주사위를 굴린다는 표현을 구현하는 것을,

상하로 굴렸을 때는 mod 4 / 좌우로 굴렸을때는 mod 3으로 생각하여 구현하면 될 것이라고 생각했지만,

1
2
	int[] diceY = {1,5,6,2};
	int[] diceX = {1,3,4,6};

6과 1은 어느 방향으로 굴려도 나올 수 있기 때문에 이런식으로 변수를 선언해주었다.

출력해야하는 경우와, 복사해야 하는 경우가 다르므로 각 각의 변수로 관리해주도록 한다.

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
### 틀린코드입니다.

import java.util.Arrays;
import java.util.Scanner;

public class Main_14499 {
	static int[] dice = {0,0,0,0,0,0};
	static int[] diceX = {0,2,5,3};
	static int[] diceY = {0,4,5,1};
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int M = sc.nextInt();
		int x = sc.nextInt();
		int y = sc.nextInt();
		int K = sc.nextInt();
		int[][] map = new int[N][M];
		for(int ty=0; ty<N; ty++) {
			for(int tx=0; tx<M; tx++) {
				map[ty][tx] = sc.nextInt();
			}
		}
		System.out.println(Arrays.toString(diceX));
		System.out.println(Arrays.toString(diceY));
		System.out.println(Arrays.toString(dice));
		int top = 0;
		int bot = 0;
		int bot_i = 5; // 바텀에서 복사할때 알아보기 위한 인덱스
		int diceX_i = 0;
		int diceY_i = 0;
		for(int k=0; k<K; k++) {
			int cmd = sc.nextInt();
			// 동 : 1 / 서 : 2 / 북 : 3 / 남 : 4
			int nextX = x;
			int nextY = y;
			boolean pass = false;
//			System.out.println("@"+cmd);
			if ( cmd == 1 ) {
				nextX = x+1;
				if ( nextX >= 0 && nextX < M ) {
					diceX_i += 1;
					top = dice[diceX[(diceX_i+2)%4]];
					bot = dice[diceX[diceX_i%4]];
					bot_i = diceX[diceX_i%4];
					diceY[1] = diceX[(diceX_i%2)+2];
					diceY[3] = diceX[(diceX_i%2)];
					x = nextX;
				} else {
					pass = true;
				}
			} else if ( cmd == 2 ) {
				nextX = x-1;
				if ( nextX >= 0 && nextX < M ) {
					diceX_i += 3;
					top = dice[diceX[(diceX_i+2)%4]];
					bot = dice[diceX[diceX_i%4]];
					bot_i = diceX[diceX_i%4];
					diceY[1] = diceX[(diceX_i%2)+2];
					diceY[3] = diceX[(diceX_i%2)];
					x = nextX;
				} else {
					pass = true;
				}
			} else if ( cmd == 3 ) {
				nextY = y-1;
				if ( nextY >= 0 && nextY < N ) {
					diceY_i += 3;
					top = dice[diceY[(diceY_i+2)%4]];
					bot = dice[diceY[diceY_i%4]];
					bot_i = diceY[diceY_i%4];
					diceX[1] = diceY[(diceY_i%2)];
					diceX[3] = diceY[(diceY_i%2)+2];
					y = nextY;
				} else {
					pass = true;
				}
			} else if ( cmd == 4 ) {
				nextY = y+1;
				if ( nextY >= 0 && nextY < N ) {
					diceY_i += 1;
					top = dice[diceY[(diceY_i+2)%4]];
					bot = dice[diceY[diceY_i%4]];
					bot_i = diceY[diceY_i%4];
					diceX[0] = diceY[(diceY_i%2)+2];
					diceX[2] = diceY[(diceY_i%2)];
					y = nextY;
				} else {
					pass = true;
				}
			}
			if ( !pass ) {
				if ( map[y][x] == 0 ) {
					map[y][x] = bot; // 맵이 0이라면 바닥에 있는 수를 복사함.
				} else { 
					dice[bot_i] = map[y][x];
					map[y][x] = 0;
				}
				
				for(int ty=0; ty<N; ty++) {
					for(int tx=0; tx<M; tx++) {
						System.out.print(map[ty][tx]);
					}System.out.println();
				}
				System.out.println(Arrays.toString(diceX));
				System.out.println(Arrays.toString(diceY));
				System.out.println(Arrays.toString(dice));
				
				System.out.println(top);
				System.out.println();
			}
		}
	}
}	

두번째 생각
  1. diceX와 diceY를 통해 규칙을 찾아 해결하려 했지만, 6시간 이상 찾아보았지만 찾지못했다.

생각해보니 코딩테스트를 할 때, 작은 조건들은 노가다해서 적어주는게 나을 것이라고 생각하였다.

  1. 문제를 다시 읽어보니 x는 행 번호이고 y는 열 번호였다.

    x y 행 열은 항상 헷갈리는 부분이다. 나중에 시간내서 정리해서 개념을 굳혀야겠다.

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
import java.util.Arrays;
import java.util.Scanner;

public class Main_14499 {
	static int[] dice = {0,0,0,0,0,0};
	static int[] ndice = {0,0,0,0,0,0};
	static int[] dx = {1,-1,0,0};
	static int[] dy = {0,0,-1,1};
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int M = sc.nextInt();
		int x = sc.nextInt();
		int y = sc.nextInt();
		int K = sc.nextInt();
		int[][] map = new int[N][M];
		for(int ty=0; ty<N; ty++) {
			for(int tx=0; tx<M; tx++) {
				map[ty][tx] = sc.nextInt();
			}
		}
		int top = 0;
		int bot = 0;
		for(int k=0; k<K; k++) {
			int cmd = sc.nextInt();
			// 동 : 1 / 서 : 2 / 북 : 3 / 남 : 4
			int nextX = x + dx[cmd-1];
			int nextY = y + dy[cmd-1];
			if ( nextX >= 0 && nextX < M && nextY >= 0 && nextY < N ) {
				if ( cmd == 1 ) {
					ndice[3] = dice[1];
					ndice[1] = dice[4];
					ndice[4] = dice[6];
					ndice[6] = dice[3];
				} else if ( cmd == 2 ) {
					ndice[1] = dice[3];
					ndice[3] = dice[6];
					ndice[6] = dice[4];
					ndice[4] = dice[1];
				} else if ( cmd == 3 ) {
					ndice[1] = dice[5];
					ndice[5] = dice[6];
					ndice[6] = dice[2];
					ndice[2] = dice[1];
				} else if ( cmd == 4 ) {
					ndice[1] = dice[2];
					ndice[2] = dice[6];
					ndice[6] = dice[5];
					ndice[5] = dice[1];
				}
			}
			
			if (map[y][x] == 0) {
				map[y][x] = bot; // 맵이 0이라면 바닥에 있는 수를 복사함.
			} else {
				dice[5] = map[y][x];
				map[y][x] = 0;
			}
			x = nextX;
			y = nextY;
			for(int i=0; i<6; i++) {
				dice[i] = ndice[i];
			}
			System.out.println(top);
		}
	}
}