Cod sursa(job #3219046)

Utilizator Destroyer2234David Andrei Goreci Destroyer2234 Data 29 martie 2024 20:08:12
Problema Problema Damelor Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
using namespace std;
#include <fstream>
#include <cmath>
ifstream cin("damesah.in");
ofstream cout("damesah.out");
int n;
int sol[14], l[14], c[14];
bool ok = 1;
int k = 0;

bool diagCheck(int l, int c, int n)
{
	for (int i = 1; i <= n; i++)
	{
		if (abs(l - i) == abs(sol[i] - c))
		{
			return false;
		}
	}

	return true;
}


void Dame(int pas)
{
	if (pas == n + 1)
	{
		if (ok)
		{
			for (int i = 1; i <= n; i++)
			{
				cout << sol[i] << " ";
			}
			ok = 0;
		}
		k++;
	}
	else
	{
		for (int j = 1; j <= n; j++)
		{
			if (c[j] == 0 && diagCheck(pas, j, pas - 1))
			{				
				c[j] = 1;
				sol[pas] = j;
				Dame(pas + 1);				
				c[j] = 0;
			}
		}
			
	}
}



int main()
{
	
	cin >> n;
	Dame(1);
	cout << '\n';
	cout << k;

}