<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>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</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> 2
<strong>Explanation:</strong> There are two distinct solutions to the 4-queens puzzle as shown.
</pre>

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

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
</pre>

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

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

class Solution {
public:
    bool isqueensettle(int i,int a,vector<string>& ss,int n){
        for(int x=0;x<ss.size();x++){
            if(ss[x][a]=='Q'){
                return false;
            }
            int row=0;
            int col=0;
            for(int y=0;y<n;y++){
                if(ss[x][y]=='Q'){
                   row=x;
                   col=y; 
                }
            }
            int r1 = row+1;
            int c1 =col+1;
            while(r1<n && c1<n){
                if(r1==i && c1==a){
                    return false;
                }
                r1++;
                c1++;
            }
            int r2 = row+1;
            int c2 =col-1;
            while(r2<n && c2>=0){
                if(r2==i && c2==a){
                    return false;
                }
                r2++;
                c2=c2-1;
            }
        }
        return true;
        
    }

    void queen(vector<string>ss ,vector<vector<string>>& ans,int i,int n,string s){
        if(i==n){
            ans.push_back(ss);
            return;
        }
        for(int a=0;a<n;a++){
            if(isqueensettle(i,a,ss,n)){
                s[a]='Q';
                ss.push_back(s);
                s[a]='.';
                queen(ss,ans,i+1,n,s);
                ss.pop_back();
            }
        }
    }
    int totalNQueens(int n) {
        vector<string>ss;
        vector<vector<string>>ans;
        int i=0;
        string s ;
        for(int j=1;j<=n;j++){
            s.push_back('.');
        }
        queen(ss,ans,i,n,s);
        return ans.size() ;
    }
};
17142