<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:
    map<int,int> mp;
    int totalNQueens(int n) {
        vector<vector<char>> board(n,vector<char>(n,'.'));
         vector<int> dp(n,0);
         m(0,board,dp);
         return mp[0];

        
        
    }
    void m(int i ,vector<vector<char>> board,vector<int>& dp){
        if(i == board.size()){
            mp[0]++;
            // for(int p = 0;p<board.size();p++){
            //     for(int p2 = 0;p2<board[0].size();p2++){
            //         cout << board[p][p2] ;
            //     }
            //     cout << '\n';
            // }
            // cout << '\n';
            return ;
        }
        for(int j = 0;j<board.size();j++){
            if(dp[j] != 1 && isvalid(i,j,board)){
                board[i][j] = 'Q';
                dp[j]=1;
                m(i+1,board,dp);
                dp[j] = 0;
                board[i][j] = '.';
            }
        }

    }
    bool isvalid(int i ,int j,vector<vector<char>> board){
        for(int k = 1;k<9;k++){
            if(i+k < board.size() && j+k < board[0].size()){
                if(board[i+k][j+k] == 'Q'){
                    return false;
                }
            }
            if(i-k >=0 && j-k >=0){
                if(board[i-k][j-
                k] == 'Q'){
                    return false;
                }

            }
             if(i+k < board.size() &&  j-k >=0){
                if(board[i+k][j-k] == 'Q'){
                    return false;
                }

            }
            if(i-k >=0  && j+k < board[0].size()){
                if(board[i-k][j+k] == 'Q'){
                    return false;
                }

            }
            
        }
        return true;
    }
};
39802