leetcode N-Queens

本文介绍了使用递归方法解决N皇后问题的算法实现,包括关键步骤和代码示例。
任然是NP问题,解决此类问题主要思想就是递归,


就是用一个循环去枚举当前所有情况,然后把元素加入,递归,再把元素移除

代码1 提示 Output Limit Exceeded  输出结果中有重复的

class Solution {
public:
    void solveNQueensHelper(vector<vector<string> > &result, vector<string> &tempResult, vector<bool> &flagRow, vector<bool> &flagCol, int &num, int n)
    {
        if(num==n)
        {
            result.push_back(tempResult);
            return ;
        }
        
        for(int i = 0; i < n; ++i)
        {
            if(flagRow[i]==false)
                continue;
            for(int j = 0; j < n; ++j)
            {
                if(flagCol[j]==false)
                    continue;

				tempResult[i][j] = 'Q';
				flagRow[i] = false;
				flagCol[j] = false;
				num++;
				solveNQueensHelper(result, tempResult, flagRow, flagCol, num, n);
				num--;
				flagRow[i] = true;
				flagCol[j] = true;
				tempResult[i][j] = '.';
				
            }
        }
        
        
        return ;
    }


 vector<vector<string> > solveNQueens(int n) {
        vector<vector<string> > result;
        
        if(n==0)
            return result;
        
        vector<bool> flagRow(n, true);
        vector<bool> flagCol(n, true);
        vector<string> tempResult;
        for(int i = 0; i < n; ++i)
        {
            string str = "";
            for(int j = 0; j < n; ++j)
                str += '.';
                
            tempResult.push_back(str);
        }
        
        int num = 0;
        solveNQueensHelper(result, tempResult, flagRow, flagCol, num, n);
    
        
        
        return result;
    }
};


Accepted 代码 参考 http://www.cnblogs.com/TenosDoIt/p/3801621.html

class Solution {
public:

 vector<vector<string> > solveNQueens(int n) {
        vector<vector<string> > result;
        
        if(n==0)
            return result;
        
        vector<int> state(n,-1);
    
        solveNQueensHelper(result, state, 0);
        
        return result;
    }
    
    
void solveNQueensHelper(vector<vector<string> > &result, vector<int> &state, int row)
    {
        int n = state.size();
        if(row==n)
        {
            vector<string> temp(n, string(n, '.'));
            for(int i = 0; i < n; ++i)
            {
                temp[i][state[i]] = 'Q';
            }
            result.push_back(temp);
            return ;
        }
        
        for(int col = 0; col < n; ++col)
        {
            if(isValid(state, row, col))
            {
                state[row] = col;
                solveNQueensHelper(result, state, row+1);
                state[row] = -1;
                
            }
        }
        
        return ;
    }
    
    bool isValid(vector<int> &state, int row, int col)
    {
        for(int i = 0; i < row; ++i)
            if(state[i]==col || abs(row - i) == abs(col - state[i]))
                return false;
        
        return true;
        
    }
};




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值