Cod sursa(job #1474797)

Utilizator aimrdlAndrei mrdl aimrdl Data 22 august 2015 23:28:52
Problema Problema Damelor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.72 kb
#include <stdio.h>

int n, remaining, sol;
int c[13], maind[26], secd[26], pos[14];

inline int is_free(int x, int y) {
	if (c[y]) return 0;
	
	if (maind[n-1-(x-y)]) return 0;
	
	if (secd[x+y]) return 0;
	
	return 1;
}

void qbkt (int l) {
	if (l == n) {
		++sol;
		if (sol == 1) {
			for (int i = 0; i < n; ++i) {
				printf("%d ", pos[i]+1);
			}
			printf("\n");
		}
				
		return;
	}
		
	for (int j = 0; j < n; ++j) {
		if (is_free(l, j)) {
			c[j] = maind[n-1-(l-j)] = secd[l+j] = 1;
			pos[l] = j;
			qbkt(l+1);
			c[j] = maind[n-1-(l-j)] = secd[l+j] = 0;
		}
	}
}			

int main (void) {
	freopen("damesah.in", "r", stdin);
	freopen("damesah.out", "w", stdout);
	
	scanf("%d", &n);
	
	qbkt(0);
	
	printf("%d", sol);
	
	return 0;
}