Cod sursa(job #150104)

Utilizator sory1806Sandu Sorina-Gabriela sory1806 Data 6 martie 2008 16:21:49
Problema Dusman Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.26 kb
#include<fstream.h>
#define max 1000
int s[max+1], k, n, m, nr_sol, mn;
char a[max][max], uz[max];
ifstream f("dusman.in");
ofstream g("dusman.out");

void back();
void write();
void read();
int corect(int o);
int sol(int o);
int min(int o);

int main()
{       read();
	back();
	g.close();
	return 0;
}

void back()
{       int i;
	i=1; s[i]=0; nr_sol=0;
	while(i!=0)
		if(s[i]<n)
		{	s[i]++;
			if(corect(i))
			{   //	nr_sol+=sol(i);
				if(i==n && nr_sol<k)
				{       nr_sol++;
					if(nr_sol==k)
					{	write();
						return;
					}
				}
				else
				{       if(i<n)
					{	mn=min(i);
						i++;
						s[i]=mn;
					}
				}
			}
		}
		else
			i--;
}

int corect(int o)
{	int i, ok=1;
	for(i=1; i<o; i++)
		if(s[o]==s[i])
		{	ok=0; break;
		}
	if(a[s[o]][s[o-1]])
		ok=0;
	return ok;
}

int sol(int o)
{	if(o==n)
		return 1;
	return 0;
}

int min(int o)
{	int i;
	for(i=1; i<=n; i++)
	{       uz[i]=0;
	}
	for(i=1; i<o; i++)
		uz[s[i]]=1;
	for(i=1; i<n; i++)
		if(uz[i]==0)
			return i-1;
	return n;

}

void read()
{       int i, x, y;
	f>>n>>k>>m;
	for(i=0; i<m; i++)
	{	f>>x>>y;
		a[x][y]=1;
		a[y][x]=1;
	}
}

void write()
{	int i;
	for(i=1; i<=n; i++)
	{       g<<s[i]<<' ';
	}
}