-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknightstour_day7.cpp
More file actions
52 lines (45 loc) · 1.01 KB
/
knightstour_day7.cpp
File metadata and controls
52 lines (45 loc) · 1.01 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
50
51
52
#include<bits/stdc++.h>
using namespace std;
#define N 8
int chess[N][N] = {0};
int moveX[] = {2,1,-1,-2,-2,-1,1,2};
int moveY[] = {1,2,2,1,-1,-2,-2,-1};
int SafeMove(int x, int y){
return (x>=0 && x<N && y>=0 && y<N && chess[x][y]==0);
}
void printChess(){
for(int i=0; i<N; ++i){
for(int j=0; j<N; ++j)
cout<<chess[i][j]<<"\t";
cout<<endl;
}
}
bool knightsTour(int x, int y, int ithMove){
int nextX, nextY;
if(ithMove==N*N) return true;
for(int i=0; i<8; ++i){
nextX = x + moveX[i];
nextY = y + moveY[i];
if(SafeMove(nextX, nextY)){
chess[nextX][nextY] = ithMove;
//if recursively tour completed then return true
if(knightsTour(nextX, nextY, ithMove+1))
return true;
//else undo the move and try other moves
else
chess[nextX][nextY] = 0;
}
}
return false;
}
int main(){
//initiate backtracking algorithm
chess[0][0]=1;
printChess();
if(knightsTour(0,0,1)){
cout<<"The knights tour to cover all of the chess is\n";
printChess();
}
else
cout<<"No such tour possible\n";
}