forked from mr-sergi/Hacktoberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KnightTour.java
55 lines (51 loc) · 1.33 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
package Graphs;
public class KnightTour {
static int N=8;
static boolean isSafe(int x,int y,int sol[][]) {
return (x>=0 && x<N && y>=0 && y<N && sol[x][y]==-1);
}
static void printSolution(int sol[][]) {
for (int x = 0; x < N; x++) {
for (int y = 0; y < N; y++)
System.out.print(sol[x][y] + " ");
System.out.println();
}
}
static boolean solve() {
int sol[][]=new int[N][N];
for (int x = 0; x < N; x++)
for (int y = 0; y < N; y++)
sol[x][y] = -1;
int xMove[] = {2, 1, -1, -2, -2, -1, 1, 2};
int yMove[] = {1, 2, 2, 1, -1, -2, -2, -1};
sol[0][0]=0;
if(!solveUtil(0,0,1,sol,xMove,yMove)) {
System.out.print("No Solution Exists");
return false;
}
else {
printSolution(sol);
return true;
}
}
static boolean solveUtil(int x,int y,int movei,int sol[][],int xMove[],int yMove[]) {
int nextx,nexty;
if(movei==N*N)
return true;
for(int i=0;i<8;i++) {
nextx=x+xMove[i];
nexty=y+yMove[i];
if(isSafe(nextx,nexty,sol)) {
sol[nextx][nexty]=movei;
if(solveUtil(nextx,nexty,movei+1,sol,xMove,yMove))
return true;
else
sol[nextx][nexty]=-1;
}
}
return false;
}
public static void main(String[] args) {
solve();
}
}