<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:
//3 map to check if the queen is not place in those directions
unordered_map<int,bool>rowCheck;
unordered_map<int,bool>topDiagnol;
unordered_map<int,bool>bottomDiagnol;
 
 bool isSafe(int row,int col,vector<vector<char>>&board){
    if(rowCheck[row]==true){
        //means if same row k andar koi aur queen placed h 
        return false;
    }
    if(topDiagnol[col-row]==true){
        return false;
    }
    if(bottomDiagnol[row+col]==true){
        return false;
    }
    //place is safe 
    return true;

 }
 void storeSolution(vector<vector<char>>&board,vector<vector<string>>&ans){
   vector<string>tempSol;
    for(auto vec:board){
        string temp="";
        for(auto ch:vec){
            temp.push_back(ch);
        }
        //string hoi usko tempSol m push krege
        tempSol.push_back(temp);
    }
    ans.push_back(tempSol);
 }
void solve(vector<vector<char>>&board,vector<vector<string>>&ans,int n,int col){
         //base case 
         if(col>=n){
            //saare column process hogye h
            //isla ntlb we have a solution to store
            storeSolution(board,ans);
            return;         }
            //main logiv
            //isse col k hr row pr queen place krke dekho
            for(int row=0;row<n;row++){
                if(isSafe(row,col,board)){
                //tin directions ka dhyan rkhna pdega and queen ko place krna pdega 
                board[row][col]='Q';
                rowCheck[row]=true;
                topDiagnol[col-row]=true;
                bottomDiagnol[row+col]=true;
                //recursion 
                solve(board,ans,n,col+1);
                //agr glt jgh pe place ki h toh mtlb backtrack krna pdega
                board[row][col]='.';
                rowCheck[row]=0;
                topDiagnol[col-row]=0;
                bottomDiagnol[row+col]=0;
                }
            }
}
    vector<vector<string>> solveNQueens(int n) {
        vector<vector<string>>ans;
        vector<vector<char>>board(n,vector<char>(n,'.'));
        int col=0;

        solve(board,ans,n,col);
        return ans;

    }
};
12390