-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
69 lines (55 loc) Β· 2.1 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package Simulation.P15662;
import java.io.*;
import java.util.*;
public class Main {
final static int R = 2, L = 6;
final static int clock = 1, rev_clock = -1;
static String[] wheels;
static Queue<Direction> q = new LinkedList<>();
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/Simulation/P15662/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
wheels = new String[T];
for (int i = 0; i < T; i++) wheels[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());
q.offer(new Direction(idx, dir));
int cur_dir = dir;
for (int i = idx; i < T-1; i++) {
cur_dir *= -1;
if (wheels[i].charAt(R) == wheels[i+1].charAt(L)) break;
else q.offer(new Direction(i+1, cur_dir));
}
cur_dir = dir;
for (int i = idx; i > 0; i--) {
cur_dir *= -1;
if (wheels[i-1].charAt(R) == wheels[i].charAt(L)) break;
else q.offer(new Direction(i-1, cur_dir));
}
while (!q.isEmpty()) {
Direction d = q.poll();
rotate(d.idx, d.dir);
}
}
int S_count = 0;
for (int i = 0; i < T; i++) S_count += wheels[i].charAt(0) - '0';
System.out.println(S_count);
}
static void rotate(int idx, int dir) {
String w = wheels[idx];
if (dir == clock) wheels[idx] = w.charAt(w.length()-1) + w.substring(0, w.length()-1);
else if (dir == rev_clock) wheels[idx] = w.substring(1) + w.charAt(0);
}
static class Direction {
int idx;
int dir;
public Direction(int idx, int dir) {
this.idx = idx;
this.dir = dir;
}
}
}