<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>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <strong>any order</strong>.</p>

<p>Each solution contains a distinct board configuration of the n-queens&#39; placement, where <code>&#39;Q&#39;</code> and <code>&#39;.&#39;</code> both indicate a queen and an empty space, respectively.</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> [[&quot;.Q..&quot;,&quot;...Q&quot;,&quot;Q...&quot;,&quot;..Q.&quot;],[&quot;..Q.&quot;,&quot;Q...&quot;,&quot;...Q&quot;,&quot;.Q..&quot;]]
<strong>Explanation:</strong> There exist two distinct solutions to the 4-queens puzzle as shown above
</pre>

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

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> [[&quot;Q&quot;]]
</pre>

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

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

class Solution 
{
public:
    vector<vector<string>> solveNQueens(const int n)
    {
        vector<string> board(n, string(n, '.'));
        vector<vector<int>> blocked(n, vector<int>(n));
        set<vector<string>> result;
        set<vector<string>> memo;

        for (int i = 0; i < n; ++i)
        {
            blockPositions(0, i, blocked);
            board[0][i] = 'Q';

            doBacktracking(1, 1, board, blocked, result, memo);

            board[0][i] = '.';
            unblockPositions(0, i, blocked);
        }

        return vector<vector<string>> { result.begin(), result.end() };
    }

private:
    void setBlocked(const int x, const int y, const bool isBlocked, vector<vector<int>> &blocked)
    {
        constexpr int directions[8][2] 
        {
            {  1, 0 }, {  0,  1 },
            { -1, 0 }, {  0, -1 },
            {  1, 1 }, {  1, -1 },
            { -1, 1 }, { -1, -1 } 
        };

        const auto n = static_cast<int>(blocked.size());

        vector<pair<int, int>> positions;

        for (int i = 0; i < 8; ++i)
        {
            int nx = x;
            int ny = y;

            while (nx >= 0 && ny >= 0 && nx < n && ny < n)
            {
                blocked[nx][ny] += isBlocked ? 1 : -1;

                nx += directions[i][0];
                ny += directions[i][1];
            }
        }
    }

    void blockPositions(const int x, const int y, vector<vector<int>> &blocked) { setBlocked(x, y, true, blocked); }
    void unblockPositions(const int x, const int y, vector<vector<int>> &blocked) { setBlocked(x, y, false, blocked); }

    void doBacktracking(const int row, const int queens,
                        vector<string> &board, vector<vector<int>> &blocked,
                        set<vector<string>> &result, set<vector<string>> &memo)
    {
        const int n = static_cast<int>(board.size());

        if (queens == n)
        {
            result.insert(board);
            return;
        }

        if (row == n)
        {
            return;
        }

        if (memo.count(board))
        {
            return;
        }

        memo.insert(board);

        for (int i = 0; i < n; ++i)
        {
            if (blocked[row][i] == 0)
            {
                blockPositions(row, i, blocked);
                board[row][i] = 'Q';

                doBacktracking(row + 1, queens + 1, board, blocked, result, memo);

                board[row][i] = '.';
                unblockPositions(row, i, blocked);
            }
        }
    }
};

16946