Pagini recente » Cod sursa (job #2245499) | Cod sursa (job #698195) | Cod sursa (job #107520) | Cod sursa (job #299443) | Cod sursa (job #2471896)
// Sa se aranjeze n dame pe o tabla de sah n*n astfel incat sa nu se atace.
#include <fstream>
#include <cmath>
using namespace std;
ifstream fin ("damesah.in");
ofstream fout ("damesah.out");
int nrSolutii;
int regina[15];
bool col[15], diagP[30], diagS[30];
void back(int l, int n) {
int j;
if (l == n) {
if (nrSolutii < 1) {
for (j = 0; j < n; j++)
fout << regina[j] + 1 << ' ';
fout << '\n';
}
nrSolutii++;
} else {
for (j = 0; j < n; j++) {
if ((!col[j]) && (!diagP[j - l + n - 1]) && (!diagS[l + j])) {
regina[l] = j;
col[j] = diagP[j - l + n - 1] = diagS[l + j] = true;
back(l + 1, n);
col[j] = diagP[j - l + n - 1] = diagS[l + j] = false;
}
}
}
}
int main() {
int n;
fin >> n;
back(0, n);
fout << nrSolutii;
fin.close();
fout.close();
return 0;
}