<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:

    bool check_ans(vector<string> temp2, int n)
    {
        int count_q = 0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(temp2[i][j]=='Q')
                count_q++;
            }
        }

        if(count_q == n)
        return true;
        
        return false;
    }
    void mark_as_per_q(vector<string>& temp2, int idx, int i, int n) {
        for(int j=0;j<n;j++)
        temp2[idx][j] = '.';

        for(int j=0;j<n;j++)
        temp2[j][i] = '.';

        int j=idx, k=i;

        while(j>=0&&k>=0)
        {
            temp2[j--][k--] = '.';
        }

        j=idx, k=i;

        while(j<n&&k<n)
        {
            temp2[j++][k++] = '.';
        }

        j=idx, k=i;

        while(j<n&&k>=0)
        {
            temp2[j++][k--] = '.';
        }

        j=idx, k=i;

        while(j>=0&&k<n)
        {
            temp2[j--][k++] = '.';
        }

        temp2[idx][i] = 'Q';
        
        return;

    }

    void rec_solveNQueens(int n, int idx, vector<string>& temp,
                          set<vector<string>>& ans) {
        if (idx >= n)
            return;

        for (int i = 0; i < n; i++) {
            if (temp[idx][i] == '1') 
            {
                temp[idx][i] = 'Q';
                vector<string> temp2 = temp;

                mark_as_per_q(temp2, idx, i, n);

                rec_solveNQueens(n, idx + 1, temp2, ans);
                
                if (idx == n - 1)
                {
                    bool check = check_ans(temp2, n);

                    if(check == true)
                    ans.insert(temp2);
                }
                
                temp[idx][i] = '1';
            }
        }
    }

    vector<vector<string>> solveNQueens(int n) {
        set<vector<string>> ans;
        vector<vector<string>> ans2;

        if (n == 2 || n==3)
        return ans2;

        string s = "";
        for (int i = 0; i < n; i++)
        s.push_back('1');
        
        vector<string> temp(n,s);
        
        rec_solveNQueens(n, 0, temp, ans);

        
        for(auto i: ans)
        {
            ans2.push_back(i);
        }
        
        return ans2;
    }
};
16440