Skip to content

Commit 32272a7

Browse files
authored
Add files via upload
1 parent aa38c8c commit 32272a7

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed

11 Backtracking/n_queen_all_ways.cpp

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
#include <iostream>
3+
using namespace std;
4+
5+
bool canPlace(int board[][20],int n,int x,int y){
6+
7+
//Column
8+
for(int k=0;k<x;k++){
9+
if(board[k][y]==1){
10+
return false;
11+
}
12+
}
13+
//Left Diag
14+
int i = x;
15+
int j = y;
16+
while(i>=0 and j>=0){
17+
if(board[i][j]==1){
18+
return false;
19+
}
20+
i--; j--;
21+
}
22+
23+
//Right Diag
24+
i = x;
25+
j = y;
26+
while(i>=0 and j<n){
27+
if(board[i][j]==1){
28+
return false;
29+
}
30+
i--; j++;
31+
}
32+
return true;
33+
}
34+
35+
void printBoard(int n,int board[][20]){
36+
37+
for(int i=0;i<n;i++){
38+
for(int j=0;j<n;j++){
39+
cout<< board[i][j]<<" ";
40+
}
41+
cout<<endl;
42+
}
43+
cout <<endl;
44+
}
45+
//Return type
46+
int solveNQueen(int n,int board[][20],int i){
47+
//base case
48+
if(i==n){
49+
//Print the board
50+
printBoard(n,board);
51+
return 1;
52+
}
53+
54+
55+
// rec case
56+
// try to place a queen in every row
57+
int ways = 0;
58+
for(int j=0;j<n;j++){
59+
//whether the current i,j is safe or not
60+
if(canPlace(board,n,i,j)){
61+
board[i][j] = 1;
62+
ways += solveNQueen(n,board,i+1);
63+
//backtrack
64+
board[i][j] = 0;
65+
}
66+
}
67+
return ways;
68+
}
69+
70+
int main() {
71+
// your code goes here
72+
int board[20][20] = {0};
73+
int n;
74+
cin>>n;
75+
76+
solveNQueen(n,board,0);
77+
78+
79+
80+
return 0;
81+
}
82+

11 Backtracking/n_queen_print_one.cpp

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
#include <iostream>
3+
using namespace std;
4+
5+
bool canPlace(int board[][20],int n,int x,int y){
6+
7+
//Column
8+
for(int k=0;k<x;k++){
9+
if(board[k][y]==1){
10+
return false;
11+
}
12+
}
13+
//Left Diag
14+
int i = x;
15+
int j = y;
16+
while(i>=0 and j>=0){
17+
if(board[i][j]==1){
18+
return false;
19+
}
20+
i--; j--;
21+
}
22+
23+
//Right Diag
24+
i = x;
25+
j = y;
26+
while(i>=0 and j<n){
27+
if(board[i][j]==1){
28+
return false;
29+
}
30+
i--; j++;
31+
}
32+
return true;
33+
}
34+
35+
void printBoard(int n,int board[][20]){
36+
37+
for(int i=0;i<n;i++){
38+
for(int j=0;j<n;j++){
39+
cout<< board[i][j]<<" ";
40+
}
41+
cout<<endl;
42+
}
43+
cout <<endl;
44+
}
45+
46+
bool solveNQueen(int n,int board[][20],int i){
47+
//base case
48+
if(i==n){
49+
//Print the board
50+
printBoard(n,board);
51+
return true;
52+
}
53+
54+
55+
// rec case
56+
// try to place a queen in every row
57+
for(int j=0;j<n;j++){
58+
//whether the current i,j is safe or not
59+
if(canPlace(board,n,i,j)){
60+
board[i][j] = 1;
61+
bool success = solveNQueen(n,board,i+1);
62+
if(success){
63+
return true;
64+
}
65+
//backtrack
66+
board[i][j] = 0;
67+
}
68+
}
69+
return false;
70+
}
71+
72+
int main() {
73+
// your code goes here
74+
int board[20][20] = {0};
75+
int n;
76+
cin>>n;
77+
78+
solveNQueen(n,board,0);
79+
80+
81+
82+
return 0;
83+
}
84+

0 commit comments

Comments
 (0)