Pagini recente » Cod sursa (job #2325549) | Cod sursa (job #512918) | Cod sursa (job #831683) | Cod sursa (job #1566920) | Cod sursa (job #2266859)
#include <cstdio>
#include <bitset>
using namespace std;
FILE *f = fopen("q.in","r");
FILE *g = fopen("qq.out","w");
int n;
int nrsol;
bitset < 40 > uc,udp,uds;
int sol[14];
void back(int lin) {
if(lin == n + 1){
if(!nrsol) {
for(int i = 1; i <= n; i++) {
fprintf(g, "%d ", sol[i]);
}
fprintf(g, "\n");
}
nrsol++;
return;
} else {
for(int col = 1; col <= n; col++) {
int x = col - lin + n - 1;
int y = lin + col;
if(!uc[col] && !udp[x] && !uds[y]) {
sol[lin] = col;
uc[col] = udp[x] = uds[y] = true;
back(lin + 1);
uc[col] = udp[x] = uds[y] = false;
}
}
}
}
int main(){
fscanf(f, "%d", &n);
back(1);
fprintf(g, "%d", nrsol);
fclose(f);
fclose(g);
return 0;
}