/
SpeedRunner.java
387 lines (333 loc) · 10.4 KB
/
SpeedRunner.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import java.io.*;
import java.util.*;
import java.math.*;
public class SpeedRunner implements Runnable {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out), true);
static StringTokenizer st = new StringTokenizer("");
public static String next() {
try {
while (!st.hasMoreTokens()) {
String s = br.readLine();
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
} catch(Exception e) {
return null;
}
}
public static void main(String[] asda) throws Exception {
new Thread(null, new SpeedRunner(), "SpeedRunner", 1<<26).start();
}
public void run() {
int cases = Integer.parseInt( next() );
for (int k = 1; k <= cases; k++) {
readInput();
String ans = solveCase();
out.println("Case #" + k + ": " + ans);
}
//
out.flush();
System.exit(0);
}
void readInput() {
N = Integer.parseInt( next() );
M = Integer.parseInt( next() );
start = null;
exit = null;
keys = new ArrayList<>();
board = new char[N][M];
for (int r = 0; r < N; r++) {
String s = next();
for (int c = 0; c < M; c++) {
board[r][c] = s.charAt(c);
if (board[r][c] == START) {
start = new Location(r, c);
board[r][c] = EMPTY;
} else if (board[r][c] == EXIT) {
exit = new Location(r, c);
board[r][c] = EMPTY;
} else if (board[r][c] == KEY) {
keys.add(new Location(r, c));
board[r][c] = EMPTY;
}
}
}
}
String solveCase() {
pathTreeCache = new HashMap<>();
bestPathCache = new HashMap<>();
mem = new PathResult[keys.size() + 1][1 << keys.size()];
computed = new boolean[keys.size() + 1][1 << keys.size()];
PathResult result = solve(-1, 0);
if (result == null) {
return "IMPOSSIBLE";
}
int frames = result.state.frames;
return frames + " " + result.path;
}
// use memoization
PathResult[][] mem;
boolean[][] computed;
PathResult solve(int lastKey, int usedKeys) {
// an invalid key index means we are starting
Location lastLocation = start;
if (lastKey >= 0) {
lastLocation = keys.get(lastKey);
}
if (Integer.bitCount(usedKeys) == keys.size()) {
// got all keys
return bestPath(lastLocation, exit);
}
if (computed[lastKey + 1][usedKeys]) {
return mem[lastKey + 1][usedKeys];
}
PathResult best = null;
for (int key = 0; key < keys.size(); key++) if ( ((1<<key)&usedKeys) == 0 ) {
// go to key after lastKey
PathResult first = bestPath(lastLocation, keys.get(key));
if (first == null) {
continue;
}
PathResult second = solve(key, usedKeys | (1<<key));
if (second == null) {
continue;
}
PathResult current = mergePaths(first, second);
if (current.compareTo(best) < 0) {
best = current;
}
}
computed[lastKey + 1][usedKeys] = true;
return mem[lastKey + 1][usedKeys] = best;
}
PathResult mergePaths(PathResult first, PathResult second) {
State newState = new State(
second.state.location,
first.state.frames + second.state.frames,
first.state.keysPressed + second.state.keysPressed
);
PathResult result = new PathResult(newState, first.path + second.path);
return result;
}
// compute the distnace from begin to any node
Map<Location, State[][]> pathTreeCache;
State[][] pathTree(Location begin) {
if (pathTreeCache.containsKey(begin)) {
return pathTreeCache.get(begin);
}
PriorityQueue<State> queue = new PriorityQueue<>();
// compute disntance from every node to end
State[][] dist = new State[N][M];
queue.add( new State(begin, 0, 0) );
while (!queue.isEmpty()) {
State current = queue.poll();
Location location = current.location;
// already computed
if (dist[location.row][location.col] != null) {
continue;
}
// mark it
dist[location.row][location.col] = current;
// is there a tile that can reach current using a jump?
for (int delta = 0; delta < DELTA_ROW.length; delta++) {
int newRow = (location.row + DELTA_ROW[delta] + N) % N;
int newCol = (location.col + DELTA_COL[delta] + M) % M;
// already computed
if (dist[newRow][newCol] != null) {
continue;
}
// we can't stay on a wall
if (board[newRow][newCol] == WALL) {
continue;
}
// this tile is empty or a ladder
Location newLocation = new Location(newRow, newCol);
if (canJump(newLocation)) {
State newState = new State(newLocation, current.frames + 1, current.keysPressed + 1);
queue.add(newState);
}
}
// can we get current falling down?
int newRow = location.row - 1;
int newCol = location.col;
Location newLocation = new Location(newRow, newCol);
// we can't is we are at the top or the top is a wall
if (newRow < 0 || board[newRow][newCol] == WALL || dist[newRow][newCol] != null) {
continue;
}
if (canJump(newLocation)) {
continue;
}
State newState = new State(newLocation, current.frames + 1, current.keysPressed);
queue.add(newState);
}
// verify distances
for (State[] array : dist) for (State state : array) if (state != null && state.frames < state.keysPressed) {
out.println("distances to " + begin);
printMatrix(dist);
throw new RuntimeException("Invalid distances");
}
pathTreeCache.put(begin, dist);
return dist;
}
void printMatrix(Object[][] matrix) {
int len = 30;
for (int r = 0; r+1 < N; r++) {
for (int c = 0; c < M; c++) {
out.printf("%30s", matrix[r][c]);
}
out.println();
}
}
// compute the best path from begin to end
Map<Location, Map<Location, PathResult>> bestPathCache;
PathResult bestPath(Location begin, Location end) {
if (bestPathCache.containsKey(begin) && bestPathCache.get(begin).containsKey(end)) {
return bestPathCache.get(begin).get(end);
}
State[][] dist = pathTree(end);
if (dist[begin.row][begin.col] == null) {
// unreachable
return null;
}
// build lexicographically smallest path
String path = "";
Location current = begin;
int frames = 0;
int keysPressed = 0;
while (!current.equals(end)) {
Location location = current;
if (!canJump(location)) {
// is falling down
frames++;
int newRow = location.row + 1;
int newCol = location.col;
Location newLocation = new Location(newRow, newCol);
current = newLocation;
continue;
}
Location bestLocation = null;
char bestCommand = '\0';
keysPressed++;
frames++;
for (int delta = 0; delta < DELTA_ROW.length; delta++) {
int newRow = (location.row + DELTA_ROW[delta] + N) % N;
int newCol = (location.col + DELTA_COL[delta] + M) % M;
char newCmd = COMMANDS[delta];
if (board[newRow][newCol] != EMPTY && board[newRow][newCol] != LADDER)
continue;
if (dist[newRow][newCol] == null) {
continue;
}
Location newLocation = new Location(newRow, newCol);
boolean inBestPath = true;
inBestPath &= dist[begin.row][begin.col].frames - frames == dist[newRow][newCol].frames;
inBestPath &= dist[begin.row][begin.col].keysPressed - keysPressed == dist[newRow][newCol].keysPressed;
if (inBestPath) {
if (bestLocation == null || newCmd < bestCommand) {
bestLocation = newLocation;
bestCommand = newCmd;
}
}
}
if (bestLocation == null || bestCommand == '\0') {
throw new RuntimeException("Invalid state");
}
current = bestLocation;
path += bestCommand;
}
PathResult result = new PathResult(dist[begin.row][begin.col], path);
if (!bestPathCache.containsKey(begin)) {
bestPathCache.put(begin, new HashMap<>());
}
bestPathCache.get(begin).put(end, result);
return result;
}
boolean canJump(Location location) {
if (board[location.row][location.col] == WALL) {
return false;
}
boolean canJump = false;
canJump |= board[location.row][location.col] == LADDER; // on a ladder
canJump |= board[location.row + 1][location.col] == LADDER; // upside a ladder
canJump |= board[location.row + 1][location.col] == WALL; // upside a wall
return canJump;
}
final static int[] DELTA_ROW = { 1, -1, 0, 0 };
final static int[] DELTA_COL = { 0, 0, 1, -1 };
final static char[] COMMANDS = "SWDA".toCharArray();
// board data
int N, M;
Location start, exit;
List<Location> keys;
char[][] board;
//
final static char EMPTY = '.';
final static char WALL = '#';
final static char LADDER = 'H';
final static char START = 'S';
final static char EXIT = 'E';
final static char KEY = 'K';
}
class PathResult implements Comparable<PathResult> {
final State state;
final String path;
PathResult(State state, String path) {
this.state = state;
this.path = path;
}
public int compareTo(PathResult that) {
if (that == null) {
return -1;
}
if (state.compareTo(that.state) != 0) {
return state.compareTo(that.state);
}
return path.compareTo(that.path);
}
}
class State implements Comparable<State> {
final Location location;
int frames;
int keysPressed;
State(Location location, int frames, int keysPressed) {
this.location = location;
this.frames = frames;
this.keysPressed = keysPressed;
}
public String toString() {
return String.format("State[%s, %d, %d]", location, frames, keysPressed);
}
public int compareTo(State that) {
if (that == null) {
return -1;
}
if (frames != that.frames) {
return frames - that.frames;
}
if (keysPressed != that.keysPressed) {
return keysPressed - that.keysPressed;
}
return 0;
}
}
class Location {
final int row, col;
Location(int row, int col) {
this.row = row;
this.col = col;
}
public int hashCode() {
return (row << 10) | col;
}
public String toString() {
return String.format("Location(%d, %d)", row + 1, col + 1);
}
public boolean equals(Object o) {
Location other = (Location)o;
return row == other.row && col == other.col;
}
}