n-皇后 ii

题目描述

继续思考“n-queens”问题 这次我们不是输出皇后的排列情况,而是输出n皇后问题一共有多少种解法

正解

class Solution {
public:
    /**
     * 
     * @param n int整型 
     * @return int整型
     */
    int totalNQueens(int n) {
        // write code here
        int res = 0;
        // ???
        vector<string> chessboard(n,string(n, '0'));
        dfs(chessboard, n, 0, res);
        return res;
    }
    
    void dfs(vector<string> &chessboard, int size, int row, int &result){
        if(row == size){
            result++;
            return ;
        }
        for(int col = 0; col < size; col++){
            if(isvalid(chessboard, size, row, col)){
                chessboard[row][col] = '1';
                dfs(chessboard, size, row+1, result);
                chessboard[row][col] = '0';
            }
        }
        
    }
    bool isvalid(vector<string> &chessboard, int size,int row, int col){
        // 检查列和对角线
        for(int i = row - 1; i >= 0; i--){
            if(chessboard[i][col] == '1')
                return false;
        }
        for(int i = row - 1, j = col - 1; i >= 0 && j >= 0 ; i--, j--){
            if(chessboard[i][j] == '1')
                return false;
        }
        for(int i = row - 1, j = col + 1; i >= 0 && col < size; i--, j++){
            if(chessboard[i][j] == '1')
                return false;
        }
        return true;
    }
    
};

错解

class Solution {
public:
    /**
     * 
     * @param n int整型 
     * @return int整型
     */
    int totalNQueens(int n) {
        // write code here
        int res = 0;
        vector<vector<int>> chessboard(n,vector<int>(n, 0));
        dfs(chessboard, n, 0, res);
        return res;
    }
    
    void dfs(vector<vector<int>> &chessboard, int size, int row, int &result){
        if(row == size){
            result++;
            return ;
        }
        for(int col = 0; col < size; col++){
            if(isvalid(chessboard, size, row, col)){
                chessboard[row][col] = 1;
                dfs(chessboard, size, row+1, result);
                chessboard[row][col] = 0;
            }
        }
        
    }
    bool isvalid(vector<vector<int>> &chessboard, int size,int row, int col){
        // 检查列和对角线
        for(int i = row - 1; i >= 0; i--){
            if(chessboard[i][col] == 1)
                return false;
        }
        for(int i = row - 1, j = col - 1; i >= 0 && j >= 0 ; i--, j--){
            if(chessboard[i][j] == 1)
                return false;
        }
        for(int i = row - 1, j = col + 1; i >= 0 && col < size; i--, j++){
            if(chessboard[i][j] == 1)
                return false;
        }
        return true;
    }
    
};

错因分析

  • 知道是初始化的时候出的的问题,不太清楚为什么用string、char就没问题,用int就出了问题。
  • 对于vector的用法不是很清楚。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值