Cod sursa(job #363388)

Utilizator DragosPopaDragos Popa DragosPopa Data 12 noiembrie 2009 22:59:31
Problema Generare de permutari Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.15 kb
#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;
}