-
Notifications
You must be signed in to change notification settings - Fork 0
/
KnightBoard.java
607 lines (540 loc) · 18.5 KB
/
KnightBoard.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/**
*
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
/**
* A knights tour:
*
* Selecting a series of moves for a knight such that each square is visited
* exactly once. If the knight ends on a square that is reachable by a knight's
* move from the beginning square, the tour is closed, otherwise it is open.
*
* <p>
* Note:
* <ul>
* <li>The board dimensions do not have to be square.</li>
* <li>The starting square counts as visited.</li>
* <li>You do not have to come back to where you started. Closed tours take much
* longer to find (potentially)</li>
* </ul>
* </p>
*
* <p>
* Not all board sizes have solutions. A 2 by N board has no closed or open
* tour. From Wikipedia: Any m �� n board with m �� n, a closed knight's tour
* is always possible unless one or more of these three conditions are met:
* <ol>
* <li>m and n are both odd</li>
* <li>m = 1, 2, or 4</li>
* <li>m = 3 and n = 4, 6, or 8.</li>
* </ol>
* It follows that if a closed tour is possible, then an open tour is also
* possible.
* </p>
*/
public class KnightBoard {
private int[][] board;
int[][] moves;
private boolean movesFilled = false;
private int rows;
private int cols;
private int maxLevel;
/**
* A helper class to represent a coordinate on the KnightBoard
*
* @author ruosh
*
*/
/*
* private class Cor implements Comparable<Cor> { private final int x; private
* final int y; private int validMoves;
*
* public Cor(int x, int y) { this.x = x; this.y = y; }
*
* public Cor(int x, int y, boolean b) { this.x = x; this.y = y; if (b) {
* this.validMoves = moves[x][y]; } }
*
* public int getX() { return this.x; }
*
* public int getY() { return this.y; }
*
* public int getMoves() { return this.validMoves; }
*
* public void setMoves(int moves) { this.validMoves = moves; }
*
* @Override public int compareTo(Cor thatCoor) { return
* Integer.valueOf(this.validMoves).compareTo(Integer.valueOf(thatCoor.
* validMoves)); } }
*/
private static final int[] drow = new int[] { 1, -1, 1, -1, 2, -2, 2, -2 };
private static final int[] dcol = new int[] { 2, 2, -2, -2, 1, 1, -1, -1 };
class Cors {
private final int row;
private final int col;
private int[] order = new int[] { 0, 1, 2, 3, 4, 5, 6, 7 };
public Cors(int row, int col) {
this.row = row;
this.col = col;
}
public int getRow(int i) {
if (i < 0 || i > 7) {
throw new IllegalArgumentException("Cor must be from 0 - 7");
}
return row + drow[order[i]];
}
public int getCol(int i) {
if (i < 0 || i > 7) {
throw new IllegalArgumentException("Cor must be from 0 - 7");
}
return col + dcol[order[i]];
}
// /* Not Used. Manual sorting instead.
public void sortMoveOrder() {
// System.out.println("Sorting...");
Integer[] o = new Integer[] { 0, 1, 2, 3, 4, 5, 6, 7 };
Arrays.sort(o, new movesComparator());
int[] order = new int[8];
for (int i = 0; i < o.length; i++) {
// auto-unboxing of Integer
order[i] = o[i];
}
this.order = order;
// System.out.println("End Sorting");
}
private class movesComparator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
int row1 = Cors.this.getRow(o1);
int col1 = Cors.this.getCol(o1);
int row2 = Cors.this.getRow(o2);
int col2 = Cors.this.getCol(o2);
if (KnightBoard.this.isCorInBound(row1, col1) && KnightBoard.this.isCorInBound(row2, col2)) {
return Integer.valueOf(KnightBoard.this.moves[row1][col1])
.compareTo(Integer.valueOf(KnightBoard.this.moves[row2][col2]));
} else {
return 0;
}
}
}
// */
}
/**
* Initialize the board to the correct size and make them all 0's
*
* @throws IllegalArgumentException when either parameter is negative.
*/
public KnightBoard(int startingRows, int startingCols) {
if (startingRows <= 0 || startingCols <= 0) {
throw new IllegalArgumentException("Size of the board must be positive");
}
this.board = new int[startingRows][startingCols];
this.rows = startingRows;
this.cols = startingCols;
this.maxLevel = rows * cols + 1;
}
private boolean isCorInBound(int row, int col) {
return !(row >= rows || col >= cols || row < 0 || col < 0);
}
private void clearBoard() {
board = new int[rows][cols];
}
private void checkBoard() {
if (!Arrays.deepEquals(board, new int[rows][cols])) {
throw new IllegalStateException("Board must be empty");
}
}
private void checkInputs(int row, int col) {
if (!isCorInBound(row, col)) {
throw new IllegalArgumentException("Board dimension must be nonnegative and in bounds");
}
}
private boolean addKnight(int row, int col, int level) {
if (!isCorInBound(row, col)) {
return false;
} else if (board[row][col] != 0) {
return false;
} else {
board[row][col] = level;
return true;
}
}
private boolean removeKnight(int row, int col, int level) {
if (!isCorInBound(row, col)) {
return false;
} else if (board[row][col] != level) {
return false;
} else {
board[row][col] = 0;
return true;
}
}
private boolean addKnightWithMoves(int row, int col, int level) {
if (!this.movesFilled) {
throw new IllegalStateException("Moves board not filled");
}
if (!addKnight(row, col, level))
return false;
else {
Cors cors = new Cors(row, col);
for (int k = 0, i, j; k < 8; k++) {
i = cors.getRow(k);
j = cors.getCol(k);
if (isCorInBound(i, j)) {
this.moves[i][j]--;
}
}
return true;
}
}
private boolean removeKnightWithMoves(int row, int col, int level) {
if (!this.movesFilled) {
throw new IllegalStateException("Moves board not filled");
}
if (!removeKnight(row, col, level))
return false;
else {
Cors cors = new Cors(row, col);
for (int k = 0, i, j; k < 8; k++) {
i = cors.getRow(k);
j = cors.getCol(k);
if (isCorInBound(i, j)) {
this.moves[i][j]++;
}
}
return true;
}
}
// TODO: Optimize
private void fillMoves() {
if (this.moves == null) {
this.moves = new int[rows][cols];
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
Cors cors = new Cors(i, j);
int moves = 0;
for (int k = 0; k < 8; k++) {
int row = cors.getRow(k);
int col = cors.getCol(k);
if (isCorInBound(row, col)) {
moves++;
}
}
this.moves[i][j] = moves;
}
}
this.movesFilled = true;
}
/**
* Modifies the board by labeling the moves from 1 (at startingRow,startingCol)
* up to the area of the board in proper knight move steps.
*
* @throws IllegalStateException when the board contains non-zero values.
* @throws IllegalArgumentException when either parameter is negative or out of
* bounds.
* @param startingRow
* @param startingCol
* @returns true when the board is solvable from the specified starting position
*/
public boolean slowSolve(int startingRow, int startingCol) {
checkBoard();
checkInputs(startingRow, startingCol);
return nextMove(startingRow, startingCol, 1);
}
private boolean nextMove(int row, int col, int level) {
// if (level == maxLevel) {
// return true;
// } else {
// if (!addKnight(row, col, level)) {
// return false;
// }
//
// int nextLevel = level + 1;
// int nextRow, nextCol;
//
// Cors cors = new Cors(row, col);
// for (int i = 0; i < 8; i++) {
//
//// System.out.println(this);
// nextRow = cors.getRow(i);
// nextCol = cors.getCol(i);
//// System.out.println("cor: " + nextRow + "," + nextCol + "at level" + level);
// if (nextMove(nextRow, nextCol, nextLevel)) {
// return true;
// } else {
// removeKnight(nextRow, nextCol, nextLevel);
// }
// }
// return false;
// }
if (!addKnight(row, col, level)) {
return false;
}
int nextLevel = level + 1;
if (nextLevel < maxLevel) {
Cors cors = new Cors(row, col);
int nextRow, nextCol;
for (int i = 0; i < 8; i++) {
nextRow = cors.getRow(i);
nextCol = cors.getCol(i);
// System.out.println("cor: " + nextRow + "," + nextCol + "at level" + level);
// System.out.println(solutions);
// System.out.println(this);
if (nextLevel < maxLevel) {
if (nextMove(nextRow, nextCol, nextLevel)) {
return true; // ignore false value;
}
}
}
} else {
return true;
}
// finished with all possibilities, so this level is not possible at this
// position
removeKnight(row, col, level);
return false;
}
public boolean solve(int row, int col) {
checkBoard();
checkInputs(row, col);
fillMoves();
return fastNextMove1(row, col, 1);
}
public boolean fastNextMove(int row, int col, int level) {
if (!addKnightWithMoves(row, col, level)) {
return false;
}
int nextLevel = level + 1;
if (nextLevel < maxLevel) {
Cors cors = new Cors(row, col);
cors.sortMoveOrder();
int nextRow, nextCol;
for (int i = 0; i < 8; i++) {
nextRow = cors.getRow(i);
nextCol = cors.getCol(i);
// System.out.println("cor: " + nextRow + "," + nextCol + "at level" + level);
// System.out.println(solutions);
// System.out.println(this);
if (nextLevel < maxLevel) {
if (fastNextMove(nextRow, nextCol, nextLevel)) {
return true; // ignore false value;
}
}
}
} else {
return true;
}
// finished with all possibilities, so this level is not possible at this
// position
removeKnightWithMoves(row, col, level);
return false;
}
public boolean fastNextMove1(int row, int col, int level) {
if (!addKnightWithMoves(row, col, level)) {
return false;
}
int nextLevel = level + 1;
if (nextLevel < maxLevel) {
Cors cors = new Cors(row, col);
List<int[]> order = new ArrayList<>();
for (int i = 0; i < 8; i++) {
int corRow = cors.getRow(i);
int corCol = cors.getCol(i);
if (isCorInBound(corRow, corCol)) {
int [] element = new int[] {i, moves[corRow][corCol]};
if (order.size() == 0) {
order.add(element);
} else {
// insertion sort
for (int j = 0, s = order.size(); j < s; j++) {
int nextMoveCount = order.get(j)[1];
int curMoveCount = element[1];
//Stable vs. non-stable sort comparison
//1894509645
//6704765
if (curMoveCount < nextMoveCount || j == s - 1) {
order.add(j, element);
break;
}
}
}
} else {
// cor is not in bound, ignore it
continue;
}
}
// cors.sortMoveOrder();
int nextRow, nextCol;
// for (int i = 0; i < 8; i++) {
// nextRow = cors.getRow(i);
// nextCol = cors.getCol(i);
// // System.out.println("cor: " + nextRow + "," + nextCol + "at level" + level);
// // System.out.println(solutions);
//// System.out.println(this);
// if (nextLevel < maxLevel) {
// if (fastNextMove(nextRow, nextCol, nextLevel)) {
// return true; // ignore false value;
// }
// }
// }
for (int[] pos : order) {
int i = pos[0];
nextRow = cors.getRow(i);
nextCol = cors.getCol(i);
// System.out.println("cor: " + nextRow + "," + nextCol + "at level" + level);
// System.out.println(solutions);
// System.out.println(this);
if (nextLevel < maxLevel) {
if (fastNextMove(nextRow, nextCol, nextLevel)) {
return true; // ignore false value;
}
}
}
} else {
return true;
}
// finished with all possibilities, so this level is not possible at this
// position
removeKnightWithMoves(row, col, level);
return false;
}
/**
* Count the number of solutions.
*
* @throws IllegalStateException when the board contains non-zero values.
* @throws IllegalArgumentException when either parameter is negative or out of
* bounds.
* @param startingRow
* @param startingCol
* @returns the number of solutions from the starting position specified
*/
public int countSolutions(int startingRow, int startingCol) {
checkBoard();
checkInputs(startingRow, startingCol);
return subSolutions(startingRow, startingCol, 1);
}
// TODO: Optimize
public int countAllSolutions() {
int solutions = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
clearBoard();
solutions += countSolutions(i, j);
}
}
return solutions;
}
private int subSolutions(int row, int col, int level) {
if (!addKnight(row, col, level)) {
return 0;
}
int nextLevel = level + 1;
int solutions = 0;
if (nextLevel < maxLevel) {
Cors cors = new Cors(row, col);
int nextRow, nextCol;
for (int i = 0; i < 8; i++) {
nextRow = cors.getRow(i);
nextCol = cors.getCol(i);
// System.out.println("cor: " + nextRow + "," + nextCol + "at level" + level);
// System.out.println(solutions);
// System.out.println(this);
if (nextLevel < maxLevel) {
solutions += subSolutions(nextRow, nextCol, nextLevel);
}
}
} else {
solutions += 1;
}
removeKnight(row, col, level);
return solutions;
}
/**
* (THESE ARE NOT VALID SOLUTIONS, They JUST TO DEMONSTRATE FORMAT)
* <p>
* Single spaces between numbers, but leading spaces on single digit numbers:
*
* <pre>
1 2 5
3 4 6
7 8 9
* </pre>
*
* Which is equivalant to: " 1 2 5\n 3 4 6\n 7 8 9\n"
* </p>
* <p>
* When there are two digit numbers (rows*cols >= 10) Put a leading space in
* front of single digit numbers: (spaces replaced with _ to show the
* difference)
*
* <pre>
_1 _2 15 _6
_3 _4 _7 11
_8 _9 10 12
13 14 _5 16
* </pre>
*
* So it looks like this:
*
* <pre>
1 2 15 6
3 4 7 11
8 9 10 12
13 14 5 16
* </pre>
* </p>
* Blank boards display 0's as underscores.
*
*/
@Override
public String toString() {
return toString(board);
}
private String toString(int[][] board) {
StringBuilder s = new StringBuilder();
String format;
if (rows * cols > 10) {
// need string padding
int length = (int) Math.floor(Math.log10(rows * cols)) + 1;
format = "%" + length + "d";
} else {
format = "%d";
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
s.append(String.format(format, board[i][j])).append(" ");
}
s.delete(s.length(), s.length()).append("\n");
}
return s.toString();
}
public static void main(String... args) {
int rows, cols;
if (args.length != 0) {
rows = Integer.parseInt(args[0]);
cols = Integer.parseInt(args[1]);
} else {
rows = 25;
cols = 25;
}
long time;
KnightBoard kb = new KnightBoard(rows, cols);
System.out.println(kb);
time = System.nanoTime();
// System.out.println(kb.solve(0, 0));
// System.out.println(kb.fastSolve(0, 0));
System.out.println(System.nanoTime() - time);
kb.clearBoard();
time = System.nanoTime();
// System.out.println(kb.solve(0, 0));
System.out.println(kb.solve(1, 3));
System.out.println(System.nanoTime() - time);
// System.out.println(kb.countAllSolutions());
System.out.println(kb);
// kb.fillMoves();
// System.out.println(kb.toString(kb.moves));
}
}