Skip to content

Commit 7eb7944

Browse files
author
Mohit Jain
committed
recursion advance + backtracking basics
1 parent b9058ae commit 7eb7944

19 files changed

+514
-2
lines changed

0-1Knapsack

34.5 KB
Binary file not shown.

0-1Knapsack.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
6+
int profit(int N, int C, int *wt,
7+
int *prices){
8+
9+
if(N == 0 || C == 0) {
10+
return 0;
11+
}
12+
13+
int includeItem, excludeItem;
14+
int ans = 0;
15+
includeItem = excludeItem = INT_MIN;
16+
if(wt[N-1] <=C) {
17+
includeItem = prices[N-1] + profit(N-1,
18+
C-wt[N-1],wt,prices);
19+
}
20+
excludeItem = profit(N-1, C,wt,prices);
21+
22+
return max(includeItem,excludeItem);
23+
}
24+
25+
26+
int main() {
27+
28+
#ifndef ONLINE_JUDGE
29+
// for getting input from input.txt
30+
freopen("input.txt", "r", stdin);
31+
// for writing output to output.txt
32+
freopen("output.txt", "w", stdout);
33+
#endif
34+
35+
36+
int weights[] = {1,2,3,5};
37+
38+
int prices[] = {40,20,30,100};
39+
40+
int N = 4;
41+
int C = 7;
42+
cout<<profit(N,C,weights,prices)<<endl;
43+
return 0;
44+
}

CheckSorted

39.5 KB
Binary file not shown.

CheckSorted.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
bool checkSorted(int *a, int n){
6+
7+
if(n == 0 or n == 1) {
8+
return true;
9+
}
10+
11+
if (a[0] <= a[1] and checkSorted(a+1, n-1)) { return true; }
12+
13+
14+
15+
return false;
16+
17+
}
18+
19+
20+
int main() {
21+
22+
#ifndef ONLINE_JUDGE
23+
// for getting input from input.txt
24+
freopen("input.txt", "r", stdin);
25+
// for writing output to output.txt
26+
freopen("output.txt", "w", stdout);
27+
#endif
28+
29+
30+
int n;
31+
32+
cin>>n;
33+
34+
int a[n];
35+
36+
for(int i = 0; i < n ; i ++) {
37+
cin>>a[i];
38+
}
39+
if(checkSorted(a,n-1)) {
40+
cout<<"true"<<endl;
41+
42+
}
43+
else {
44+
cout<<"false"<<endl;
45+
}
46+
47+
48+
49+
50+
51+
52+
return 0;
53+
}

RatInMaze

39.5 KB
Binary file not shown.

RatInMaze.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
bool ratInMaze(char maze[10][10], int
6+
soln[][10], int i,int j,int m,int n){
7+
8+
if(i == m and j == n) {
9+
10+
soln[m][n] = 1;
11+
12+
for(int i=0; i <=m; i++){
13+
for(int j =0; j <=n; j++) {
14+
cout<<soln[i][j]<< " ";
15+
}
16+
cout<<endl;
17+
}
18+
cout<<endl;
19+
20+
return true;
21+
}
22+
23+
// Rec case
24+
if(i > m || j >n) return false;
25+
26+
if(maze[i][j] == 'X') return false;
27+
28+
soln[i][j] = 1;
29+
bool rightSide = ratInMaze(maze,soln,
30+
i,j+1,m,n);
31+
bool downSide = ratInMaze(maze,soln,
32+
i+1,j , m, n);
33+
soln[i][j] = 0;
34+
if (rightSide || downSide) return
35+
true;
36+
return false;
37+
}
38+
39+
int main() {
40+
41+
#ifndef ONLINE_JUDGE
42+
// for getting input from input.txt
43+
freopen("input.txt", "r", stdin);
44+
// for writing output to output.txt
45+
freopen("output.txt", "w", stdout);
46+
#endif
47+
48+
char maze[10][10] = {
49+
"0000",
50+
"00X0",
51+
"000X",
52+
"0X00"
53+
};
54+
55+
56+
int soln[10][10] = {0};
57+
int m = 4, n =4;
58+
59+
bool ans = ratInMaze(maze,soln,0,0,m-1
60+
,n-1);
61+
if(!ans) {
62+
cout<< " path does not exist";
63+
}
64+
return 0;
65+
}

StringPermutation.cpp

Whitespace-only changes.

