<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>

void markAttackArea(vector<vector<bool>> &visited, int row, int col, int n) {

    // mark row
    for(int j=0; j<n; j++) {
        visited[row][j] = true;
    }

    // mark column
    for(int i=0; i<n; i++) {
        visited[i][col] = true;
    }

    // mark top right tilted diagonal
    for(int i=row, j=col; i>=0 && j<n; i--, j++) {
        visited[i][j] = true;
    }

    // mark bottom right tilted diagonal
    for(int i=row, j=col; i<n && j>=0; i++, j--) {
        visited[i][j] = true;
    }

    // mark top left tilted diagonal
    for(int i=row, j=col; i>=0 && j>=0; i--, j--) {
        visited[i][j] = true;
    }

    // mark bottom left tilted diagonal
    for(int i=row, j=col; i<n && j<n; i++, j++) {
        visited[i][j] = true;
    }

}

void placeQueens(int n,
    vector<vector<string>> &ans,
    vector<string> chessBoard,
    vector<vector<bool>> visited,
    int queensPlaced, 
    int rowBeingPopulated) {
        if(n == rowBeingPopulated) {
            if(queensPlaced != n) return;
            ans.push_back(chessBoard);
            return;
        }

        for(int j=0; j<n; j++) {
            if(visited[rowBeingPopulated][j]) continue;

            chessBoard[rowBeingPopulated][j] = 'Q';
            vector<string> newChessBoard = chessBoard;

            visited[rowBeingPopulated][j] = true;
            vector<vector<bool>> newVisited = visited;

            markAttackArea(newVisited, rowBeingPopulated, j, n);

            placeQueens(n, ans, newChessBoard, newVisited, queensPlaced+1, rowBeingPopulated+1);

            // backtrack
            chessBoard[rowBeingPopulated][j] = '.';
            visited[rowBeingPopulated][j] = false;
            
        }




}

class Solution {
public:
    vector<vector<string>> solveNQueens(int n) {
        vector<vector<bool>> visited(n, vector<bool>(n, false));
        vector<vector<string>> ans;
        vector<string> chessBoard;

        for(int i=0; i<n; i++) {
            string temp;
            for(int j=0; j<n; j++) {
                temp.push_back('.');
            }
            chessBoard.push_back(temp);
        }

        int queensPlaced = 0;
        int rowBeingPopulated = 0;

        placeQueens(n, ans, chessBoard, visited, queensPlaced, rowBeingPopulated);
        
        return ans;
    }
};
42259