-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknightTour.java
58 lines (54 loc) · 1.49 KB
/
knightTour.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
import java.util.*;
class knightTour{
static int x[] ={-2,-2,-1,1,-1,1,2,2};
static int y[] ={-1,1,-2,-2,2,2,-1,1};
static void refresh(int[][] board,int n){
for(int i=0;i<n;i++)
Arrays.fill(board[i],-1);
}
static boolean isSafe(int[][] board,int r,int c){
int n = board.length;
if(r<0||r>=n||c<0||c>=n||board[r][c] != -1) return false;
return true;
}
static boolean solve(int[][] board,int theKnight,int n,int row,int col){
if(theKnight == n*n) return true;
else{
for(int i=0;i<8;i++){
if(isSafe(board,row+x[i],col+y[i])){
board[row+x[i]][col+y[i]] = theKnight;
if(solve(board,theKnight+1,n,row+x[i],col+y[i])) return true;
else{
board[row+x[i]][col+y[i]] = -1;
}
}
}
}
return false;
}
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println("Enter chess board size");
int n = s.nextInt();
int board[][] = new int[n][n];
refresh(board,n);
int f=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
board[i][j] = 0;
if(solve(board,1,n,i,j)){
f=1;
for(int k=0;k<n;k++)
{
for(int l=0;l<n;l++)
System.out.printf("%d ",board[k][l]);
System.out.println("");
}
}
System.out.println("");
refresh(board,n);
}
}
if(f==0) System.out.println("No solution");
}
}