#include "leetcode.h" class Solution { public: int totalNQueens(int n) { vector col(n, true); vector anti(2 * n - 1, true); vector main(2 * n - 1, true); vector row(n, 0); int count = 0; helper(0, row, col, main, anti, count); return count; } private: void helper(int i, vector &row, vector &col, vector &main, vector &anti, int &count) { if (i == row.size()) { count++; return; } for (int j = 0; j < col.size(); j++) if (col[j] && main[i + j] && anti[i + col.size() - 1 - j]) { row[i] = j; col[j] = main[i + j] = anti[i + col.size() - 1 - j] = false; helper(i + 1, row, col, main, anti, count); col[j] = main[i + j] = anti[i + col.size() - 1 - j] = true; } } }; int main() { Solution obj; cout << obj.totalNQueens(4); }