Pagini recente » Cod sursa (job #1648618) | Istoria paginii runda/simulare-oji-10-2023/clasament | Cod sursa (job #1119079) | Cod sursa (job #2499746) | Cod sursa (job #2719097)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
const int NMax = 13;
int n, nr;
bool k;
int st[NMax + 5];
bool column[NMax + 5], principal[2 * NMax + 5], secondary[2 * NMax + 5];
void Read(){
fin >> n;
}
void Print(){
for (int i = 1; i <= n; i++)
fout << st[i] << ' ';
fout << '\n';
}
void Backtracking(int r){
for (int c = 1; c <= n; c++){
if (column[c]|| principal[r - c + n] || secondary[r + c])
continue;
st[r] = c;
if (r == n){
if (!k){
Print();
k = 1;
}
nr++;
}
else{
column[c] = principal[r - c + n] = secondary[r + c] = 1;
Backtracking(r + 1);
column[c] = principal[r - c + n] = secondary[r + c] = 0;
}
}
}
int main(){
Read();
Backtracking(1);
fout << nr << '\n';
return 0;
}