-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathKnightTour.java
71 lines (67 loc) · 1.41 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
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.lang.*;
import java.io.*;
class KnightTour
{
static int n;
public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter n");
n=Integer.parseInt(br.readLine());
int sol[][]=new int [n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
sol[i][j]=-1;
}
}
solveTour(sol);
}
public static int isSafe(int x,int y,int sol[][])
{
if(x>=0&&x<n&&y>=0&&y<n&&sol[x][y]==-1)
return 1;
else
return -1;
}
public static void solveTour(int sol[][])
{
sol[0][0]=0; //starting point
int xMove[] = {2, 1, -1, -2, -2, -1, 1, 2};
int yMove[] = {1, 2, 2, 1, -1, -2, -2, -1};
if(solveTourNextMove(0,0,1,sol,xMove,yMove)==1)
{
System.out.println();
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
System.out.print(sol[i][j]+"\t");
}
System.out.println();
}
}
else
System.out.println("Solution doesn't exist");
}
public static int solveTourNextMove(int x,int y, int count,int sol[][], int xMove[],int yMove[])
{
if (count == n*n)
return 1;
int x1,y1;
for(int i=0;i<8;i++)//8 for 8 moves of a knight
{
x1=x+xMove[i];
y1=y+yMove[i];
if(isSafe(x1,y1,sol)==1)
{
sol[x1][y1]=count;
if(solveTourNextMove(x1,y1,count+1,sol,xMove,yMove)==1)
return 1;
sol[x1][y1]=-1;
}
}
return -1;
}
}