Cod sursa(job #1872634)

Utilizator petru.ciocirlanPetru Ciocirlan petru.ciocirlan Data 8 februarie 2017 14:11:17
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.67 kb
#include <fstream>
#include <cmath>
//#define abs(x) ((x < 0 ? -x : x)) GRESIT :'(
using namespace std;
ifstream f("damesah.in");
ofstream g("damesah.out");
int n, k, x[14], nrsol;

bool cond()
{
	for(int i = 1; i < k; ++i)
		if(abs(x[k] - x[i]) == abs(k-i) || x[i] == x[k]) return 0;
	return 1;
}

void back_dame()
{
	k++;
	for(int i = 1; i <= n; ++i)
	{
		x[k] = i;
		if(k == 1 || cond())
		{
			//check[i] = 1;
			if(k == n)
			{
				if(!nrsol) for(int i = 1; i <= n; ++i) g<<x[i]<<' ';
				nrsol++;
			}
			else back_dame();
			//check[i] = 0;
		}
	}
	k--;
}

int main()
{
    f>>n;
    back_dame();
    g<<'\n'<<nrsol;
    return 0;
}