Pagini recente » Cod sursa (job #568098) | Cod sursa (job #3228731) | Cod sursa (job #2815372) | Cod sursa (job #3272608) | Cod sursa (job #2416844)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
int ind[15], ans[15];
bool col[15], diagpr[15], diagsec[15];
int n, cnt;
void backtrack(int pos){
if(pos == n + 1){
bool schimb = 0;
for(int i = 1; i <= n; ++i)
if(ind[i] < ans[i]) schimb = 1;
if(schimb){
for(int i = 1; i <= n; ++i)
ans[i] = ind[i];
}
cnt++;
return;
}
for(int i = 1; i <= n; ++i){
if(!col[i] && !diagpr[15 - i + pos] && !diagsec[i + pos]){
ind[i] = pos;
col[i] = diagpr[15 - i + pos] = diagsec[i + pos] = 1;
backtrack(pos + 1);
ind[i] = 0;
col[i] = diagpr[15 - i + pos] = diagsec[i + pos] = 0;
}
}
}
int main()
{
fin >> n;
for(int i = 1; i <= n; i++) ans[i] = 1e9;
backtrack(1);
for(int i = 1; i <= n; ++i) fout << ans[i] << " ";
fout << "\n" << cnt;
return 0;
}