Skip to content

Commit ec127df

Browse files
authored
Add Knight's Tour (TheAlgorithms#2701)
1 parent 4f13178 commit ec127df

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed

Backtracking/KnightsTour.java

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package Backtracking;
2+
3+
import java.util.*;
4+
5+
/*
6+
* Problem Statement: -
7+
8+
Given a N*N board with the Knight placed on the first block of an empty board. Moving according to the rules of
9+
chess knight must visit each square exactly once. Print the order of each cell in which they are visited.
10+
11+
Example: -
12+
13+
Input : N = 8
14+
15+
Output:
16+
0 59 38 33 30 17 8 63
17+
37 34 31 60 9 62 29 16
18+
58 1 36 39 32 27 18 7
19+
35 48 41 26 61 10 15 28
20+
42 57 2 49 40 23 6 19
21+
47 50 45 54 25 20 11 14
22+
56 43 52 3 22 13 24 5
23+
51 46 55 44 53 4 21 12
24+
25+
*/
26+
27+
public class KnightsTour {
28+
private final static int base = 12;
29+
private final static int[][] moves = {{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}}; // Possible moves by knight on chess
30+
private static int[][] grid; // chess grid
31+
private static int total; // total squares in chess
32+
33+
public static void main(String[] args) {
34+
grid = new int[base][base];
35+
total = (base - 4) * (base - 4);
36+
37+
for (int r = 0; r < base; r++)
38+
for (int c = 0; c < base; c++)
39+
if (r < 2 || r > base - 3 || c < 2 || c > base - 3)
40+
grid[r][c] = -1;
41+
42+
int row = 2 + (int) (Math.random() * (base - 4));
43+
int col = 2 + (int) (Math.random() * (base - 4));
44+
45+
grid[row][col] = 1;
46+
47+
if (solve(row, col, 2))
48+
printResult();
49+
else System.out.println("no result");
50+
51+
}
52+
53+
// Return True when solvable
54+
private static boolean solve(int row, int column, int count) {
55+
if (count > total)
56+
return true;
57+
58+
List<int[]> neighbor = neighbors(row, column);
59+
60+
if (neighbor.isEmpty() && count != total)
61+
return false;
62+
63+
Collections.sort(neighbor, new Comparator<int[]>() {
64+
public int compare(int[] a, int[] b) {
65+
return a[2] - b[2];
66+
}
67+
});
68+
69+
for (int[] nb : neighbor) {
70+
row = nb[0];
71+
column = nb[1];
72+
grid[row][column] = count;
73+
if (!orphanDetected(count, row, column) && solve(row, column, count + 1))
74+
return true;
75+
grid[row][column] = 0;
76+
}
77+
78+
return false;
79+
}
80+
81+
// Returns List of neighbours
82+
private static List<int[]> neighbors(int row, int column) {
83+
List<int[]> neighbour = new ArrayList<>();
84+
85+
for (int[] m : moves) {
86+
int x = m[0];
87+
int y = m[1];
88+
if (grid[row + y][column + x] == 0) {
89+
int num = countNeighbors(row + y, column + x);
90+
neighbour.add(new int[]{row + y, column + x, num});
91+
}
92+
}
93+
return neighbour;
94+
}
95+
96+
// Returns the total count of neighbors
97+
private static int countNeighbors(int row, int column) {
98+
int num = 0;
99+
for (int[] m : moves)
100+
if (grid[row + m[1]][column + m[0]] == 0)
101+
num++;
102+
return num;
103+
}
104+
105+
// Returns true if it is orphan
106+
private static boolean orphanDetected(int count, int row, int column) {
107+
if (count < total - 1) {
108+
List<int[]> neighbor = neighbors(row, column);
109+
for (int[] nb : neighbor)
110+
if (countNeighbors(nb[0], nb[1]) == 0)
111+
return true;
112+
}
113+
return false;
114+
}
115+
116+
// Prints the result grid
117+
private static void printResult() {
118+
for (int[] row : grid) {
119+
for (int i : row) {
120+
if (i == -1) continue;
121+
System.out.printf("%2d ", i);
122+
}
123+
System.out.println();
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)