#include <iostream>
void tryQueen(int col, int n, int* trace, bool* rowFlag, bool* pDiaFlag, bool* sDiaFlag, bool &found) {
if (col == n) {
std::cout << "Position of " << n << " queens (row, col): \n";
for (int c = 0; c < n; ++c) {
int r = trace[c];
std::cout << "(" << r << ", " << c << ")\n";
}
found = true;
std::cout << "\nDrawing the board...\n";
int* traceRow = new int[n]();
for (int c = 0; c < n; ++c) {
traceRow[trace[c]] = c;
}
for (int r = 0; r < n; ++r) {
for (int c = 0; c < n; ++c) {
std::cout << (traceRow[r] == c ? '*' : '.') << ' ';
}
std::cout << '\n';
}
delete[] traceRow;
return;
}
for (int row = 0; row < n; ++row) {
if (!rowFlag[row] && !pDiaFlag[n-1+(col-row)] && !sDiaFlag[n*2-2-(row+col)]) {
trace[col] = row;
rowFlag[row] = true;
pDiaFlag[n-1+(col-row)] = true;
sDiaFlag[n*2-2-(row+col)] = true;
tryQueen(col+1, n, trace, rowFlag, pDiaFlag, sDiaFlag, found);
rowFlag[row] = false;
pDiaFlag[n-1+(col-row)] = false;
sDiaFlag[n*2-2-(row+col)] = false;
if (found) {
return;
}
}
}
}
int main() {
int n;
std::cout << "Enter n: ";
std::cin >> n;
int* trace = new int[n]();
bool* rowFlag = new bool[n]();
bool* pDiaFlag = new bool[n*2-1]();
bool* sDiaFlag = new bool[n*2-1]();
bool found = false;
tryQueen(0, n, trace, rowFlag, pDiaFlag, sDiaFlag, found);
if (!found) {
std::cout << "Solution not found!\n";
}
delete[] trace;
delete[] rowFlag;
delete[] pDiaFlag;
delete[] sDiaFlag;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKdm9pZCB0cnlRdWVlbihpbnQgY29sLCBpbnQgbiwgaW50KiB0cmFjZSwgYm9vbCogcm93RmxhZywgYm9vbCogcERpYUZsYWcsIGJvb2wqIHNEaWFGbGFnLCBib29sICZmb3VuZCkgewogICAgaWYgKGNvbCA9PSBuKSB7CiAgICAgICAgc3RkOjpjb3V0IDw8ICJQb3NpdGlvbiBvZiAiIDw8IG4gPDwgIiBxdWVlbnMgKHJvdywgY29sKTogXG4iOwogICAgICAgIGZvciAoaW50IGMgPSAwOyBjIDwgbjsgKytjKSB7CiAgICAgICAgICAgIGludCByID0gdHJhY2VbY107CiAgICAgICAgICAgIHN0ZDo6Y291dCA8PCAiKCIgPDwgciA8PCAiLCAiIDw8IGMgPDwgIilcbiI7CiAgICAgICAgfQogICAgICAgIGZvdW5kID0gdHJ1ZTsKCiAgICAgICAgc3RkOjpjb3V0IDw8ICJcbkRyYXdpbmcgdGhlIGJvYXJkLi4uXG4iOwogICAgICAgIGludCogdHJhY2VSb3cgPSBuZXcgaW50W25dKCk7CiAgICAgICAgZm9yIChpbnQgYyA9IDA7IGMgPCBuOyArK2MpIHsKICAgICAgICAgICAgdHJhY2VSb3dbdHJhY2VbY11dID0gYzsKICAgICAgICB9CiAgICAgICAgZm9yIChpbnQgciA9IDA7IHIgPCBuOyArK3IpIHsKICAgICAgICAgICAgZm9yIChpbnQgYyA9IDA7IGMgPCBuOyArK2MpIHsKICAgICAgICAgICAgICAgIHN0ZDo6Y291dCA8PCAodHJhY2VSb3dbcl0gPT0gYyA/ICcqJyA6ICcuJykgPDwgJyAnOyAKICAgICAgICAgICAgfQogICAgICAgICAgICBzdGQ6OmNvdXQgPDwgJ1xuJzsKICAgICAgICB9CiAgICAgICAgZGVsZXRlW10gdHJhY2VSb3c7CgogICAgICAgIHJldHVybjsKICAgIH0KICAgIGZvciAoaW50IHJvdyA9IDA7IHJvdyA8IG47ICsrcm93KSB7CiAgICAgICAgaWYgKCFyb3dGbGFnW3Jvd10gJiYgIXBEaWFGbGFnW24tMSsoY29sLXJvdyldICYmICFzRGlhRmxhZ1tuKjItMi0ocm93K2NvbCldKSB7CiAgICAgICAgICAgIHRyYWNlW2NvbF0gPSByb3c7CiAgICAgICAgICAgIHJvd0ZsYWdbcm93XSA9IHRydWU7CiAgICAgICAgICAgIHBEaWFGbGFnW24tMSsoY29sLXJvdyldID0gdHJ1ZTsKICAgICAgICAgICAgc0RpYUZsYWdbbioyLTItKHJvdytjb2wpXSA9IHRydWU7CiAgICAgICAgICAgIHRyeVF1ZWVuKGNvbCsxLCBuLCB0cmFjZSwgcm93RmxhZywgcERpYUZsYWcsIHNEaWFGbGFnLCBmb3VuZCk7CiAgICAgICAgICAgIHJvd0ZsYWdbcm93XSA9IGZhbHNlOwogICAgICAgICAgICBwRGlhRmxhZ1tuLTErKGNvbC1yb3cpXSA9IGZhbHNlOwogICAgICAgICAgICBzRGlhRmxhZ1tuKjItMi0ocm93K2NvbCldID0gZmFsc2U7CiAgICAgICAgICAgIGlmIChmb3VuZCkgewogICAgICAgICAgICAgICAgcmV0dXJuOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQp9CgppbnQgbWFpbigpIHsKICAgIGludCBuOwogICAgc3RkOjpjb3V0IDw8ICJFbnRlciBuOiAiOwogICAgc3RkOjpjaW4gPj4gbjsKCiAgICBpbnQqIHRyYWNlID0gbmV3IGludFtuXSgpOwogICAgYm9vbCogcm93RmxhZyA9IG5ldyBib29sW25dKCk7CiAgICBib29sKiBwRGlhRmxhZyA9IG5ldyBib29sW24qMi0xXSgpOwogICAgYm9vbCogc0RpYUZsYWcgPSBuZXcgYm9vbFtuKjItMV0oKTsKICAgIGJvb2wgZm91bmQgPSBmYWxzZTsKCiAgICB0cnlRdWVlbigwLCBuLCB0cmFjZSwgcm93RmxhZywgcERpYUZsYWcsIHNEaWFGbGFnLCBmb3VuZCk7CgogICAgaWYgKCFmb3VuZCkgewogICAgICAgIHN0ZDo6Y291dCA8PCAiU29sdXRpb24gbm90IGZvdW5kIVxuIjsKICAgIH0KCiAgICBkZWxldGVbXSB0cmFjZTsKICAgIGRlbGV0ZVtdIHJvd0ZsYWc7CiAgICBkZWxldGVbXSBwRGlhRmxhZzsKICAgIGRlbGV0ZVtdIHNEaWFGbGFnOwoKICAgIHJldHVybiAwOwp9