100% found this document useful (1 vote)
285 views

Problema Colorarii Hartilor

This C++ program defines functions to generate all valid permutations of integers from 0 to m that satisfy constraints defined in a 2D array. It uses recursion to increment values in an array, check if each value is valid compared to previous values based on the 2D array, and output valid permutations up to length n. The main function initializes values, calls the functions in a loop to generate all valid permutations, and terminates when no more can be found.

Uploaded by

zuzu_deeia
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
285 views

Problema Colorarii Hartilor

This C++ program defines functions to generate all valid permutations of integers from 0 to m that satisfy constraints defined in a 2D array. It uses recursion to increment values in an array, check if each value is valid compared to previous values based on the 2D array, and output valid permutations up to length n. The main function initializes values, calls the functions in a loop to generate all valid permutations, and terminates when no more can be found.

Uploaded by

zuzu_deeia
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

using namespace std;


typedef int sir[100];
sir x;
int m,i,j,k,n;
int as,ev;
int a[100][100];
void succ(sir x,int k,int& as)
{
if(x[k]<m)
{
as=1;
x[k]=x[k]+1;
}
else as=0;
}
void valid(sir x, int k, int& ev)
{
ev=1;
for(i=1;i<=k-1;i++)
if((a[k][i]==1)&&(x[k]==x[i]))
ev=0;
}
void afis(sir x,int k)
{
int i;
for (i=1;i<=k;i++)
cout<<x[i]<<" ";
cout<<endl;
}
main ()
{
cout<<"n="; cin>>n;
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
cin>>a[i][j];
a[j][i]=a[i][j];
}
cout<<"m="; cin>>m;
k=1;
x[k]=0;
while(k>0)
{
do
{
succ(x,k,as);
if(as)
valid(x,k,ev);
}
while(as&&!ev);
if(as)
if(k==n)
afis(x,k);
else
{
k=k+1;
x[k]=0;
}
else k=k-1;

}
}

You might also like