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

    bool isSafe(int row, int col, vector<vector<char>> &board, int n) {
        // Check if it's safe to place a queen at board[row][col]
        int i = row;
        int j = col;

        // Check row on the left
        while (j >= 0) {
            if (board[i][j] == 'Q') {
                return false;
            }
            j--;
        }

        // Check upper left diagonal
        i = row;
        j = col;
        while (i >= 0 && j >= 0) {
            if (board[i][j] == 'Q') {
                return false;
            }
            i--;
            j--;
        }

        // Check bottom left diagonal
        i = row;
        j = col;
        while (i < n && j >= 0) {
            if (board[i][j] == 'Q') {
                return false;
            }
            i++;
            j--;
        }

        // If no conflicts found, it's safe to place the queen
        return true;
    }


    void solve(vector<vector<char>> &board, int col, int n, vector<vector<string>> &final) {
        // Base case: if all columns are filled, store the current board configuration
        if (col == n) {
            vector<string> currentSolution;
            for (const auto& row : board) {
                currentSolution.push_back(string(row.begin(), row.end()));
            }
            final.push_back(currentSolution);
            return;
        }

        // Recursive case: try placing a queen in each row of the current column
        for (int row = 0; row < n; row++) {
            if (isSafe(row, col, board, n)) {
                // Place a queen
                board[row][col] = 'Q';

                // Recur to place the rest of the queens
                solve(board, col + 1, n, final);

                // Backtrack
                board[row][col] = '.';
            }
        }
    }


    vector<vector<string>> solveNQueens(int n) {
        // Initialize the board
        vector<vector<char>> board(n, vector<char>(n, '.'));
        vector<vector<string>> final;

        // Start solving from the first column
        solve(board, 0, n, final);

        return final;
    }


    int totalNQueens(int n) {
         vector<vector<char>> board(n, vector<char>(n, '.'));
        vector<vector<string>> final;

         solve(board, 0, n, final);

        int no_rows = final.size();

        return no_rows;

    }
};
10447