-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPlayingWithWheels.java
128 lines (112 loc) · 3.84 KB
/
PlayingWithWheels.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
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class PlayingWithWheels {
private static final BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
static class Node {
int[] node;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(node);
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Node other = (Node) obj;
if (!Arrays.equals(node, other.node))
return false;
return true;
}
public Node(List<Integer> s) {
this.node = new int[s.size()];
for (int i = 0; i < s.size(); ++i) {
this.node[i] = s.get(i).intValue();
}
}
public Node(Node node, int i, int p) {
this.node = Arrays.copyOf(node.node, node.node.length);
this.node[i] += p;
if (this.node[i] < 0) {
this.node[i] = 9;
} else {
this.node[i] %= 10;
}
}
}
private static Set<Node> adjacent(Node node) {
Set<Node> adjacent = new HashSet<>();
for (int i = 0; i < node.node.length; ++i) {
adjacent.add(new Node(node, i, 1));
adjacent.add(new Node(node, i, -1));
}
return adjacent;
}
private static int solve(Node start, Node end, Set<Node> forbidden) {
if (start.equals(end)) {
return 0;
}
Deque<Node> next = new ArrayDeque<>();
Set<Node> visited = new HashSet<>();
Map<Node, Integer> depths = new HashMap<>();
int depth = 0;
next.push(start);
depths.put(start, depth);
while (!next.isEmpty()) {
Node node = next.pop();
depth = depths.get(node);
for (Node adjNode : adjacent(node)) {
if (adjNode.equals(end)) {
return forbidden.contains(end) ? -1 : depth + 1;
}
if (!forbidden.contains(adjNode) &&
!visited.contains(adjNode)) {
visited.add(adjNode);
next.addLast(adjNode);
depths.put(adjNode, depth + 1);
}
}
}
return -1;
}
private static List<Integer> parseLine(String line) {
return stream(line.trim().split(" "))
.filter(x -> !x.equals(""))
.map(Integer::parseInt)
.collect(toList());
}
public static void main(String[] args) throws IOException {
int n = Integer.parseInt(reader.readLine().trim());
for (int i = 0; i < n; ++i) {
String currentLine = "";
while ((currentLine = reader.readLine()).trim().equals(""))
;
Node start = new Node(parseLine(currentLine));
Node end = new Node(parseLine(reader.readLine()));
int m = Integer.parseInt(reader.readLine().trim());
Set<Node> forbidden = new HashSet<>();
for (int j = 0; j < m; ++j) {
forbidden.add(new Node(parseLine(reader.readLine())));
}
System.out.println(solve(start, end, forbidden));
}
}
}