Cover image for LeetCode Hot 100 P51 N-Queens

LeetCode Hot 100 P51 N-Queens

Words 350
Views
Visitors

Timeline

Timeline

2026-03-17

init

Backtracking

Problem:

Backtracking: note that when checking diagonals, there are two diagonals, with directions "" and “/”;

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
#include <vector>#include <string>#include <unordered_set>using std::vector;using std::string;using std::unordered_set;class Solution {    private:        unordered_set<int> uset;        bool queen_can_stand(vector<string> &board, int n, int posi, int posj)        {                int i, j;                if (uset.count(posj))                        return false;                // Diagonal \                i = posi;                j = posj;                while (i >= 0 && j >= 0) {                        if (board[i][j] == 'Q')                                return false;                        i--;                        j--;                }                // Diagonal \                i = posi;                j = posj;                while (i < n && j < n) {                        if (board[i][j] == 'Q')                                return false;                        i++;                        j++;                }                // Diagonal /                i = posi;                j = posj;                while (i >= 0 && j < n) {                        if (board[i][j] == 'Q')                                return false;                        i--;                        j++;                }                // Diagonal /                i = posi;                j = posj;                while (i < n && j >= 0) {                        if (board[i][j] == 'Q')                                return false;                        i++;                        j--;                }                return true;        }        void __solveNQueens(vector<vector<string> > &res, vector<string> &board, int n, int row)        {                int j;                if (row == n) { // The last row is already filled                        res.push_back(board);                        return;                }                for (j = 0; j < n; j++) {                        if (queen_can_stand(board, n, row, j)) {                                uset.insert(j); // Mark that column j already has a queen                                board[row][j] = 'Q';                                __solveNQueens(res, board, n, row + 1);                                board[row][j] = '.';                                uset.erase(j);                        }                }        }    public:        vector<vector<string> > solveNQueens(int n)        {                int i;                string line;                vector<string> board;                vector<vector<string> > res;                for (i = 0; i < n; i++)                        line.push_back('.');                for (i = 0; i < n; i++)                        board.push_back(line);                __solveNQueens(res, board, n, 0);                return res;        }};
Loading comments…