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

#include<bits/stdc++.h>
using namespace std;

class Solution {
private:

    void solve1(vector<vector<int>> &grid, int k, int l){
        int n=grid.size();

        for(int i=-10;i<=10;++i){
            if(k+i>=0 && k+i<n)
                if(grid[k+i][l]<2) grid[k+i][l] = 1;
            if(l+i>=0 && l+i<n)
                if(grid[k][l+i]<2) grid[k][l+i] = 1;
        }

        for(int i=-10;i<=10;++i){
            if(k+i>=0 && k+i<n && l+i>=0 && l+i<n)
                if(grid[k+i][l+i]<2) grid[k+i][l+i] = 1;
            if(l-i>=0 && l-i<n && k+i>=0 && k+i<n)
                if(grid[k+i][l-i]<2) grid[k+i][l-i] = 1;
        }

        grid[k][l]=2;
        //
        //cout<<"Done\n";
    }

    void solve2(vector<vector<int>> &grid, vector<vector<string>> &ans){
        vector<string> ans1;
        int n=grid.size();

        for(int i=0;i<n;++i){
            string ans2;

            for(int j=0;j<n;++j)
                if(grid[i][j]==2) ans2.push_back('Q');
                else ans2.push_back('.');

            ans1.push_back(ans2);
        }

        ans.push_back(ans1);
    }

    void solve(vector<vector<int>> grid, vector<vector<string>> &ans, int i, int rem){
        if(rem==0){
            solve2(grid,ans);
            return;
        }

        int n=grid.size();

        for(int k=0;k<n;++k){

            if(grid[i][k] == 0){
                auto grid1=grid;
                solve1(grid1,i,k);

                solve(grid1,ans,i+1,rem-1);
            }
        }
    }
public:
    vector<vector<string>> solveNQueens(int n) {
        vector<vector<int>> grid(n,vector<int>(n,0));

        vector<vector<string>> ans;

        solve(grid,ans,0,n);

        return ans;
    }
};
34159