Pagini recente » Cod sursa (job #2138068) | Cod sursa (job #1278183) | Cod sursa (job #1490666) | Cod sursa (job #2717439) | Cod sursa (job #2825543)
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
int sol[14], n, nr_sol, ok;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
bool valid(int k, int j){
for(int x = 1; x < k; ++x){
if((sol[x] == j) || abs(k - x) == abs(j - sol[x])){
return 0;
}
}
return 1;
}
void bkt(int k){
if(k == n + 1){
if(!ok){
for(int i = 1; i <= n; ++i){
fout << sol[i] << " ";
}
fout << "\n";
ok = 1;
}
nr_sol++;
return;
}
for(int j = 1; j <= n; ++j){
if(valid(k, j)){
sol[k] = j;
bkt(k + 1);
}
}
}
int main(){
fin >> n;
bkt(1);
fout << nr_sol << "\n";
return 0;
}