-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDice.java
More file actions
79 lines (72 loc) · 2.54 KB
/
Dice.java
File metadata and controls
79 lines (72 loc) · 2.54 KB
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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.IOException;
public class Main {
private static int[][] map;
private static int N;
private static int M;
private static int row;
private static int col;
private static int k;
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());
row = Integer.parseInt(st.nextToken());
col = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
map = new int[N][M];
for (int r = 0; r < N; r++) {
st = new StringTokenizer(br.readLine());
for (int c = 0; c < M; c++) {
map[r][c] = Integer.parseInt(st.nextToken());
}
}
int[] drow = new int[] { 0, 0, -1, +1 };
int[] dcol = new int[] { 1, -1, 0, 0 };
int[] dice = new int[7];
int nrow, ncol;
st = new StringTokenizer(br.readLine());
while (k > 0) {
int dir = Integer.parseInt(st.nextToken());
nrow = row + drow[dir - 1];
ncol = col + dcol[dir - 1];
if (nrow >= 0 && nrow < N && ncol >= 0 && ncol < M) {
int top = dice[1];
if (dir == 1) {
dice[1] = dice[4];
dice[4] = dice[6];
dice[6] = dice[3];
dice[3] = top;
} else if (dir == 2) {
dice[1] = dice[3];
dice[3] = dice[6];
dice[6] = dice[4];
dice[4] = top;
} else if (dir == 3) {
dice[1] = dice[5];
dice[5] = dice[6];
dice[6] = dice[2];
dice[2] = top;
} else {
dice[1] = dice[2];
dice[2] = dice[6];
dice[6] = dice[5];
dice[5] = top;
}
row = nrow;
col = nrow;
if (map[row][col] == 0) {
map[row][col] = dice[6];
} else {
dice[6] = map[row][col];
map[row][col] = 0;
}
System.out.println(dice[1]);
}
k--;
}
}
}