-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknightsTour.java
68 lines (56 loc) · 2.19 KB
/
knightsTour.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
package Backtracking;
class knightsTour {
// N x N chessboard
public static final int N = 5;
// Below arrays details all 8 possible movements for a knight.
// Don't change the sequence of below arrays
public static final int row[] = { 2, 1, -1, -2, -2, -1, 1, 2 , 2 };
public static final int col[] = { 1, 2, 2, 1, -1, -2, -2, -1, 1 };
// Check if (x, y) is valid chess board coordinates
// Note that a knight cannot go out of the chessboard
private static boolean isValid(int x, int y) {
if (x < 0 || y < 0 || x >= N || y >= N)
return false;
return true;
}
private static void print(int[][] visited) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(visited[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
// Recursive function to perform the Knight's tour using backtracking
public static void knightTour(int visited[][], int x, int y, int pos) {
// mark current square as visited
visited[x][y] = pos;
// if all squares are visited, print the solution
if (pos >= N*N) {
print(visited);
// backtrack before returning
visited[x][y] = 0;
return;
}
// check for all 8 possible movements for a knight
// and recurse for each valid movement
for (int k = 0; k < 8; k++) {
// Get the new position of Knight from current
// position on chessboard
int newX = x + row[k];
int newY = y + col[k];
// if new position is a valid and not visited yet
if (isValid(newX, newY) && visited[newX][newY] == 0)
knightTour(visited, newX, newY, pos + 1);
}
// backtrack from current square and remove it from current path
visited[x][y] = 0;
}
public static void main(String[] args) {
int visited[][] = new int[N][N];
int pos = 1;
// start knight tour from corner square (0, 0)
knightTour(visited, 0, 0, pos);
}
}