leetcode n皇后问题

63 篇文章 0 订阅
50 篇文章 0 订阅

题目1:
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:
//回朔法,逐行扫描,flag不用关注行,只关注列和对角线,flag包含所有点

代码1:

class Solution {
public:
    vector<vector<string> > solveNQueens(int n) {
        m_size = n;
        m_vectmp = vector<string>(n, string(n,'.'));
        vector<vector<bool> > flag(n, vector<bool>(n, true));
        solve(0, flag);
        return m_res;
        
    }
    
    //逐行扫描,每行必定有一个
    void solve(int sequence, vector<vector<bool> > &flag)
    {
        if (sequence == m_size)
        {
            m_res.push_back(m_vectmp);
            return;
        }

        for (int j=0; j<m_size; j++)
        {
            if (flag[sequence][j])
            {
                m_vectmp[sequence][j] = 'Q';
                //这里还原flag有困难,所以传入临时的复制
                vector<vector<bool> > flagtmp = flag;
                set1(sequence, j, flagtmp);
                solve( sequence+1, flagtmp);    
                m_vectmp[sequence][j] = '.'; 
            }
        }
    }
    //逐行扫描,不用关注行,只关注列和对角线
    void set1(int i, int j, vector<vector<bool> > &flag)
    {
        for(int m=0; m<m_size; ++m)
        {
            int tmp1 = i + j;
            int tmp2 = i - j;
            for(int k=0; k<m_size; ++k)
            {
                //行下标+/-列下标相同的为同一对角线
                if(j == k || m + k ==  tmp1|| m - k == tmp2)
                {
                    flag[m][k] = false;
                }
            }
        }
    }
    
    int m_size;
    vector<vector<string> > m_res;
    vector<string> m_vectmp;
};

思路2:
/*
由于行下标+/-列下标相同的为同一对角线, 因此flag只需要3行,第一行为哪些列
已经被标记,第二三行分别为哪些斜对角线被标记,这样子可以还原,递归时不需要传入
flag的复制
*/

代码2:

class Solution {
public:
    vector<vector<string> > solveNQueens(int n) {
        m_vectmp = vector<string>(n, string(n, '.'));
        m_flag = vector<vector<bool> >(3, vector<bool>(2*n, true));
        m_size = n;
        
        solve(0);
        return m_res;
        
    }
    
    //逐行扫描,每行必定有一个
    void solve(int sequence)
    {
        if (sequence == m_size)
        {
            m_res.push_back(m_vectmp);
            return;
        }

        for (int j=0; j<m_size; ++j)
        {
            if (flag[0][j] && flag[1][sequence + j] && flag[2][sequence - j + m_size])
            {
                m_vectmp[sequence][j] = 'Q';
                flag[0][j] = flag[1][sequence + j] = flag[2][sequence - j + m_size] = false;
                solve(m_vectmp, sequence+1, flag, n);
                flag[0][j] = flag[1][sequence + j] = flag[2][sequence - j + m_size] = true;           
                m_vectmp[sequence][j] = '.'; 
            }
        }
    }
    
    int m_size;
    vector<vector<bool> > m_flag;
    vector<string>m_vectmp;
    vector<vector<string> > m_res;
};

题目2:
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) 
    {
        m_size = n;
        m_res = 0;
        m_flag = vector<vector<bool> >(3, vector<bool>(2*n, true));
        DFS(0);
        return m_res;
        
    }
    
    void DFS(int sequence, )//也可以在每个递归内生成一个flag
    {
        if (sequence == m_size)
        {
            ++m_res;
            return;
        }
        for (int j=0; j<m_size; ++j)
        {
            if (m_flag[0][j] && m_flag[1][sequence + j] && m_flag[2][sequence - j + m_size])
            {
                m_flag[0][j] = m_flag[1][sequence + j] = m_flag[2][sequence - j + m_size] = false;
                DFS(sequence+1);
                m_flag[0][j] = m_flag[1][sequence + j] = m_flag[2][sequence - j + m_size] = true;
            }
        }
    }
    
    int m_size;
    int m_res;
    vector<vector<bool> > m_flag;
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值