-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKnight
45 lines (45 loc) · 1.06 KB
/
Knight
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
#include<bits/stdc++.h>
using namespace std;
int d=8;
bool canPlace(int board[][8],int n,int curRow,int curCol){
if(board[curRow][curCol]==0 && curRow>=0 && curRow<n && curCol>=0 && curCol<n)
return true;
else return false;
}
bool solve(int board[][8],int n,int move_no,int curRow,int curCol){
if(move_no==64){
return true;
}
int rowDir[]={+2,+1,-1,-2,-2,-1,+1,+2};
int colDir[]={+1,+2,+2,+1,-1,-2,-2,-1};
for(int curDir=0;curDir<8;++curDir){
int nextRow=curRow+rowDir[curDir];
int nextCol=curCol+colDir[curDir];
if(canPlace(board,n,nextRow,nextCol)){
board[nextRow][nextCol]=move_no+1;
bool canSuccess=solve(board,n,move_no+1,nextRow,nextCol);
if(canSuccess) return true;
else board[nextRow][nextCol]=0;
}
}
return false;
}
printBoard(int board[][8],int n){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cout<<board[i][j]<<"\t";
}
cout<<endl;
}
}
int main(){
int board[8][8]={0};
board[0][0]=1;
bool ans=solve(board,8,1,0,0);
if(ans){
printBoard(board,8);
}
else
cout<<"Can not be done"<<endl;
return 0;
}