Pagini recente » Cod sursa (job #729115) | Cod sursa (job #2678455) | Cod sursa (job #3125208) | Cod sursa (job #1894243) | Cod sursa (job #363388)
Cod sursa(job #363388)
#include<fstream>
using namespace std;
ifstream f("permutari.in");
ofstream g("permutari.out");
typedef struct{
int st[100];
int top;
} stiva;
stiva stack;
int n;
int init(stiva *s)
{
(*s).st[(*s).top]=0;
return 1;
}
int succesor(stiva *s)
{
if((*s).st[(*s).top]<n)
{
(*s).st[(*s).top]++;
return 1;
}
else
return 0;
}
int valid(stiva s)
{
for(int i=0;i<s.top;i++)
if(s.st[i]==s.st[s.top])
return 0;
return 1;
}
int solutie(stiva s)
{
if(s.top==n)
return 1;
else
return 0;
}
int tipar(stiva s)
{
for(int i=1;i<=s.top;i++)
g<<s.st[i]<<" ";
g<<endl;
return 1;
}
int bktr()
{
int am,este;
stack.top=1;
init(&stack);
while(stack.top>=0)
{
do {
am=succesor(&stack);
este=valid(stack);
}
while(am && !este);
if(am)
if(solutie(stack))
tipar(stack);
else
{
stack.top++;
init(&stack);
}
else
stack.top--;
}
return 1;
}
int main()
{
f>>n;
bktr();
f.close();
g.close();
return 0;
}