LeetCode刷题笔记(N-Queens)

最近北京甚是凉爽,动不动就洒下雨点,刷新了我对北京不怎么下雨的观点。刚刷了一道被标为“hard”的题目,但实际上并不是很难,下面就和大家分享一下经验吧!

题目如下:

题意分析:

本题为著名的N皇后问题:要求将n个皇后摆放在n*n的棋盘格中,使得横、纵以及两个角度各自的2*n-1条对角线方向均不会同时出现两个皇后。请找到n皇后问题的所有解,解中要求皇后的位置用'Q'表示,非皇后的位置用 '.'表示。

方法一(递归回溯法①)

采取“逐行试凑法”,先用index记录试放皇后当前行,row记录n皇后问题的一个解,用布尔型数组col、diam1、diam2分别标记是否在纵向和两个角度对角线方向放过皇后,其中两个角度的对角线个数均为2*n-1,且右上到左下的对角线满足横纵坐标相加之和为定值,左上到右下的对角线满足横坐标-纵坐标为定值。然后创建findsolveNQueens函数进行递归调用,其中递归终止条件为“if ( index == n ) res.push_back( getResult( n, row) );”,当访问位置的纵向与两个角度对角线方向均未放过皇后,那么可以在该位置放下皇后且继续递归调用。由于这里需要回溯,所以需要清除之前的标记。最后,通过getResult函数将n皇后问题的每个解row转换成题目要求的解形式,并返回n皇后问题的所有解即可。

解题代码如下:

class Solution{
private:
    void findsolveNQueens(int n, int index, vector<int>& row, vector<bool>& col, vector<bool>& diam1, vector<bool>& diam2, vector<vector<string>>& res){
        if ( index == n ) res.push_back( getResult( n, row) );

        for (int i = 0; i < n; i++) {
            //由于数组diam2是从0开始访问的,所以整体需要+n-1,即index-i+n-1
            if( !col[i] && !diam1[index+i] && !diam2[index-i+n-1] )
                {
                    row.push_back(i);
                    col[i] = true;
                    diam1[index+i] = true;
                    diam2[index-i+n-1] = true;
                    findsolveNQueens(n, index+1, row, col, diam1, diam2, res);
                    col[i] = false;
                    diam1[index+i] = false;
                    diam2[index-i+n-1] = false;
                    row.pop_back();
             }
        }
    }

    vector<string> getResult( int n, vector<int>& row ){
        vector<string> result ( n, string(n,'.') );
        for ( int i = 0; i < n; i++ ) {
            result[i][row[i]] = 'Q';
        }
      return result;
    }


public:
    vector<vector<string>> solveNQueens( int n ){
        vector<vector<string>> res;
        vector<int> row;
        vector<bool> col( n, false );
        vector<bool> diam1( 2*n-1, false );
        vector<bool> diam2( 2*n-1, false );
        findsolveNQueens( n, 0, row, col, diam1, diam2 , res);

        return res;
    }
};

提交后的结果如下:

 

方法二(递归回溯法②)

与方法一不同在于:1、采用一个函数isValid去判断某个位置是否可以放皇后,而不再用布尔型数组去标记了;2、直接用获得问题的解,不用通过getResult函数将n皇后问题的每个解row转换成题目要求的解形式。

解题代码如下:

class Solution {
public:
    vector<vector<string>> solveNQueens(int n) {
        vector<vector<string>> res;
        vector<string> queens(n, string(n, '.'));
        findsolveNQueens(0, queens, res);
        return res;
    }
    void findsolveNQueens(int index, vector<string>& queens, vector<vector<string>>& res) {
        int n = queens.size();
        if (index == n) {
            res.push_back(queens);
            return;
        }
        for (int i = 0; i < n; ++i) {
            if (isValid(queens, index, i)) {
                queens[index][i] = 'Q';
                findsolveNQueens(index + 1, queens, res);
                queens[index][i] = '.';
            }
        }
    }
    bool isValid(vector<string>& queens, int row, int col) {
        for (int i = 0; i < row; ++i) {
            if (queens[i][col] == 'Q') return false;
        }
        for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; --i, --j) {
            if (queens[i][j] == 'Q') return false;
        }
        for (int i = row - 1, j = col + 1; i >= 0 && j < queens.size(); --i, ++j) {
            if (queens[i][j] == 'Q') return false;
        }
        return true;
    }
};

提交后的结果如下:

 

 

 

日积月累,与君共进,增增小结,未完待续。 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值