-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnightsTour.java
More file actions
188 lines (165 loc) · 5.99 KB
/
Copy pathKnightsTour.java
File metadata and controls
188 lines (165 loc) · 5.99 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
import java.util.*;
public class KnightsTour {
private static int gridSize = 8;
private static int attempt_count = 0;
private static int[] starting_coordinates;
private static Boolean is_closed_tour = true;
public enum DirectionDeltas {
NNW(new int[] { -2, -1 }), NNE(new int[] { -2, 1 }),
ENE(new int[] { -1, 2 }), ESE(new int[] { 1, 2 }),
SSE(new int[] { 2, 1 }), SSW(new int[] { 2, -1 }),
WSW(new int[] { 1, -2 }), WNW(new int[] { -1, -2 })
;
private final int[] delta;
DirectionDeltas(int[] delta) {
this.delta = delta;
}
public int[] getDeltas() {
return this.delta;
}
}
private static int[] findLastMove(int[][] matrix) {
int progress_count = 0;
int[] coordinates = new int[] { 0, 0 };
for(int i = 0; i < gridSize; i++) {
for(int j = 0; j < gridSize; j++) {
if(matrix[i][j] > progress_count) {
progress_count = matrix[i][j];
coordinates = new int[] { i, j };
}
}
}
return coordinates;
}
private static int[][] findLegalMoves(int[][] matrix, int[] last_coordinates) {
return findLegalMoves(matrix, last_coordinates, true);
}
private static int[][] findLegalMoves(int[][] matrix, int[] last_coordinates, Boolean peek_ahead) {
ArrayList<int[]> available_moves = new ArrayList<int[]>();
for (DirectionDeltas delta : DirectionDeltas.values()) {
int[] offset = delta.getDeltas();
int new_row = offset[0] + last_coordinates[0];
int new_col = offset[1] + last_coordinates[1];
if(new_row >= gridSize || new_col >= gridSize || new_row < 0 || new_col < 0)
continue;
// if(0 != matrix[new_row][new_col])
// continue;
int[] new_coordinates = new int[] { new_row, new_col };
available_moves.add(new_coordinates);
}
int results_length = (peek_ahead) ? 3 : 2;
int[][] results = new int[available_moves.size()][results_length];
for(int i = 0; i < available_moves.size(); i++) {
int[] move_coordinates = available_moves.get(i);
results[i][0] = move_coordinates[0];
results[i][1] = move_coordinates[1];
// third index reserved for sorting by possible moves available
if(peek_ahead) {
int[][] peeked_moves = findLegalMoves(matrix, new int[] { results[i][0], results[i][1] }, false);
results[i][2] = peeked_moves.length;
}
}
// @todo: implement Warnsdorff heuristic to sort available options
if(peek_ahead) {
// for(int i = 0; i < results.length; i++) {
// int[][] peeked_moves = findLegalMoves(matrix, new int[] { results[i][0], results[i][1] }, false);
// results[i][2] = peeked_moves.length;
// }
Arrays.sort(results, new Comparator<int[]>() {
@Override
public int compare(final int[] entry1, final int[] entry2) {
final int field1 = entry1[2];
final int field2 = entry2[2];
return Integer.compare(field1, field2);
}
});
int[][] new_results = new int[results.length][2];
for(int i = 0; i < results.length; i++) {
new_results[i][0] = results[i][0];
new_results[i][1] = results[i][1];
}
results = new_results;
}
return results;
}
private static Boolean isComplete(int[][] tour_progress, int[] last_coordinates) {
for(int i = 0; i < gridSize; i++) {
for(int j = 0; j < gridSize; j++) {
if(0 == tour_progress[i][j])
return false;
}
}
if(!is_closed_tour)
return true;
for (DirectionDeltas delta : DirectionDeltas.values()) {
int[] offset = delta.getDeltas();
int new_row = offset[0] + last_coordinates[0];
int new_col = offset[1] + last_coordinates[1];
int[] new_coordinates = new int[] { new_row, new_col };
if(Arrays.equals(new_coordinates, starting_coordinates))
return true;
}
return false;
}
private static int[][] move(int[][] tour_progress, int[] last_coordinates, int move_count) {
attempt_count += 1;
int[][] available_moves = findLegalMoves(tour_progress, last_coordinates);
for(int i = 0; i < available_moves.length; i++) {
int[][] progress_copy = new int[gridSize][gridSize];
for (int j = 0; j < gridSize; j++)
progress_copy[j] = Arrays.copyOf(tour_progress[j], tour_progress[j].length);
int new_row = available_moves[i][0];
int new_col = available_moves[i][1];
if(0 != progress_copy[new_row][new_col])
continue;
progress_copy[new_row][new_col] = move_count + 1;
if(isComplete(progress_copy, available_moves[i]))
return progress_copy;
int[][] attempt = move(progress_copy, available_moves[i], move_count + 1);
int[] latest_coordinates = findLastMove(attempt);
if(isComplete(attempt, latest_coordinates))
return attempt;
}
return new int[gridSize][gridSize];
}
private static void printGrid(int[][] matrix) {
for(int i = 0; i < gridSize; i++) {
if(i == 0)
System.out.print(" ");
System.out.print(" " + (i+1) + " ");
}
System.out.print("\n");
for(int i = 0; i < gridSize; i++) {
System.out.print((i+1) + " ");
for(int j = 0; j < gridSize; j++) {
String mark = (matrix[i][j] != 0) ? String.valueOf(matrix[i][j]) : " ";
if(matrix[i][j] < 10)
mark = " " + mark;
System.out.print("[" + mark + "]");
}
System.out.println("");
}
}
private static int promptPlacement(Scanner sc, String type) {
return promptPlacement(sc, type, false);
}
private static int promptPlacement(Scanner sc, String type, Boolean try_again) {
if(try_again)
System.out.println("Sorry, that is not a valid entry! Please try again!");
System.out.print("\nPlease enter which " + type + " to place the knight: ");
int index = sc.nextInt();
return (index <= 0 || index > gridSize) ? promptPlacement(sc, type, true) : index;
}
public static void main(String []args) {
int[][] matrix = new int[gridSize][gridSize];
printGrid(matrix);
Scanner sc = new Scanner(System.in);
int user_row = promptPlacement(sc, "row") - 1;
int user_col = promptPlacement(sc, "column") - 1;
starting_coordinates = new int[] { user_row, user_col };
matrix[user_row][user_col] = 1;
int[][] complete_matrix = move(matrix, starting_coordinates, 1);
printGrid(complete_matrix);
System.out.println("attempted " + attempt_count + " moves.");
}
}