<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>

<p>Given an integer <code>n</code>, return <em>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/queens.jpg" style="width: 600px; height: 268px;" />
<pre>
<strong>Input:</strong> n = 4
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two distinct solutions to the 4-queens puzzle as shown.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 9</code></li>
</ul>

class Solution {
public:
    int totalNQueens(int n) {
        vector<vector<vector<int>>> solutions;
        set<int> used_cols;
        set<int> used_diagonals;
        set<int> used_diagonals_2;
        vector<vector<int>> starter(n, vector<int>(n));
        helper(starter, 0, used_cols, used_diagonals, used_diagonals_2, n, solutions);

        vector<vector<string>> final_solution;
        for (int i = 0; i < solutions.size(); ++i) {
            vector<string> current_solution;
            for (int j = 0; j < n; ++j) {
                string current_string = "";
                for (int k = 0; k < n; ++k) {
                    if (solutions[i][j][k] == 1) {
                        current_string += "Q";
                    } else {
                        current_string += ".";
                    }
                }
                current_solution.push_back(current_string);
            }
            final_solution.push_back(current_solution);
        }
        return final_solution.size();
    }

    void helper(vector<vector<int>> current, int num_placed, set<int> used_cols, set<int> used_diagonals, set<int> used_diagonals_2, int n, vector<vector<vector<int>>>& solutions) {
        std::cout << "New one: " << std::endl;
        for (int i = 0; i < current.size(); ++i) {
            for (int j = 0; j < current[0].size(); ++j) {
                std::cout << current[i][j] << " ";
            }
            std::cout << std::endl;
        }
        std::cout << "Stack: ";

        for (const auto& item : used_cols) {
            std::cout << item << " ";
        }
        std::cout << std::endl << std::endl;


        if (num_placed == n) {
            solutions.push_back(current);
        }
        for (int i = 0; i < current.size(); ++i) {
            if (used_cols.find(i) == used_cols.end() &&
                used_diagonals.find(i + num_placed) == used_diagonals.end() &&
                used_diagonals_2.find(i - num_placed) == used_diagonals_2.end()) {
                used_cols.insert(i);
                used_diagonals.insert(i + num_placed);
                used_diagonals_2.insert(i - num_placed);
                current[num_placed][i] = 1;
                helper(current, num_placed + 1, used_cols, used_diagonals, used_diagonals_2, n, solutions);
                used_cols.erase(i);
                used_diagonals.erase(i + num_placed);
                used_diagonals_2.erase(i - num_placed);
                current[num_placed][i] = 0;
            }
        }
    }
};
40832