Pagini recente » Cod sursa (job #1587858) | Cod sursa (job #2357390) | Cod sursa (job #1112460) | Cod sursa (job #2586544) | Cod sursa (job #2416856)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int ind[15];
bool colblock[15], diagpr[30], diagsec[30];
int n, cnt;
void backtrack(int lin){
if(lin == n + 1){
if(cnt == 0){
for(int i = 1; i <= n; ++i)
fout << ind[i] << " ";
}
cnt++;
return;
}
for(int col = 1; col <= n; ++col){
if(!colblock[col] && !diagpr[15 - col + lin] && !diagsec[col + lin]){
ind[lin] = col;
colblock[col] = diagpr[15 - col + lin] = diagsec[col + lin] = 1;
backtrack(lin + 1);
colblock[col] = diagpr[15 - col + lin] = diagsec[col + lin] = 0;
}
}
}
int main()
{
fin >> n;
backtrack(1);
fout << "\n" << cnt;
return 0;
}