Cod sursa(job #1874860)

Utilizator petru.ciocirlanPetru Ciocirlan petru.ciocirlan Data 10 februarie 2017 15:07:38
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.69 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], check[14], nrsol;

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

void back_dame()
{
	k++;
	for(int i = 1; i <= n; ++i)
	{
		x[k] = i;
		if(!check[i] && (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;
}