Cod sursa(job #966711)

Utilizator BlackElfSpulber Iosif BlackElf Data 26 iunie 2013 15:11:23
Problema Generare de permutari Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

typedef unsigned int instance;
typedef vector<unsigned int> config;

ofstream out ("permutari.in");

config root (instance P)
{
	config c;
	return c;
}

int reject (instance P, config c)
{
	if (c.size() > P)
		return true;
	
	for (int i = 0; i < (int) c.size() - 1; i++)
		if (c[i] == c.back())
			return true;

	return false;
}

int accept (instance P, config c)
{
	return c.size() == P;
}

int bad (config c)
{
	return c.size() >= 1 && c[0] == -1;
}

config next (instance P, config c)
{
	if (c.back() >= P)
	{
		c.clear();
		c.push_back(-1);
		return c;
	}

	c.back() ++;
	return c;
}

config first (instance P, config c)
{
	if (c.size() >= P)
	{
		c.clear();
		c.push_back(-1);
		return c;
	}

	c.push_back (1);
	return c;
}

void output (instance P, config c)
{
	for (int i = 0; i < c.size(); i++)
		out << c[i] << ' ';
	out << endl;
}

void bt (instance P, config c)
{
	if (reject (P, c))
		return;
	if (accept (P, c))
		output (P, c);

	config s;
	s = first (P, c);
	while (!bad(s))
	{
		bt (P, s);
		s = next (P, s);
	}
}

int main ()
{
	int n;
	ifstream in ("permutari.in");
	in >> n;
	bt (n, root (n));

	return 0;
}