<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 solution_appender(vector<vector<char>> &chessboard , int board_size,vector<vector<string>> &final_ans)
    {
        //after i get a chessboard config which works i will traverse it row wise and attach each row string to 
        //an empty array. After all rows traversed, i will move that array to our solution array
        vector<string> out;
        for(int i =0;i<board_size;i++)
        {
            string output = "";
            for(int j=0;j<board_size;j++)
            {
                output+=chessboard[i][j];
            }
            out.push_back(output);
        }
        final_ans.push_back(out);
    }

    bool isSafe(int row, int col, vector<vector<bool>> &safe_move,int board_size)
    {
        //check along same row
        for(int i =0;i<board_size;i++)
        {
            if(safe_move[row][i]) return false;
        }
        //check along same col
        for(int i=0;i<board_size;i++)
        {
            if(safe_move[i][col]) return false;
        }
        //check diagonals

        //check diagonals up left, up right, down left, down right

        //up left
        int i = row, j = col;
        while(i!=-1 && j!=-1)
        {
            if(safe_move[i][j]) return false;
            i--;
            j--;
        }

        //up right
        i = row,j=col;
        while(i!=-1 && j!=board_size)
        {
            if(safe_move[i][j]) return false;
            i--;
            j++;
        }

        //down left
        i=row, j = col;
        while(i!=board_size && j!=-1)
        {
            if(safe_move[i][j]) return false;
            i++;
            j--;
        }

        //down right

        i=row, j =col;
        while(i!=board_size && j!=board_size)
        {
            if(safe_move[i][j]) return false;
            i++;
            j++;
        }
        return true;
    }
    void solve(vector<vector<char>> &chessboard ,int row, int board_size,vector<vector<string>> &final_ans,vector<vector<bool>> &safe_move)
    {
        //base case : if i am able to place all queens;
        if(row == board_size)
        {
            solution_appender(chessboard,board_size,final_ans);
            return;
        }
        for(int col =0;col<board_size;col++)
        {
            //first check if position is safe
            if(isSafe(row,col,safe_move,board_size))
            {
                chessboard[row][col] = 'Q';
                safe_move[row][col] = true;
                solve(chessboard,row+1,board_size,final_ans,safe_move);
                safe_move[row][col] = false;
                chessboard[row][col] = '.';
            }
        }

    }
    vector<vector<string>> solveNQueens(int n) {
        //create a chessboard
        vector<vector<char>> chessboard(n, vector<char> (n,'.'));
        vector<vector<bool>> safe_move(n, vector<bool> (n,0));
        vector<vector<string>> final_ans;
        solve(chessboard,0,n,final_ans,safe_move);
        return final_ans;

    }
};
12896