Pagini recente » Cod sursa (job #3251553) | Cod sursa (job #594746) | Cod sursa (job #2633480) | Cod sursa (job #2181827) | Cod sursa (job #1450963)
#include <fstream>
#define VM 14
#define pb push_back
using namespace std;
ifstream f("damesah.in");
ofstream g("damesah.out");
int n;
int nrSol = 0;
bool ok = true;
int column[VM], line[VM];
int d1[2 * VM], d2[2 * VM];
int rez[VM];
int size = 1;
void place(int i, int j, int x){
line[i] += x;
column[j] += x;
d1[i - j + n] += x;
d2[i + j] += x;
}
bool isFree(int i, int j){
return (line[i] == 0 && column[j] == 0 && d1[i - j + n] == 0 && d2[i + j] == 0);
}
void bkt(int i){
///Stop
if(i > n){
ok = false;
++nrSol;
return;
}
///
for(int j = 1 ; j <= n ; ++j){
if(isFree(i , j)){
place(i , j , 1);
if(ok){
rez[size] = j;
++size;
}
bkt(i + 1);
place(i , j , -1);
if(ok)
--size;
}
}
}
int main()
{
f>>n;
bkt(1);
for(int i = 1 ; i < size ; ++i)
g<<rez[i]<<' ';
g<<'\n'<<nrSol;
return 0;
}