<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(int n) {
        vector<vector<pair<int, int>>> solutions;
        vector<pair<int, int>> currentSolution;
        vector<vector<bool>> boardOfPossibilities(n, vector<bool>(n, true));
        solutions = solve(boardOfPossibilities, currentSolution);
        return formatSolutions(solutions, n);
    }

private:
    vector<vector<bool>> placeQueen(const vector<vector<bool>>& board, int x, int y) {
        vector<vector<bool>> newBoard = board;
        int boardSize = newBoard.size();
        for (int i = 0; i < boardSize; i++) {
            int diag1 = x - (y - i);
            int diag2 = x + (y - i);
            if (diag1 >= 0 && diag1 < boardSize) { newBoard[i][diag1] = false; }
            if (diag2 >= 0 && diag2 < boardSize) { newBoard[i][diag2] = false; }
            if (i == y) { for (int j = 0; j < boardSize; j++) { newBoard[i][j] = false; } }
            newBoard[i][x] = false;
        }
        return newBoard;
    }

    vector<pair<int, int>> findPossibilities(const vector<vector<bool>>& board) {
        vector<pair<int, int>> possibilities;

        // Iterate through the board
        for (int i = 0; i < board.size(); ++i) {
            for (int j = 0; j < board[i].size(); ++j) {
                if (board[i][j]) {
                    // If the value is true, store the coordinates (i, j)
                    possibilities.emplace_back(i, j);
                }
            }
        }

        return possibilities;
    }

    vector<vector<pair<int, int>>> solve(const vector<vector<bool>>& board, vector<pair<int, int>>& currentSolution, int row = 0) {
        int n = board.size();
        vector<vector<pair<int, int>>> results;

        // Base case: If we've placed all queens, add the current solution to the results.
        if (row == n) {
            results.push_back(currentSolution);
            return results;
        }

        // Find possible positions to place a queen in the current row
        for (int col = 0; col < n; ++col) {
            if (board[row][col]) {
                // Place the queen and mark the board
                currentSolution.emplace_back(row, col);
                vector<vector<bool>> newBoard = placeQueen(board, col, row);

                // Recursively solve for the next row
                vector<vector<pair<int, int>>> subResults = solve(newBoard, currentSolution, row + 1);

                // Add all the sub-results to the main results
                results.insert(results.end(), subResults.begin(), subResults.end());

                // Backtrack: Remove the queen from the current solution
                currentSolution.pop_back();
            }
        }

        return results;
    }

    vector<vector<string>> formatSolutions(const vector<vector<pair<int, int>>>& solutions, int n) {
        vector<vector<string>> result;
        for (const vector<pair<int, int>>& solution : solutions) {
            vector<string> board(n, string(n, '.'));
            for (const auto& [y, x] : solution) {
                board[y][x] = 'Q';
            }
            result.push_back(board);
        }
        return result;
    }
};
23528