-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSolution.java
More file actions
264 lines (235 loc) · 6.87 KB
/
Solution.java
File metadata and controls
264 lines (235 loc) · 6.87 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import java.util.Vector;
/**
* @author thiagogenez
*
* Facebook hiring sample test
*
* There are K pegs. Each peg can hold discs in decreasing order of
* radius when looked from bottom to top of the peg. There are N discs
* which have radius 1 to N; Given the initial configuration of the pegs
* and the final configuration of the pegs, output the moves required to
* transform from the initial to final configuration. You are required
* to do the transformations in minimal number of moves.
*
* A move consists of picking the topmost disc of any one of the pegs
* and placing it on top of anyother peg. At anypoint of time, the
* decreasing radius property of all the pegs must be maintained.
*
* Constraints: 1<= N<=8 3<= K<=5
*
* Input Format: N K 2nd line contains N integers. Each integer in the
* second line is in the range 1 to K where the i-th integer denotes the
* peg to which disc of radius i is present in the initial
* configuration. 3rd line denotes the final configuration in a format
* similar to the initial configuration.
*
* Output Format: The first line contains M - The minimal number of
* moves required to complete the transformation. The following M lines
* describe a move, by a peg number to pick from and a peg number to
* place on. If there are more than one solutions, it's sufficient to
* output any one of them. You can assume, there is always a solution
* with less than 7 moves and the initial confirguration will not be
* same as the final one.
*
* Sample Input #00:
*
* 2 3
* 1 1
* 2 2
*
* Sample Output #00:
*
* 3
* 1 3
* 1 2
* 3 2
*
* Sample Input #01:
*
* 6 4
* 4 2 4 3 1 1
* 1 1 1 1 1 1
*
* Sample Output #01:
*
* 5
* 3 1
* 4 3
* 4 1
* 2 1
* 3 1
*
* NOTE: You need to write the full code taking all inputs are from
* stdin and outputs to stdout If you are using "Java", the classname is
* "Solution"
*
*/
public class Solution {
public Solution() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
String input[] = br.readLine().split(" ");
@SuppressWarnings("unused")
int n = Integer.parseInt(input[0]);
int k = Integer.parseInt(input[1]);
String begin_config = br.readLine();
String end_config = br.readLine();
Vector<String> moves = breadthFirstSearch(k, begin_config,
end_config);
System.out.println(moves.size());
for (String move : moves) {
System.out.println(move);
}
} catch (Exception e) {
System.err.println("Error:" + e.getMessage());
e.printStackTrace();
}
}
private Vector<String> breadthFirstSearch(int k, String begin_config,
String end_config) {
Node node = new Node(null, null, begin_config);
Queue<Solution.Node> queue = new LinkedList<Solution.Node>();
queue.add(node);
while (!queue.isEmpty()) {
node = queue.remove();
if (!node.isVisited()) {
node.setVisited(true);
if (node.getConfig().equals(end_config))
break;
ArrayList<Node> nextsNodes = node.getNexts(k);
queue.addAll(nextsNodes);
}
}
queue.clear();
queue = null;
Vector<String> moves = new Vector<String>();
while (node.getFather() != null) {
moves.add(node.getLast_move());
node = node.getFather();
}
Collections.reverse(moves);
return moves;
}
public static void main(String args[]) {
new Solution();
}
private class Node {
private Node father;
private String last_move;
private String config;
private boolean visited = false;
public Node(Node father, String last_move, String config) {
super();
this.father = father;
this.last_move = last_move;
this.config = config;
}
public ArrayList<Node> getNexts(int k) {
ArrayList<Node> nexts = new ArrayList<Solution.Node>();
Vector<Stack<Integer>> pegs = getPegs(k);
for (int from = 0; from < k; from++) {
for (int to = 0; to < k; to++) {
if (from != to) {
if (!pegs.get(from).isEmpty()
&& (pegs.get(to).isEmpty() || pegs.get(from)
.peek() < pegs.get(to).peek())) {
String s[] = this.config.split(" ");
s[pegs.get(from).peek() - 1] = String
.valueOf(to + 1);
String config = "";
for (int i = 0; i < s.length; i++) {
config += s[i] + " ";
}
config = config.trim();
String last_move = String.valueOf(from + 1) + " "
+ String.valueOf(to + 1);
nexts.add(new Node(this, last_move, config));
}
}
}
}
return nexts;
}
private Vector<Stack<Integer>> getPegs(int k) {
Vector<Stack<Integer>> pegs = new Vector<Stack<Integer>>(k);
for (int i = 0; i < k; i++) {
pegs.add(new Stack<Integer>());
}
String s[] = this.config.split(" ");
for (int i = s.length - 1; i >= 0; i--) {
pegs.get(Integer.parseInt(s[i]) - 1).add(i + 1);
}
return pegs;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + getOuterType().hashCode();
result = prime * result
+ ((config == null) ? 0 : config.hashCode());
result = prime * result
+ ((father == null) ? 0 : father.hashCode());
result = prime * result
+ ((last_move == null) ? 0 : last_move.hashCode());
result = prime * result + (visited ? 1231 : 1237);
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 (!getOuterType().equals(other.getOuterType()))
return false;
if (config == null) {
if (other.config != null)
return false;
} else if (!config.equals(other.config))
return false;
if (father == null) {
if (other.father != null)
return false;
} else if (!father.equals(other.father))
return false;
if (last_move == null) {
if (other.last_move != null)
return false;
} else if (!last_move.equals(other.last_move))
return false;
if (visited != other.visited)
return false;
return true;
}
public Node getFather() {
return father;
}
public String getLast_move() {
return last_move;
}
public String getConfig() {
return config;
}
public boolean isVisited() {
return visited;
}
public void setVisited(boolean visited) {
this.visited = visited;
}
private Solution getOuterType() {
return Solution.this;
}
}
}