力扣 51. N 皇后

题目来源:https://leetcode.cn/problems/n-queens/description/

C++题解1:回溯法。用一个vector<vector<int>> qmap(n, vector<int>(n, 0))标记后面行不能使用的位置,每次递归下一层为下一行的位置,直到达到n行,保存结果。

class Solution {
public:
    vector<vector<string>> res;
    vector<string> lujing;
    void backtracking(int n, vector<vector<int>> qmap, int i) {
        // i为行,j为列
        if(i == n) {
            res.push_back(lujing);
            return;
        }

        for(int j = 0; j < n; j++) {
            if(qmap[i][j] == 0) {

                string a = "";  // 生成行string
                for(int b = 0; b < n; b++) {
                    if(b == j) a = a + 'Q';
                    else a = a + '.';
                }
                lujing.push_back(a);

                vector<vector<int>> temqmap = qmap; // 保存当前qmap,用于回溯
                // 更新qmap斜下方
                int pp = j - 1;
                int qq = j + 1;
                for(int mm = i+1; mm < n; mm++){
                    if(pp >= 0) {qmap[mm][pp] = 1; pp--;}
                    if(qq < n) {qmap[mm][qq] = 1; qq++;}
                }
                // 更新qmap正下方
                for(int mm = i; mm < n; mm++) qmap[mm][j] = 1;

                backtracking(n, qmap, i+1); //更新下一行
                qmap = temqmap;
                lujing.pop_back();
            }
        }
        return;
    }
    vector<vector<string>> solveNQueens(int n) {
        vector<vector<int>> qmap(n, vector<int>(n, 0));
        backtracking(n, qmap, 0);
        return res;
    }
};

太开心了!这是我自己第一次独立做出的“困难”题目! 

C++题解2(来源代码随想录):思路同上,在一些细节上处理不太一样。

class Solution {
private:
vector<vector<string>> result;
// n 为输入的棋盘大小
// row 是当前递归到棋盘的第几行了
void backtracking(int n, int row, vector<string>& chessboard) {
    if (row == n) {
        result.push_back(chessboard);
        return;
    }
    for (int col = 0; col < n; col++) {
        if (isValid(row, col, chessboard, n)) { // 验证合法就可以放
            chessboard[row][col] = 'Q'; // 放置皇后
            backtracking(n, row + 1, chessboard);
            chessboard[row][col] = '.'; // 回溯,撤销皇后
        }
    }
}
bool isValid(int row, int col, vector<string>& chessboard, int n) {
    // 检查列
    for (int i = 0; i < row; i++) { // 这是一个剪枝
        if (chessboard[i][col] == 'Q') {
            return false;
        }
    }
    // 检查 45度角是否有皇后
    for (int i = row - 1, j = col - 1; i >=0 && j >= 0; i--, j--) {
        if (chessboard[i][j] == 'Q') {
            return false;
        }
    }
    // 检查 135度角是否有皇后
    for(int i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) {
        if (chessboard[i][j] == 'Q') {
            return false;
        }
    }
    return true;
}
public:
    vector<vector<string>> solveNQueens(int n) {
        result.clear();
        std::vector<std::string> chessboard(n, std::string(n, '.'));
        backtracking(n, 0, chessboard);
        return result;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值