-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnightTour.java
More file actions
49 lines (45 loc) · 1.36 KB
/
KnightTour.java
File metadata and controls
49 lines (45 loc) · 1.36 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
public class KnightTour {
private int[][] board = new int[8][8];
private int[]rows = { -2, -1, 1, 2, 2, 1, -1, -2};
private int[] cols = { 1, 2, 2, 1, -1, -2, -2, -1};
public static int count = 0;
public Boolean kTour(int r, int c, int move){
if(move == 65){
return true;
}
if(move == 1){
board[r][c] = move;
move++;
}
for(int i = 0; i < board.length;i++){
if(validMove(r + rows[i],c + cols[i])){
count++;
board[r + rows[i]][c + cols[i]] = move;
if(kTour(r + rows[i],c + cols[i],move+1)){
return true;
}
else board[r + rows[i]][c + cols[i]] = 0;
}
}
return false;
}
private Boolean validMove(int x, int y){
if((x<board.length&& x >= 0)&& (y<board[0].length && y >= 0) && board[x][y] == 0 ){
return true;
}
else return false;
}
public static void main(String[] args){
KnightTour tour = new KnightTour();
tour.kTour(0,0,1);
for(int i = 0; i<8; i++)
{
for(int j = 0; j<8; j++)
{
System.out.print( " " + tour.board[i][j] + " ");
}
System.out.println();
}
System.out.println("time it ran " + count);
}
}