Cod sursa(job #1877092)

Utilizator cautionPopescu Teodor caution Data 12 februarie 2017 22:13:23
Problema Problema Damelor Scor 90
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <bits/stdc++.h>

using namespace std;

const int kMaxN = 13;

unsigned char n;
int counter = 0;
unsigned char v[kMaxN + 1];

void backtrack(int k)
{
	if (k == n) {
		if (++counter == 1) { 
			for (unsigned char i = 0; i < n; ++i) printf("%hhd ", v[i]);
			printf("\n");
		}
		return;
	}
	for (unsigned char i = 1; i <= n; ++i) {
		bool valid = true;
		for (unsigned char j = 0; j < k; ++j) {
			if (v[j] == i || v[j] - j == i - k ||
			    v[j] + j == i + k) {
				valid = false;
				break;
			}
		}
		if (!valid) continue;
		v[k] = i;
		backtrack(k + 1);
	}
}

int main()
{
	freopen("damesah.in", "rt", stdin);
	freopen("damesah.out", "wt", stdout);

	scanf("%hhd", &n);
	backtrack(0);
	printf("%d\n", counter);

	return 0;
}