<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:
    void diagonal(vector<string> &dp, int i, int j, int n){
        for(int x=0;x<n;x++){
            if(x!=j){
                dp[i][x] = '.';
            }
            if(x!=i){
                dp[x][j] = '.';
            }
            if(x!=0){
                if(i-x>=0 && j-x>=0) dp[i-x][j-x] = '.';
                if(i+x<n && j+x<n) dp[i+x][j+x] = '.';
                if(i-x>=0 && j+x<n) dp[i-x][j+x] = '.';
                if(i+x<n && j-x>=0) dp[i+x][j-x] = '.';
            }
        }
    }
    void find(int i, int &ans, vector<string> &dp, int n){
        if(i>=n){
            ans++;
            return;
        }
        for(int j=0;j<n;j++){
            if(dp[i][j]=='.'){
                continue;
            }else{
                vector<string> k = dp;
                diagonal(k,i,j,n);
                find(i+1,ans,k,n);
            }
        }
    }
    int totalNQueens(int n) {
        string k;
        for(int i=0;i<n;i++){
            k += 'Q';
        }
        vector<string> dp(n,k);
        int ans = 0;
        find(0,ans,dp,n);
        return ans;
    }
};
14567