Pagini recente » Cod sursa (job #1609530) | Cod sursa (job #434351) | Cod sursa (job #3182266) | Cod sursa (job #2482108) | Cod sursa (job #2830124)
#include <iostream>
#include <fstream>
#define MAX 20
using namespace std;
int n,st[MAX],nrsol,f[MAX];
ifstream fin("damesah.in");
ofstream fout("damesah.out");
bool verif(int pas){
for(int i = 1; i < pas; i++){
if(pas-i == abs(st[pas]-st[i])){
return false;
}
}
return true;
}
void gen(int pas){
if(pas == n+1){
nrsol++;
if(nrsol == 1){
for(int i = 1; i <= n; i++){
fout << st[i] << " ";
}
fout << "\n";
}
}else{
for(int i = 1; i <= n; i++){
if(f[i] == 0){
f[i] = 1;
st[pas] = i;
if(verif(pas)){
gen(pas+1);
}
f[i] = 0;
}
}
}
}
int main()
{
fin >> n;
gen(1);
fout << nrsol << "\n";
return 0;
}