-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathKhushi2.cpp
53 lines (45 loc) · 1.03 KB
/
Khushi2.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
#include<iostream>
#include<string>
using namespace std;
void displayboard(int chess[][10],int n){
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < n ; j++){
cout<<chess[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
void printQueen(int chess[][10] , int r , int c ,int move,int n){
if(r < 0 || c < 0 || r >= n || c >= n || chess[r][c] > 0){
return;
}
else if(move == n*n){
chess[r][c] = move;
displayboard(chess, n);
chess[r][c] = 0;
return;
}
chess[r][c] = move;
printQueen(chess , r-2,c+1,move+1,n);
printQueen(chess , r-1,c+2,move+1,n);
printQueen(chess , r+1,c+2,move+1,n);
printQueen(chess , r+2,c+1,move+1,n);
printQueen(chess , r+2,c-1,move+1,n);
printQueen(chess , r+1,c-2,move+1,n);
printQueen(chess , r-1,c-2,move+1,n);
printQueen(chess , r-2,c-1,move+1,n);
chess[r][c] = 0;
}
int main(){
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
int n,r,c;
cin>>n;
cin>>r;
cin>>c;
int chess[10][10] = {0};
printQueen(chess,r ,c ,1 , n);
}