N- Queens Algorithm

The eight queens puzzle is an example of the more general N queens problem of put N non-attacking queens on an n×n chessboard, for which solutions exist for all natural numbers N with the exception of N = 2 and N = 3.The eight queens puzzle is the problem of put eight chess queens on an 8×8 chessboard so that no two queens threaten each other; thus, a solution necessitates that no two queens share the same row, column, or diagonal. Chess composer Max Bezzel published the eight queens puzzle in 1848.Nauck also extended the puzzle to the N queens problem, with N queens on a chessboard of n×n squares. In 1874, S. Gunther proposed a method use determinants to find solutions.
class Solution {
public:
    vector<vector<string> > solveNQueens(int n) {
        vector<vector<string>> result;
        if (n == 0) {
            return result;
        }
        vector<string> sol(n, string(n, '.'));
        vector<vector<bool>> visited(3, vector<bool>(2*n, false));
        dfs(result, sol, visited, 0);
        return result;
    }
    
    void dfs(vector<vector<string>>& result, vector<string>& sol,  vector<vector<bool>>& visited, int row) {
        int n = sol.size();
        if (row == n) {
            result.push_back(sol);
            return;
        }
        for (int col = 0; col < n; col++) {
            if (visited[0][col] || visited[1][row+col] || visited[2][n-1-row+col]) {
                continue;
            }
            visited[0][col] = true;
            visited[1][row+col] = true;
            visited[2][n-1-row+col] = true;
            sol[row][col] = 'Q';
            dfs(result, sol, visited, row + 1);
            sol[row][col] = '.';
            visited[0][col] = false;
            visited[1][row+col] = false;
            visited[2][n-1-row+col] = false;
        }
    }
};

LANGUAGE:

DARK MODE: