Code: Select all
#include<iostream>
using namespace std;
int st[100],depth,m[50][50],top,n,zile;
int tipar(int st[],int top)
{
zile++;
//m[i][d[i]]=st[top+1];
//for(int i=1;i<=top;i++)
cout<<endl;
for(int i=1;i<=n;i++)
{
cout<<st[i]<<" ";
m[st[i]][i]=st[top+1];
}
top=1;
return 0;
}
int succesor(int st[],int top)
{
if(st[top]<n)
{
st[top]++;
return 1;
}
else return 0;
}
int valid(int st[],int top)
{
if(top>1)
{
for(int i=1;i<top;i++)
{
if(st[i]==st[top])
return 0;
}
for(int i=1;i<=depth+1;i++)
{
if(st[top]==m[st[top-1]][i])
return 0;
}
}
return 1;
}
int init(int st[],int top)
{
st[top]=0;
}
int solutie(int st[],int top)
{
if(top==n)
return 1;
return 0;
}
int back(int st[],int top)
{
top=1;
init(st,top);
while(top>0)
{
while(succesor(st,top))
{
if(valid(st,top))
if(solutie(st,top))
tipar(st,top);
else
{
top++;
init(st,top);
}
}
top--;
}
}
int main()
{
cout<<"n= ";
cin>>n;
back(st,top);
cout<<endl<<"Zile= "<<zile;
return 0;
}
La mine nu stiu cum se face dar inca e permutari :)) ...