n-queens-ii

(i)
【题目描述】The n-queens puzzle is the problem of placing n queens on an n×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…”]
]

【解题思路】深度遍历+回溯。
1.从上到下,从左到右,判断某个位置是否可以放皇后,可以放,转2,不可以,转3;
2.放置皇后,并判断是否已经放置N个皇后,如果是,记录结果并回溯;否则转1,递归判断下一行能否放置皇后;
3.判断本行下一列是否可以放置皇后。如果本列无法放置皇后,剪枝;否则查看下一列能否放置皇后。
即,可以放置,就往下找;放不了,就往回看,拜托上层变一变,看能不能继续往下找,直到第一层都试过最后一列的位置,程序结束。
由于需要记录所有可行结果并输出,在每次得到可行结果时,将当前结果保存,并将Q还原为".",方便回溯。

【考查内容】查找,深度搜索

class Solution {
public:
    //判断位置(row, col)是否可以放置皇后
    bool judgeQ(vector<string> &queen, int row, int col) {
        int n = queen.size();
        for (int i = 0; i < row; ++i) {
            for (int j = 0; j < n; ++j) {
                if (queen[i][j] == 'Q') {
                    if (i == row || j == col || abs(row-i) == abs(col-j))
                        return false;
                }
            }
        }
        return true;
    }
    //递归放置皇后
    void placeQ(vector<string> &queen, vector<vector<string> > &res, int row, int col) {
        int n = queen.size();
        //超过棋盘大小,结束
        if (row == n || col == n)
            return;
        //(row, col)处可以放置皇后
        if (judgeQ(queen, row, col)){
            queen[row][col] = 'Q';
            //如果皇后放在最后一行,得到一个可行解,否则为中间状态
            if (row == n-1) {
                //将可行解入栈,将置为Q的位置恢复为.,回溯
                res.push_back(queen);
                queen[row][col] = '.';
                return;
            } else {
                //中间状态,继续判断下一行是否可以放置皇后,如果下一行不可以放置,将该点还原,判断改行的下一列是否可以放置皇后
                placeQ(queen, res, row+1, 0);
                queen[row][col] = '.';
                placeQ(queen, res, row, col+1);
            }
        } else {
            //当前位置无法放置皇后,如果已经是该行的最后一列,表示当前行无法放置,返回;否则,尝试当前行的下一列
            if (col == n-1)
                return;
            else
                placeQ(queen, res, row, col+1);
        }
    }
 
    vector<vector<string> > solveNQueens(int n) {
        vector<vector<string> > res;
        if (n < 0)
            return res;
        int row = 0, col = 0; 
        string str(n, '.');
        vector<string> queen(n, str);
        placeQ(queen, res, row, col);
        return res;
    }
};

(ii)
【题目描述】Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
根据n皇后问题,现在返回n皇后不同的解决方案的数量而不是具体的放置布局。

【解题思路】只需要改变得到可行解时对结果的处理。不用将现有的结果保存,只需要用一个整数记录可行解的个数即可。

【考查内容】查找,深度搜索

class Solution {
public:
    int totalNQueens(int n) {
        // write your code here
        int res = 0;
        if (n < 0)
            return res;
        int row = 0, col = 0;
        string str(n, '.');
        vector<string> queen(n, str);
        placeQ(queen, n, res, row, col);
        return res;
    }
    
    void placeQ(vector<string> &queen, int n, int &res, int row, int col) {
        if (row >= n || col >= n)
            return;
        //当前位置可以放置皇后
        if (judgeQ(queen, n, row, col)) {
            queen[row][col] = 'Q';
            //最后一行放置皇后,得到可行结果
            if (row == n-1) {
                ++res;
                queen[row][col] = '.';
                return;
            } else {
            //中间行放置皇后,递归
                placeQ(queen, n, res, row+1, 0);
                queen[row][col] = '.';
                placeQ(queen, n, res, row, col+1);
            }
        } else {
        //当前位置不可以放置皇后
            //当前行已经尝试至最后一列,回溯
            if (col == n-1)
                return;
            else
                placeQ(queen, n, res, row, col+1);
        }
    }
    
    bool judgeQ(vector<string> &queen, int n, int row, int col) {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (queen[i][j] == 'Q') {
                    if (i == row || j == col || abs(row-i) == abs(col-j))
                        return false;
                }
            }
        }
        return true;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值