Skip to content

Commit 5df139a

Browse files
Mohit JainMohit Jain
Mohit Jain
authored and
Mohit Jain
committed
backtracking advanced
1 parent 7eb7944 commit 5df139a

9 files changed

+246
-16
lines changed

StringPermutation

79.9 KB
Binary file not shown.

StringPermutation.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <bits/stdc++.h>
2+
#include <string>
3+
4+
using namespace std;
5+
void permute(char *in, int i, set<string> &s) {
6+
// base case
7+
8+
if(in[i] == '\0') {
9+
10+
// cout<<in<<endl;
11+
string gd(in);
12+
s.insert(gd);
13+
return;
14+
15+
}
16+
17+
for (int j = i; in[j]!= '\0';j++){
18+
19+
swap(in[i],in[j]);
20+
permute(in,i+1,s);
21+
22+
// Backtracking - to restore the
23+
// original array
24+
25+
swap(in[i],in[j]);
26+
}
27+
28+
29+
30+
31+
}
32+
33+
int main() {
34+
35+
#ifndef ONLINE_JUDGE
36+
// for getting input from input.txt
37+
freopen("input.txt", "r", stdin);
38+
// for writing output to output.txt
39+
freopen("output.txt", "w", stdout);
40+
#endif
41+
42+
set<string> s;
43+
char in[100];
44+
cin >> in;
45+
46+
permute(in,0,s);
47+
48+
for(auto str: s){ cout<<str<<",";}
49+
return 0;
50+
}

input.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
123
1+
4

nQueen

39.5 KB
Binary file not shown.

nQueen.cpp

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
bool isSafe(int board[][10], int i, int j
6+
, int n) {
7+
for(int row =0; row<i; row++){
8+
if(board[row][j] == 1){
9+
return false;
10+
}
11+
}
12+
int x = i;
13+
int y = j;
14+
15+
while (x >=0 and y>=0){
16+
if(board[x][y] == 1) {
17+
return false;
18+
}
19+
x--;
20+
y--;
21+
}
22+
23+
x = i;
24+
y = j;
25+
26+
while (x >=0 and y<n){
27+
if(board[x][y] == 1) {
28+
return false;
29+
}
30+
x--;
31+
y++;
32+
}
33+
return true;
34+
}
35+
bool solvedQueen(int board[][10],int i,
36+
int n) {
37+
// Base Case
38+
if(i == n) {
39+
40+
for(int i = 0; i<n;i++) {
41+
for(int j=0; j< n; j ++) {
42+
if(board[i][j] == 1){
43+
cout<<" Q ";
44+
}
45+
else {
46+
cout<< " . ";
47+
}
48+
}
49+
cout<<endl;
50+
}
51+
cout<<endl<<endl;
52+
return false;
53+
}
54+
// rec case
55+
// Try to place queen in current row
56+
for(int j=0; j <n; j++){
57+
if(isSafe(board,i,j,n)){
58+
board[i][j] = 1;
59+
bool nextQueenRakhPaye =
60+
solvedQueen(board,i+1,n);
61+
if(nextQueenRakhPaye) {
62+
return true;
63+
}
64+
board[i][j] = 0;
65+
}
66+
67+
}
68+
return false;
69+
}
70+
71+
int main() {
72+
73+
74+
#ifndef ONLINE_JUDGE
75+
// for getting input from input.txt
76+
freopen("input.txt", "r", stdin);
77+
// for writing output to output.txt
78+
freopen("output.txt", "w", stdout);
79+
#endif
80+
int n;
81+
cin>>n;
82+
int board[10][10] = {0};
83+
84+
solvedQueen(board,0,n);
85+
86+
return 0 ;
87+
}

nQueenBitmask

40.3 KB
Binary file not shown.

nQueenBitmask.cpp

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
bool isSafe(int board[][10], int i, int j
6+
, int n) {
7+
for(int row =0; row<i; row++){
8+
if(board[row][j] == 1){
9+
return false;
10+
}
11+
}
12+
int x = i;
13+
int y = j;
14+
15+
while (x >=0 and y>=0){
16+
if(board[x][y] == 1) {
17+
return false;
18+
}
19+
x--;
20+
y--;
21+
}
22+
23+
x = i;
24+
y = j;
25+
26+
while (x >=0 and y<n){
27+
if(board[x][y] == 1) {
28+
return false;
29+
}
30+
x--;
31+
y++;
32+
}
33+
return true;
34+
}
35+
bool solvedQueen(int board[][10],int i,
36+
int n) {
37+
// Base Case
38+
if(i == n) {
39+
40+
for(int i = 0; i<n;i++) {
41+
for(int j=0; j< n; j ++) {
42+
if(board[i][j] == 1){
43+
cout<<" Q ";
44+
}
45+
else {
46+
cout<< " . ";
47+
}
48+
}
49+
cout<<endl;
50+
}
51+
cout<<endl<<endl;
52+
return false;
53+
}
54+
// rec case
55+
// Try to place queen in current row
56+
for(int j=0; j <n; j++){
57+
if(isSafe(board,i,j,n)){
58+
board[i][j] = 1;
59+
bool nextQueenRakhPaye =
60+
solvedQueen(board,i+1,n);
61+
if(nextQueenRakhPaye) {
62+
return true;
63+
}
64+
board[i][j] = 0;
65+
}
66+
67+
}
68+
return false;
69+
}
70+
bitset<30> col,d1,d2;
71+
72+
void solve(int r, int n, int &ans
73+
) {
74+
if(r== n) {
75+
ans++;
76+
return;
77+
}
78+
for(int c=0; c<n;c++){
79+
if(!col[c] and !d1[r-c + n-1]
80+
and !d2[r+c]){
81+
col[c] = d1[r-c+n-1] =
82+
d2[r+c] = 1;
83+
solve(r+1,n,ans);
84+
col[c] = d1[r-c+n-1] =
85+
d2[r+c] = 0;
86+
}
87+
}
88+
}
89+
int main() {
90+
91+
92+
#ifndef ONLINE_JUDGE
93+
// for getting input from input.txt
94+
freopen("input.txt", "r", stdin);
95+
// for writing output to output.txt
96+
freopen("output.txt", "w", stdout);
97+
#endif
98+
int n;
99+
cin>>n;
100+
int board[10][10] = {0};
101+
int ans = 0;
102+
solve(0,n,ans);
103+
cout<<ans<<endl;
104+
// solvedQueen(board,0,n);
105+
106+
return 0 ;
107+
}

output.txt

+1-15
Original file line numberDiff line numberDiff line change
@@ -1,15 +1 @@
1-
1 1 0 0
2-
0 1 0 0
3-
0 1 1 0
4-
0 0 1 1
5-
6-
1 0 0 0
7-
1 1 0 0
8-
0 1 1 0
9-
0 0 1 1
10-
11-
1 0 0 0
12-
1 0 0 0
13-
1 1 1 0
14-
0 0 1 1
15-
1+
2

scanf.cpp

Whitespace-only changes.

0 commit comments

Comments
 (0)