Leetcode N-Queens系列

#51 N-Queens


The n-queens puzzle is the problem of placing n queens on ann×n chessboard such that no two queens attack each other.

Given an integer n, return all distinct solutions to the n-queens puzzle.

Each solution contains a distinct board configuration of the n-queens' placement, where'Q' and '.' both indicate a queen and an empty space respectively.

For example,
There exist two distinct solutions to the 4-queens puzzle:

[
 [".Q..",  // Solution 1
  "...Q",
  "Q...",
  "..Q."],

 ["..Q.",  // Solution 2
  "Q...",
  "...Q",
  ".Q.."]
]

       经典的回溯问题,为了减少对每个方格是否可放置皇后的判断,提前用一个数组记录下来。

        代码中 isvalid 不同下标范围具有不同的含义:

                  0  ~ n- 1                         index = col  代表 第 col  列的状态(共n列)

                  n  ~ 3n-2                        index = n + row + col 代表(row,col)所在的正斜线的状态(共2n -1个正斜线)

                  3n -1 ~  5n -3                index = 3n - 2 + n -row + col 代表(row, col)所在反斜线的状态(共2n -1个反斜线)

class Solution {
public:
    vector< vector<string> > solveNQueens(int n) {

        vector< vector<string> > res;
        if (n == 0) return res;

        int row = 0;
        vector<int> isvalid(n + 2 * (2 * n - 1), 1);
        vector<int> col(n, -1);

        while(row >= 0){
            if(col[row] != -1) {
                isvalid[col[row]] = isvalid[n + row + col[row]] = isvalid[3 * n - 2 + n  - row + col[row]] = 1;
            }
            col[row] ++;
            while(col[row] < n){
                if(isvalid[col[row]] && isvalid[n + row + col[row]] && isvalid[3 * n - 2 + n - row + col[row]] ) break;
                col[row] ++;
            }

            if(col[row] == n)  row--; //回溯
            else if(row == n -1){
                vector<string> one;
                for(int i = 0; i < n; i ++){
                    string s = "";
                    s.insert(0, n - 1, '.');
                    s.insert(col[i], 1, 'Q');
                    one.push_back(s);
                }
                res.push_back(one);
            }
            else{
                isvalid[col[row]] = isvalid[n + row + col[row]] = isvalid[3 * n - 2 + n - row + col[row]] = 0;
                col[++row] = -1;
           }
        }
        return res;
    }
};


#52 N-Queens II


Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

由上面代码稍加修改即可:

class Solution {
public:
    int totalNQueens(int n) {
        if (n == 0) return 0;

        int res = 0, row = 0;
        vector<int> isvalid(n + 2 * (2 * n - 1), 1);
        vector<int> col(n, -1);

        while(row >= 0){
            if(col[row] != -1) {
                isvalid[col[row]] = isvalid[n + row + col[row]] = isvalid[3 * n - 2 + n  - row + col[row]] = 1;
            }
            col[row] ++;
            while(col[row] < n){
                if(isvalid[col[row]] && isvalid[n + row + col[row]] && isvalid[3 * n - 2 + n - row + col[row]] ) break;
                col[row] ++;
            }

            if(col[row] == n)  row--; //回溯
            else if(row == n -1) res ++;
            else{
                isvalid[col[row]] = isvalid[n + row + col[row]] = isvalid[3 * n - 2 + n - row + col[row]] = 0;
                col[++row] = -1;
           }
        }
        return res;
    }
};

可以写成递归形式如下:

class Solution {
public:
    void dfs(int &res, vector<int> &isvalid, int row, int n) {
        if (row == n){
            ++res; return;
        }

        for (int col = 0; col < n; col++) {
            if (isvalid[col] && isvalid[n + row + col] && isvalid[3 * n - 2 + n - row + col]){
                isvalid[col] = isvalid[n + row + col] = isvalid[3 * n - 2 + n - row + col] = 0;
                dfs(res, isvalid, row + 1, n);
                isvalid[col] = isvalid[n + row + col] = isvalid[3 * n - 2 + n  - row + col] = 1;
            }
        }
        return;
    }
    int totalNQueens(int n) {
        if (n == 0) return 0;
        int res = 0;
        vector<int> isvalid(n + 2 * (2 * n - 1), 1);
        dfs(res, isvalid, 0, n);
        return res;
    }
};



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值