Pagini recente » Cod sursa (job #1786983) | Cod sursa (job #2354598) | Cod sursa (job #2887848) | Cod sursa (job #1821368) | Cod sursa (job #2825519)
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
struct pos{
int i, j;
} vect_pos[14];
int sol[14], n, nr_sol, ok;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
bool valid(int k, int pos2){
for(int x = 1; x < k; ++x){
if(((vect_pos[x].i == k) || (vect_pos[x].j == pos2)) || (abs(k - vect_pos[x].i) == abs(pos2 - vect_pos[x].j))){
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 i = k; i <= n; ++i){
for(int j = 1; j <= n; ++j){
if(valid(i, j)){
sol[i] = j;
vect_pos[k].i = i;
vect_pos[k].j = j;
bkt(k + 1);
}
}
}
}
int main(){
fin >> n;
bkt(1);
fout << nr_sol << "\n";
return 0;
}