Pagini recente » Cod sursa (job #2879734) | Cod sursa (job #3230820) | Cod sursa (job #181956) | Cod sursa (job #1563532) | Cod sursa (job #3240733)
#include <bits/stdc++.h>
using namespace std;
const int MAX_NUM = 15;
int n, answer;
vector<int> arr;
bitset<MAX_NUM * 2> usedCol, usedDiag1, usedDiag2;
ifstream fin("damesah.in");
ofstream fout("damesah.out");
void solve(int row) {
if (row == n) {
++answer;
if (answer == 1) {
for (int i = 0; i < n; ++i) {
fout << arr[i] + 1 << " ";
}
fout << "\n";
}
return;
}
for (int col = 0; col < n; ++col) {
if (!usedCol[col] && !usedDiag1[row - col + n - 1] && !usedDiag2[row + col]) {
arr[row] = col;
usedCol[col] = usedDiag1[row - col + n - 1] = usedDiag2[row + col] = true;
solve(row + 1);
usedCol[col] = usedDiag1[row - col + n - 1] = usedDiag2[row + col] = false;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
fin >> n;
arr.resize(n);
solve(0);
fout << answer;
return 0;
}