-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolveKnightProblem.cpp
More file actions
62 lines (57 loc) · 1.31 KB
/
SolveKnightProblem.cpp
File metadata and controls
62 lines (57 loc) · 1.31 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
53
54
55
56
57
58
59
60
61
62
// Recursion [ Backtracking ]
// Solving Knightmove Problem
#include<iostream>
using namespace std;
const int D=8;
bool canPlace(int board[D][D], int n, int r, int c){
if(r>= 0 && r < n && c>=0 && c < n && board[r][c] == 0){
return true;
}
else{
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<<board[i][j]<<" ";
}
cout<<endl;
}
}
bool SolveKnightMove(int board[D][D], int n, int move_no, int curRow, int curCol){
if(move_no == n*n){
printBoard(board, n);
cout<<"-------------------\n";
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)== true){
board[nextRow][nextCol] = move_no + 1;
bool isSuccessful = SolveKnightMove(board, n, move_no + 1, nextRow, nextCol);
// if(isSuccessful==true){
// return true;
// }
board[nextRow][nextCol] = 0;
}
}
return false;
}
int main(){
int n;
cin>>n;
int board[D][D] = {0};
board[0][0] = 1;
bool ans = SolveKnightMove(board, n, 1, 0, 0);
// if(ans == true){
// printBoard(board, n);
// }
// else{
// cout<<"Your Board Can't be solve !!";
// }
return 0;
}