Cod sursa(job #1231565)

Utilizator daniel.amarieiDaniel Amariei daniel.amariei Data 20 septembrie 2014 23:01:00
Problema Problema Damelor Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <fstream>
using namespace std;

#define ABS(x) ((x) >= 0 ? (x) : -(x))
int n, s, L[14], C[14];

ifstream ifs("damesah.in");
ofstream ofs("damesah.out");

	
void backtrack(int q)
{
	if (q > n)
	{
		++s;
		
		if (s == 1)
		{
			// Print first solution
			for (int i = 1; i <= n; ++i)
			{
				ofs << V[i] << " ";
			}
			
			ofs << "\n";
		}
	} 
	else
	{
		for (int j = 1; j <= n; ++j)
		{
			bool is_safe = true;
			for (int i = 1; i < q && is_safe; ++i)
			{
				if (C[i])
				{
					is_safe = (j == C[i]) && ((q - i) == ABS(j - C[i]));
				}
			}
			
			if (is_safe)
			{
				C[q] = j;
				backtrack(q+1);
				C[q] = 0;
			}
		}
	}
}


int main()
{	
	ifs >> n;
	backtrack(1);
	ofs << s;
	
	return 0;
}