TowerOfHanoi.cpp

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
void move(int n, char src, char helper , char dest){
6+
7+
if (n ==0) return;
8+
9+
10+
move(n-1,src,dest,helper);
11+
12+
cout<<"Shift disl"<<n << "from" <<src << "to" <<dest<<endl;
13+
14+
move(n-1,helper,src,dest);
15+
}
16+
17+
18+
19+
20+
int main() {
21+
22+
23+
#ifndef ONLINE_JUDGE
24+
// for getting input from input.txt
25+
freopen("input.txt", "r", stdin);
26+
// for writing output to output.txt
27+
freopen("output.txt", "w", stdout);
28+
#endif
29+
30+
int n;
31+
32+
cin>>n;
33+
34+
move(n,'A','B','C');
35+
36+
binary consist of only 0 and 1
37+
38+
pehle col vala loop h
39+
use sahi karo
40+
code m
41+
mera code dekho idhar
42+
43+
map<char,int> a[100];
44+
45+
for(int i=0; i< str.len(); i++) {
46+
a[str]++;
47+
48+
}
49+
50+
for(ans: a) {
51+
52+
cout<<ans.key<<ans.value;
53+
}
54+
55+
56+
57+
for(int i = 0; i < col ; i ++) {
58+
for (int j = 0; j < row; j++) {
59+
if(mat[j][i] == 1) {
60+
ans = i;
61+
break;
62+
63+
}
64+
}
65+
}
66+
67+
68+
69+
retrun 0;
70+
}
71+
72+
just iterate matrix and check whether it is 1 in column then it would return answer O(n2)
73+
74+

generateBrackets

39.5 KB
Binary file not shown.

generateBrackets.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
void generateBrackets(char *out, int n,
6+
int idx, int open, int close){
7+
8+
9+
if(idx == 2*n) {
10+
out[idx] = '\0';
11+
12+
cout<< out <<endl;
13+
return;
14+
}
15+
16+
17+
if (open < n) {
18+
19+
out[idx] = '{';
20+
generateBrackets(out,n,idx+1,open+1,
21+
close);
22+
}
23+
24+
if (close < open) {
25+
26+
out[idx] = '}';
27+
generateBrackets(out,n,idx+1,open,
28+
close+1);
29+
}
30+
return;
31+
32+
33+
}
34+
35+
36+
37+
38+
int main() {
39+
40+
#ifndef ONLINE_JUDGE
41+
// for getting input from input.txt
42+
freopen("input.txt", "r", stdin);
43+
// for writing output to output.txt
44+
freopen("output.txt", "w", stdout);
45+
#endif
46+
47+
int n;
48+
49+
cin>>n;
50+
51+
char out[1000];
52+
53+
int idx = 0;
54+
55+
generateBrackets(out,n,idx,0,0);
56+
57+
58+
59+
return 0;
60+
}

generateStrings

40.7 KB
Binary file not shown.

generateStrings.cpp

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
void generateStrings(char *in,char *out,
6+
int i, int j) {
7+
8+
if(in[i] == '\0'){
9+
10+
out[j] = '\0';
11+
12+
cout << out <<endl;
13+
14+
return;
15+
}
16+
17+
// rec case
18+
19+
int digit = in[i] - '0';
20+
if (digit!= 0) {
21+
char ch = digit + 'A' - 1;
22+
out[j] = ch;
23+
24+
generateStrings(in,out,i+1,j+1);
25+
}
26+
if(in[i+1]!= '\0') {
27+
28+
int secondDigit = in[i+1] - '0';
29+
30+
int no = digit*10 + secondDigit;
31+
if(no <= 26) {
32+
char ch = no + 'A' - 1;
33+
out[j] = ch;
34+
generateStrings(in,out,i+2,j+1);
35+
}
36+
37+
}
38+
39+
return;
40+
41+
}
42+
43+
int main () {
44+
45+
#ifndef ONLINE_JUDGE
46+
// for getting input from input.txt
47+
freopen("input.txt", "r", stdin);
48+
// for writing output to output.txt
49+
freopen("output.txt", "w", stdout);
50+
#endif
51+
52+
char a[100];
53+
54+
cin>>a;
55+
56+
char out[100];
57+
58+
generateStrings(a,out,0,0);
59+
60+
61+
62+
return 0;
63+
}

input.txt

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

keypadRecursion

56.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)