<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:
    void fill(int n, int r, int c, vector<vector<bool>>& availability){
        int i=r;int j=c;
        availability[r][c] = false;
        while(j<n){
            availability[i][j] = false;
            j++;
        }
        j=c;
        while(j>=0){
            availability[i][j] = false;
            j--;
        }
        j=c;
        while(i<n){
            availability[i][j] = false;
            i++;
        }
        i=r;
        while(i>=0){
            availability[i][j] = false;
            i--;
        }
        i=r;j=c;
        while(i<n && j<n){
            availability[i][j] = false;
            i++;
            j++;
        }
        i=r;j=c;
        while(i<n && j>=0){
            availability[i][j] = false;
            i++;
            j--;
        }
        i=r;j=c;
        while(i>=0 && j<n){
            availability[i][j] = false;
            i--;
            j++;
        }
        i=r;j=c;
        while(i>=0 && j>=0){
            availability[i][j] = false;
            i--;
            j--;
        }

    }
    void print(vector<vector<bool>> a){
        for(int i=0;i<a.size();i++){
            for(int j=0;j<a[0].size();j++){
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
    }
    void printm(vector<vector<int>> a){
        for(int i=0;i<a.size();i++){
            for(int j=0;j<a[0].size();j++){
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
    }
    bool find(int n, int row, vector<vector<bool>> availability,vector<vector<int>>& matrix, vector<vector<vector<int>>>& ans){
        if(row>=n) {
            // cout<<"Pushhing matrix"<<endl;
            // printm(matrix);
            // cout<<endl;
            ans.push_back(matrix);return true;}
        bool answer = false;
        for(int i=0;i<n;i++){
            if(availability[row][i]==true){
                vector<vector<int>> board = matrix;
                vector<vector<bool>> old = availability;
                matrix[row][i] = 1;
                availability[row][i] = false;
                fill(n,row,i,availability);
                // cout<<"=======Matrix========="<<endl;
                // printm(matrix);
                // cout<<"=======Availability========="<<endl;
                // print(availability);
                // cout<<"--------------------------"<<endl;

                if(find(n,row+1,availability,matrix,ans)==true) {answer = true;}
                availability = old;
                matrix = board;
            }
        }
        return answer;
    }
    vector<vector<string>> solveNQueens(int n) {
        vector<vector<bool>> availability(n,vector<bool>(n,true));
        vector<vector<int>> matrix(n,vector<int>(n,0));
        vector<vector<vector<int>>> ans;
        bool f = find(n,0,availability,matrix,ans);
        // cout<<"printing ans[0]"<<endl;
        // printm(ans[0]);
        vector<vector<string>> ret;
        for(int i=0;i<ans.size();i++){

            vector<string> tmp;
            for(int j=0;j<n;j++){
                string q="";
                for(int k=0;k<n;k++){
                    if(ans[i][j][k]==0) q+=".";
                    else q+="Q";
                }
                tmp.push_back(q);
            }
            ret.push_back(tmp);
        }
        return ret;

    }
};
44284