Cod sursa(job #2029962)

Utilizator randiboyPucani Catalin randiboy Data 30 septembrie 2017 18:39:37
Problema Problema Damelor Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.02 kb
// ConsoleApplication19.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <math.h>
#include <algorithm>
using namespace std;
ifstream f("damesah.in");
ofstream g("damesah.out");
int n, sol[101], nrsol;


int elem(int k)
{
	int i;
	for (i = 1; i<k; i++)
	{
		if (sol[i] == sol[k] || sol[k]==sol[k-1] || sol[k]==sol[k-2])
			return 0;
		for (i = 1; i < k; i++)
			if (abs(k - i) == abs(sol[k] - sol[i]))
				return 0;
		return 1;
	}
	
}

int solutie(int k)
{
	if (k == n)
		return 1;
	return 0;
	
}



void tiparire(int k)
{
	nrsol++;
	if (nrsol == 1)
	{
		for (int i = 1; i <= n; i++)
			g << sol[i] << " ";
		g << "\n";
	}
}

void BackTracking(int k)
{
	int i,p=0;
	for (i =1; i <= n; i++)
	{
		sol[k] = i;
		if (elem(k))
		{
			if (solutie(k))
			{
				tiparire(k);
			}
			else
				BackTracking(k + 1);
		}

	}
}



int main()
{
	f >> n;
	BackTracking(1);
	g << nrsol;
	return 0;
}