Cover image for leetcode热题100 P51 N皇后

leetcode热题100 P51 N皇后

字数 378
阅读
访客

时间轴

时间轴

2026-03-17

init

回溯

题目:

回溯:注意判断对角线时是两条对角线,方向分别为""和"/";

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;                // 斜角 \                i = posi;                j = posj;                while (i >= 0 && j >= 0) {                        if (board[i][j] == 'Q')                                return false;                        i--;                        j--;                }                // 斜角 \                i = posi;                j = posj;                while (i < n && j < n) {                        if (board[i][j] == 'Q')                                return false;                        i++;                        j++;                }                // 斜角 /                i = posi;                j = posj;                while (i >= 0 && j < n) {                        if (board[i][j] == 'Q')                                return false;                        i--;                        j++;                }                // 斜角 /                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) { // 最后一行已经填满                        res.push_back(board);                        return;                }                for (j = 0; j < n; j++) {                        if (queen_can_stand(board, n, row, j)) {                                uset.insert(j); // 标识第j列已经有皇后                                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;        }};
评论加载中…