-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathknightproblem.cpp
59 lines (51 loc) · 1.25 KB
/
knightproblem.cpp
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
#include <iostream>
#include <iomanip>
using namespace std;
const int D =8;
bool canPlace(int board[D][D],int n,int r,int c){
return
r >= 0 && r <n &&
c >=0 && c < n &&
board[r][c]==0;
}
bool solveKnight(int board[D][D],int n,int move_no,int curRow,int curCol){
if(move_no == n*n){
return true;
}
int row_dir[]= {+2,+1,-1,-2,-2,-1,+1,+2};
int col_dir[]= {+1,+2,+2,+1,-1,-2,-2,-1};
for(int cur_dir =0;cur_dir<8;cur_dir++){
int nextRow = curRow + row_dir[cur_dir];
int nextCol = curCol + col_dir[cur_dir];
if(canPlace(board,n,nextRow,nextCol) == true){
board[nextRow][nextCol]= move_no + 1;
bool isSucess= solveKnight(board,n,move_no+1,nextRow,nextCol);
if(isSucess ==true){
return true;
}else {
board[nextRow][nextCol]=0;
}
}
}
return false;
}
void printBoard(int board[D][D],int n){
for(int i=0;i<n;i++){
for(int j=0;j <n;j++){
cout << setw(3) << board[i][j] << " ";
}
cout << endl;
}
}
int main(){
int board[D][D] = {0};
int n;
cin >>n;
board[0][0]=1;
bool ans = solveKnight(board,n,1,0,0);
if(ans == true ){
printBoard(board,n);
}else {
cout << "sorry!!!";
}
}