-
Notifications
You must be signed in to change notification settings - Fork 0
/
sml_14499_rollDice.java
108 lines (90 loc) · 2.92 KB
/
sml_14499_rollDice.java
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
package simulation;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
* 1. 문제 링크: https://www.acmicpc.net/problem/14499
*/
public class sml_14499_rollDice {
static final int[] dy = {0, 0, -1, 1};
static final int[] dx = {1, -1, 0, 0};
static int N, M;
static int[][] map;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
map = new int[N][M];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
Dice dice = new Dice(y, x);
st = new StringTokenizer(br.readLine());
while (K-- > 0) {
dice.roll(Integer.parseInt(st.nextToken()) - 1);
}
System.out.print(dice.getMovement().trim());
br.close();
}
static class Dice {
int y, x;
int[] num = new int[6];
StringBuilder sb = new StringBuilder();
public Dice(int y, int x) {
this.y = y;
this.x = x;
}
void roll(int dir) {
int ny = y + dy[dir];
int nx = x + dx[dir];
if (ny < 0 || nx < 0 || N <= ny || M <= nx) return;
// 위, 동, 북, 서, 남, 밑
int tmp = num[0];
switch (dir) {
case 0: // 동
num[0] = num[3];
num[3] = num[5];
num[5] = num[2];
num[2] = tmp;
break;
case 1: // 서
num[0] = num[2];
num[2] = num[5];
num[5] = num[3];
num[3] = tmp;
break;
case 2: // 남
num[0] = num[1];
num[1] = num[5];
num[5] = num[4];
num[4] = tmp;
break;
default: // 북
num[0] = num[4];
num[4] = num[5];
num[5] = num[1];
num[1] = tmp;
}
if (map[ny][nx] == 0) {
map[ny][nx] = num[5];
} else {
num[5] = map[ny][nx];
map[ny][nx] = 0;
}
y = ny;
x = nx;
sb.append(num[0]).append("\n");
}
String getMovement() {
return sb.toString();
}
}
}