Super solution for exponential problems
This time I am proposing a super solution for all problems of exponential nature. Just by changing few parameters in this simple program can be used to find solutions for following problems and many other :-
1) All permutations and combinations problems
2) All permutations of the given string
3) Finding all the subsets of the given set
4) N-queen problem
5) Dynamic algorithm
Output of the Above program is
I will put some more sample programs which uses this simple program to solve some known problems.
1) All permutations and combinations problems
2) All permutations of the given string
3) Finding all the subsets of the given set
4) N-queen problem
5) Dynamic algorithm
/*This program is copyrighted by Manoj Ransing.
Use only for academic purpose*/
/*This program will print all the strings of length 3,
where each one of the character can take value from
'0' to '3' e.g 000, 001, 002, 003, 010, 011, ...,
300,..,333*/
#include <iostream>
using namespace std;
const char size = 3;/*This is the size of the elements*/
const char choice = 4;/*This is the number of choices for elements*/
string str(size,'0');/*The set of elements*/
long solutions = 0;/*Total number of solutions*/
/*Condition to continue recursing*/
bool check(int i)
{
return true;
};
/*Printing the solution*/
void print()
{
printf("%s ",str.c_str());
};
void perm(int n)
{
/*This condition means end of the boundary,
This must be one of the solution or output*/
if(n == size)
{
solutions++;
print();
return;
};
/*This loop continues bounding values to elements*/
for(int i=0; i<choice;i++)
{
str.at(n) = '0' + i;
/*Check for the condition for the current element
if condition satisfies recurse for the next n*/
if(check(n))
perm(n+1);
}
}
int main()
{
perm(0);
printf("\n");
printf("number of solutions is %ld\n", solutions);
return 0;
}
Output of the Above program is
000 001 002 003 010 011 012 013
020 021 022 023 030 031 032 033
100 101 102 103 110 111 112 113
120 121 122 123 130 131 132 133
200 201 202 203 210 211 212 213
220 221 222 223 230 231 232 233
300 301 302 303 310 311 312 313
320 321 322 323 330 331 332 333
number of solutions is 64
I will put some more sample programs which uses this simple program to solve some known problems.
0 Comments:
Post a Comment
<< Home