#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
void solve(int i, int& n, vector<string>& tmp, vector<vector<string>>& ans, vector<bool>& c, vector<bool>& md, vector<bool>& td) {
if (i == n) {
ans.push_back(tmp);
return;
}
string str(n, '.');
for (int j = 0; j < n; j++) {
if ((c[j] == false && md[i - j + n] == false && td[i + j] == false)) {
str[j] = 'Q';
c[j] = true;
md[i - j + n] = true;
td[i + j] = true;
tmp.push_back(str);
solve(i + 1, n, tmp, ans, c, md, td);
tmp.pop_back();
str[j] = '.';
c[j] = false;
md[i - j + n] = false;
td[i + j] = false;
}
}
}
int totalNQueens(int n) {
vector<vector<string>> ans;
vector<string> tmp;
vector<bool> c(n, false), md(2 * n + 3, false), td(2 * n + 3, false);
solve(0, n, tmp, ans, c, md, td);
return ans.size();
}
};
int main() {
int n = 1;
Solution s;
int totalSolutions = s.totalNQueens(n);
cout << "Total number of solutions for " << n << " queens: " << totalSolutions << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwpjbGFzcyBTb2x1dGlvbiB7CnB1YmxpYzoKICAgIHZvaWQgc29sdmUoaW50IGksIGludCYgbiwgdmVjdG9yPHN0cmluZz4mIHRtcCwgdmVjdG9yPHZlY3RvcjxzdHJpbmc+PiYgYW5zLCB2ZWN0b3I8Ym9vbD4mIGMsIHZlY3Rvcjxib29sPiYgbWQsIHZlY3Rvcjxib29sPiYgdGQpIHsKICAgICAgICBpZiAoaSA9PSBuKSB7CiAgICAgICAgICAgIGFucy5wdXNoX2JhY2sodG1wKTsKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIH0KICAgICAgICBzdHJpbmcgc3RyKG4sICcuJyk7CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBuOyBqKyspIHsKICAgICAgICAgICAgaWYgKChjW2pdID09IGZhbHNlICYmIG1kW2kgLSBqICsgbl0gPT0gZmFsc2UgJiYgdGRbaSArIGpdID09IGZhbHNlKSkgewogICAgICAgICAgICAgICAgc3RyW2pdID0gJ1EnOwogICAgICAgICAgICAgICAgY1tqXSA9IHRydWU7CiAgICAgICAgICAgICAgICBtZFtpIC0gaiArIG5dID0gdHJ1ZTsKICAgICAgICAgICAgICAgIHRkW2kgKyBqXSA9IHRydWU7CiAgICAgICAgICAgICAgICB0bXAucHVzaF9iYWNrKHN0cik7CiAgICAgICAgICAgICAgICBzb2x2ZShpICsgMSwgbiwgdG1wLCBhbnMsIGMsIG1kLCB0ZCk7CiAgICAgICAgICAgICAgICB0bXAucG9wX2JhY2soKTsKICAgICAgICAgICAgICAgIHN0cltqXSA9ICcuJzsKICAgICAgICAgICAgICAgIGNbal0gPSBmYWxzZTsKICAgICAgICAgICAgICAgIG1kW2kgLSBqICsgbl0gPSBmYWxzZTsKICAgICAgICAgICAgICAgIHRkW2kgKyBqXSA9IGZhbHNlOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgaW50IHRvdGFsTlF1ZWVucyhpbnQgbikgewogICAgICAgIHZlY3Rvcjx2ZWN0b3I8c3RyaW5nPj4gYW5zOwogICAgICAgIHZlY3RvcjxzdHJpbmc+IHRtcDsKICAgICAgICB2ZWN0b3I8Ym9vbD4gYyhuLCBmYWxzZSksIG1kKDIgKiBuICsgMywgZmFsc2UpLCB0ZCgyICogbiArIDMsIGZhbHNlKTsKICAgICAgICBzb2x2ZSgwLCBuLCB0bXAsIGFucywgYywgbWQsIHRkKTsKICAgICAgICByZXR1cm4gYW5zLnNpemUoKTsKICAgIH0KfTsKCmludCBtYWluKCkgewogICAgaW50IG4gPSAxOwogICAgU29sdXRpb24gczsKICAgIGludCB0b3RhbFNvbHV0aW9ucyA9IHMudG90YWxOUXVlZW5zKG4pOwogICAgY291dCA8PCAiVG90YWwgbnVtYmVyIG9mIHNvbHV0aW9ucyBmb3IgIiA8PCBuIDw8ICIgcXVlZW5zOiAiIDw8IHRvdGFsU29sdXRpb25zIDw8IGVuZGw7CiAgICByZXR1cm4gMDsKfQo=