-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnightsTour.java
More file actions
67 lines (53 loc) · 1.78 KB
/
KnightsTour.java
File metadata and controls
67 lines (53 loc) · 1.78 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
package BackTracking;
import java.util.ArrayList;
import java.util.List;
public class KnightsTour {
private static final int N = 8;
private static final int[][] board = new int[N][N];
private static int moveCount = 0;
public static void main(String[] args) {
int startRow = 0, startCol = 0;
if (solve(startRow, startCol, 1)) {
System.out.println("Number of moves: " + moveCount);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(board[i][j] + " ");
}
System.out.println();
}
} else {
System.out.println("No solution found.");
}
}
private static List<int[]> validMoves(int row, int col) {
List<int[]> moves = new ArrayList<>();
int[] rowMoves = {-2, -1, 1, 2, 2, 1, -1, -2};
int[] colMoves = {1, 2, 2, 1, -1, -2, -2, -1};
for (int i = 0; i < 8; i++) {
int newRow = row + rowMoves[i];
int newCol = col + colMoves[i];
if (newRow >= 0 && newRow < N && newCol >= 0 && newCol < N && board[newRow][newCol] == 0) {
moves.add(new int[]{newRow, newCol});
}
}
return moves;
}
private static boolean solve(int row, int col, int moveNum) {
board[row][col] = moveNum;
moveCount++;
if (moveNum == N * N) {
return true;
}
List<int[]> moves = validMoves(row, col);
for (int[] move : moves) {
int newRow = move[0];
int newCol = move[1];
if (solve(newRow, newCol, moveNum + 1)) {
return true;
}
}
board[row][col] = 0;
moveCount--;
return false;
}
}