-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14499.java
134 lines (106 loc) · 3.05 KB
/
14499.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String[] in = br.readLine().split(" ");
int N = Integer.parseInt(in[0]), M = Integer.parseInt(in[1]);
int X = Integer.parseInt(in[2]), Y = Integer.parseInt(in[3]);
int K = Integer.parseInt(in[4]);
int[][] board = new int[N][M];
for (int i = 0; i < N; i++) {
in = br.readLine().split(" ");
for (int j = 0; j < M; j++) {
board[i][j] = Integer.parseInt(in[j]);
}
}
String[] commands = br.readLine().split(" ");
Dice dice = new Dice(X, Y, board);
for (String command : commands) {
int value = dice.move(command);
if (value >= 0)
bw.write(value + "\n");
}
bw.close();
}
}
class Dice {
private int x;
private int y;
private int[] values = new int[7];
private int[][] board;
private int center = 1;
private int east = 3;
private int south = 5;
public Dice(int x, int y, int[][] board) {
this.x = x;
this.y = y;
this.board = board;
}
public int move(String direction) {
int value = -1;
switch (direction) {
case "1":
if (0 <= y + 1 && y + 1 < board[0].length) {
y++;
value = moveEast();
}
break;
case "2":
if (0 <= y - 1 && y - 1 < board[0].length) {
y--;
value = moveWest();
}
break;
case "3":
if (0 <= x - 1 && x - 1 < board.length) {
x--;
value = moveNorth();
}
break;
case "4":
if (0 <= x + 1 && x + 1 < board.length) {
x++;
value = moveSouth();
}
break;
}
return value;
}
private int moveEast() {
int newEast = center;
center = 7 - east;
east = newEast;
copy();
return values[center];
}
private int moveWest() {
int newWest = center;
center = east;
east = 7 - newWest;
copy();
return values[center];
}
private int moveNorth() {
int newNorth = center;
center = south;
south = 7 - newNorth;
copy();
return values[center];
}
private int moveSouth() {
int newSouth = center;
center = 7 - south;
south = newSouth;
copy();
return values[center];
}
private void copy() {
if (board[x][y] == 0)
board[x][y] = values[7 - center];
else {
values[7 - center] = board[x][y];
board[x][y] = 0;
}
}
}