-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
170 lines (124 loc) · 2.98 KB
/
Copy pathMain.java
File metadata and controls
170 lines (124 loc) · 2.98 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* Written by Mike O'Beirne (michael.obeirne@gmail.com)
*
* Successfully completes the ACM ICPC judge data in 1.864 seconds.
*/
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Stack;
class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
State first;
State last;
HashMap<State, State> childToParent = new HashMap<State, State>();
HashSet<State> visited = new HashSet<State>();
ArrayDeque<State> Q = new ArrayDeque<State>();
while (in.hasNext()) {
first = new State(in.nextInt(), in.nextInt(), in.nextInt());
last = first;
childToParent.put(first, null);
visited.add(first);
Q.add(first);
// If it doesn't have solution, skip algorithm
if ((first.x + first.y + first.z) % 3 != 0) {
Q.clear();
}
// Main Algorithm - BFS
while (!Q.isEmpty()) {
State current = Q.pollFirst();
if (current.finalState()) {
last = current;
Q.clear();
} else {
ArrayList<State> toAdd = current.getChildren();
// Process and filter children states
for (State s : toAdd) {
if (!visited.contains(s)) {
visited.add(s);
if (!childToParent.containsKey(s)) {
childToParent.put(s, current);
}
Q.add(s);
}
}
}
}
// Print solution and clear data structures
printSolution(last, childToParent);
Q.clear();
childToParent.clear();
visited.clear();
}
}
public static void printSolution(State last, HashMap<State, State> map) {
Stack<String> ans = new Stack<String>();
if (map == null) {
ans.add(last.toString());
} else {
State current = last;
while (current != null) {
ans.add(current.toString());
current = map.get(current);
}
}
while (!ans.isEmpty()) {
System.out.println(ans.pop());
}
System.out.println("============");
}
static class State {
int x, y, z;
int hashCode;
public State(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
hashCode = x + 2*y + 4*z;
}
public String toString() {
return String.format("%4d%4d%4d", x, y, z);
}
public boolean finalState() {
return x == y && y == z;
}
public ArrayList<State> getChildren() {
ArrayList<State> ans = new ArrayList<State>();
if (x > y) {
ans.add(new State(x - y, 2 * y, z));
}
if (x > z) {
ans.add(new State(x - z, y, 2 * z));
}
if (y > x) {
ans.add(new State(2 * x, y - x, z));
}
if (y > z) {
ans.add(new State(x, y - z, 2 * z));
}
if (z > x) {
ans.add(new State(2 * x, y, z - x));
}
if (z > y) {
ans.add(new State(x, 2 * y, z - y));
}
return ans;
}
@Override
public boolean equals(Object o) {
State that = (State) o;
// If these two hold, then the last is implied
if (x == that.x && y == that.y) {
return true;
}
return false;
}
@Override
public int hashCode() {
return hashCode;
}
}
}