Cod sursa(job #1820043)

Utilizator howsiweiHow Si Wei howsiwei Data 1 decembrie 2016 07:19:42
Problema Problema Damelor Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
#define SZ(a) (int)(a).size()

int n;
int N;
int ans;
vector<int> queens;

void dfs(int M1, int M2, int M3) {
	if (M1 == N) {
		if (ans++ == 0) {
			for (int i = 0; i < n; i++) {
				printf("%d%c", __builtin_ctz(queens[i])+1, " \n"[i == n-1]);
			}
		}
	}
	else {
		for (auto M = N ^ (M1 | M2 | M3); M; M &= M-1) {
			int B = M & -M;
			queens.push_back(B);
			dfs(M1 | B, N & (M2 | B)<<1, N & (M3 | B)>>1);
			queens.pop_back();
		}
	}
}

int main() {
#ifdef INFOARENA
	freopen("damesah.in", "r", stdin);
	freopen("damesah.out", "w", stdout);
#endif
	cin.sync_with_stdio(false);
	cin >> n;
	N = (1<<n)-1;
	ans = 0;
	dfs(0, 0, 0);
	printf("%d\n", ans);
	return 0;
}