Cod sursa(job #1463537)

Utilizator justsomedudePalade Thomas-Emanuel justsomedude Data 21 iulie 2015 10:26:12
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include<iostream>
#include<fstream>
#include<cstdlib>

using namespace std;

ifstream fin ("damesah.in");
ofstream fout ("damesah.out");

int st[15],v[15];
int n,sol,top;

int Citire()
{
    fin>>n;
}

inline int Modul(int x)
{
	if (x < 0) return -x;
	return x;
}

int Valid(int top, int x)
{
	if (v[x] == 1) return 0;
	for (int i = 1; i < top; i++)
		if (Modul(top-i) == Modul(x-st[i])) return 0;
     return 1;
}

void Afisare()
{
    int i;
	for (i = 1; i <= n; i++)
		fout << st[i] << " ";
	fout << "\n";
	fout << sol << "\n";
	fout.close();
	exit(0); // cstdlib
}

void Back1(int top)
{
    int i;
	if (top == n+1) sol++;
	else for (i = 1; i <= n; i++)
		if (Valid(top, i))
		{
			st[top] = i;
			v[i] = 1;
			Back1(top+1);
			v[i] = 0;
}
}

void Back(int top)
{
	int i;
	if (top == n+1) Afisare();
	else for (i = 1; i <= n; i++)
		if (Valid(top, i))
		{
			st[top] = i;
			v[i] = 1;
			Back(top+1);
			v[i] = 0;
}
}

int main ()
{
    Citire();
    Back1(1);
    Back(1);
    fin.close();
    fout.close();
    return 0;
}