-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathKhushi_Tulsiyan.cpp
53 lines (44 loc) · 966 Bytes
/
Khushi_Tulsiyan.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;
bool isSafe(int chess[][10] , int row , int col ,int n)
{
//for checking at the top
for(int i = row - 1 , j = col; i >= 0 ; i--){
if(chess[i][j]==1){
return false;
}
}
//for checking at left diagonal
for(int i = row - 1 , j = col - 1; i >= 0&&j >= 0 ; i--,j--){
if(chess[i][j]==1){
return false;
}
}
// for checking at the right diagonal
for(int i = row - 1 , j = col + 1; i >= 0 && j < n ; i--,j++){
if(chess[i][j]==1){
return false;
}
}
return true;
}
void printQueen(int chess[][10] , string ans , int row ,int n){
if(row == n){
cout<<ans<<"."<<endl;
return;
}
for(int col = 0 ; col < n ; col++){
if(isSafe(chess, row , col , n)){
chess[row][col] = 1;
printQueen(chess,ans + to_string(row) + "-" +to_string(col)+ ", ",row+1,n);
chess[row][col] = 0;
}
}
}
int main(){
int n;
cin>>n;
int chess[10][10] = {0};
printQueen(chess, "" ,0 , n);
}