-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathknight_tour.cpp
More file actions
60 lines (55 loc) · 766 Bytes
/
knight_tour.cpp
File metadata and controls
60 lines (55 loc) · 766 Bytes
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
#include<bits/stdc++.h>
using namespace std;
int q[8]={ 2, 1, -1, -2, -2, -1, 1, 2 };
int w[8]={ 1, 2, 2, 1, -1, -2, -2, -1 };
int a[8][8];
int safe(int i,int j)
{
return i>=0 && j>=0 && i<8 && j<8 && a[i][j]==-1;
}
int foo(int i,int j,int val)
{
if(val==64)
return 1;
int k;
for(k=0;k<8;k++)
{
if(safe(q[k]+i,w[k]+j))
{
a[q[k]+i][w[k]+j]=val;
if(foo(q[k]+i,w[k]+j,val+1))
return 1;
else
a[q[k]+i][w[k]+j]=-1;
}
}
return 0;
}
int print_sol()
{
int i,j;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
}
int main()
{
int i,j;
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
a[i][j]=-1;
}
}
a[0][0]=0;
if(foo(0,0,1))
print_sol();
else
cout<<"sol dont exist"<<endl;
}