<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:
    bool is_good(const vector<string>& board, int num) {
        vector<bool> rows(num + 1);
        vector<bool> cols(board.front().size());

        for (int i = 0; i < board.size(); i++) {
            for (int j = 0; j < board[i].size(); j++) {
                if (board[i][j] == 'Q') {
                    if (rows[i]) {
                        return false;
                    }
                    else {
                        rows[i] = true;
                    }
                    if (cols[j]) {
                        return false;
                    }
                    else {
                        cols[j] = true;
                    }
                }
            }
        }

        for (int i = 0; i <= num; i++) {
            if (!rows[i]) return false;
        }

        int n = board.size();
        for (int diff = 0; diff < n; diff++) {
            int x1 = 0;
            int y1 = diff;
            bool found1 = false;

            int x2 = diff;
            int y2 = 0;
            bool found2 = false;

            bool found3 = false;
            bool found4 = false;

            while (y1 < n) {
                if (board[x1][y1] == 'Q') {
                    if (found1) return false;
                    found1 = true;
                }
                if (board[x1][n - (y1 + 1)] == 'Q') {
                    if (found3) return false;
                    found3 = true;
                }
                x1++;
                y1++;
            }

            while (x2 < n) {
                if (board[x2][y2] == 'Q') {
                    if (found2) return false;
                    found2 = true;
                }
                if (board[x2][n - (y2 + 1)] == 'Q') {
                    if (found4) return false;
                    found4 = true;
                }
                x2++;
                y2++;
            }
        }
        return true;
    }

    void solver(int n, int num, int& res, vector<string>& board) {
        if (num == n) return;

        for (int i = 0; i < n; i++) {
            board[num][i] = 'Q';
            if (is_good(board, num)) {
                if (num == n - 1) {
                    res++;
                }
                else {
                    solver(n, num + 1, res, board);
                }
            }
            board[num][i] = '.';
        }
    }

    int totalNQueens(int n) {
        int res = 0;
        vector<string> board(n, string(n, '.'));
        solver(n, 0, res, board);
        return res;
    }
};
18172