-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
48 lines (39 loc) Β· 1.61 KB
/
Main.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
package Simulation.P14891;
import java.io.*;
import java.util.*;
public class Main {
final static int R = 2, L = 6;
static String[] wheel = new String[4];
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Simulation/P14891/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
for (int i = 0; i < 4; i++) wheel[i] = br.readLine();
int K = Integer.parseInt(br.readLine());
while (K-- > 0) {
StringTokenizer st = new StringTokenizer(br.readLine());
int idx = Integer.parseInt(st.nextToken()) - 1;
int dir = Integer.parseInt(st.nextToken());
int[] dirs = new int[4];
dirs[idx] = dir;
for (int i = idx; i < 3; i++) {
if (wheel[i].charAt(R) != wheel[i+1].charAt(L))
dirs[i+1] = dirs[i] * (-1);
}
for (int i = idx; i > 0; i--) {
if (wheel[i].charAt(L) == wheel[i-1].charAt(R)) break;
else dirs[i-1] = dirs[i] * (-1);
}
for (int i = 0; i < 4; i++) {
if (dirs[i] != 0) rotate(i, dirs[i]);
}
}
int ans = 0;
for (int i = 0; i < 4; i++) ans += (wheel[i].charAt(0) - '0') * Math.pow(2, i);
System.out.println(ans);
}
static void rotate(int idx, int dir) {
String s = wheel[idx];
if (dir == 1) wheel[idx] = s.charAt(s.length()-1) + s.substring(0, s.length()-1);
else if (dir == -1) wheel[idx] = s.substring(1) + s.charAt(0);
}
}