<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 {
    unordered_map<int,int>rows,cols,upperDaig,lowerDaig;
public:
    vector<vector<string>> solveNQueens(int n) {
        string str;
        int col=0;
        for(int i=0;i<n;i++)
        {
            str.push_back('.');
        }
        vector<string>output;
        vector<vector<string>>ans;
        solve(ans,n,col,output,str);
        for(int i=0;i<ans.size();i++)
        {
            reverse(ans[i].begin(),ans[i].end());
        }
        return ans;

        
    }
    bool isSafe(int row,int col)
    {
        if(rows.find(row)!=rows.end())
        {
            return false;
        }
        if(cols.find(col)!=cols.end())
        {
            return false;
        }
        if(lowerDaig.find(row+col)!=lowerDaig.end())
        {
            return false;
        }
        if(upperDaig.find(row-col)!=upperDaig.end())
        {
            return false;
        }
        return true;
    }
    void solve(vector<vector<string>>&ans,int n,int col,vector<string>output,string str)
    {
        //base Case when palced Queen
        if(col>=n)
        {
            ans.push_back(output);
            return;
        }
        for(int row=0;row<n;row++)
        {
            if(isSafe(row,col))
            {
                str[row]='Q';
                output.push_back(str);
                str[row]='.';
                rows[row]=1;
                cols[col]=1;
                lowerDaig[row+col]=1;
                upperDaig[row-col]=1;
                solve(ans,n,col+1,output,str);
                output.pop_back();
                rows.erase(row);
                cols.erase(col);
                lowerDaig.erase(row+col);
                upperDaig.erase(row-col);
            }
            
        } 
    }
};
20996