[LeetCode 51]N皇后

这篇博客探讨了如何用C++解决LeetCode的51题——N皇后问题,即在n×n棋盘上放置n个皇后,使得它们互不攻击。文章通过递归方法阐述了解决思路,并给出了4皇后问题的具体示例。此外,还提及了N皇后问题的解法数量计数,即LeetCode的52题。

[LeetCode 51]C++实现N皇后

n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。在这里插入图片描述

给定一个整数 n,返回所有不同的 n 皇后问题的解决方案。

每一种解法包含一个明确的 n 皇后问题的棋子放置方案,该方案中 ‘Q’ 和 ‘.’ 分别代表了皇后和空位。

示例:

输入:4
输出:[
[".Q…", // 解法 1
“…Q”,
“Q…”,
“…Q.”],

["…Q.", // 解法 2
“Q…”,
“…Q”,
“.Q…”]
]
解释: 4 皇后问题存在两个不同的解法。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/n-queens

步骤1:

设立方向数组:(x↓ y→)
(x,y)表示皇后所在位置,
方向数组按照8个方向延伸到边界,表示攻击范围
(x-1,y-1) (x-1,y) (x-1,y+1)
 (x,y-1)   (x,y)   (x,y+1)
(x+1,y-1) (x+1,y) (x+1,y+1)  

例:mark(3,3)
1 0 0 1 0 0 1 0
0 1 0 1 0 1 0 0
0 0 1 1 1 0 0 0
1 1 1 1 1 1 1 1 
0 0 1 1 1 0 0 0
0 1 0 1 0 1 0 0
1 0 0 1 0 0 1 0
0 0 0 1 0 0 0 1

步骤2:
以4皇后为例
每一行必须放一个皇后,我们只是选择放在哪一列
依次递归直到皇后的个数为N
在这里插入图片描述

实现代码

class Solution {
public:
    vector<vector<string>> solveNQueens(int n) {
        vector<vector<string>> result;//最终结果
        vector<string> location;//存储摆放结果
        vector<vector<int>> mark;//标记是否可以放皇后

        for(int i=0;i<n;++i){
            mark.push_back(vector<int>());//摆放 NxN棋盘
            //mark初始值都为0
            for(int j=0;j<n;++j){
                mark[i].push_back(0);
            }
            //location初始化
            location.push_back("");
            location[i].append(n,'.');//result[i]初始化都为.
        }
        generate(0,n,location,result,mark);
        return result;        
    }
    void generate(int k,int n,vector<string>& location,vector<vector<string>>& result,vector<vector<int>>& mark){
        //k代表行数,n代表皇后个数
        if(k==n){
            result.push_back(location);
            return;
        }
        for(int i=0;i<n;++i){
            if(mark[k][i]==0){
                vector<vector<int>> temp_mark=mark;
                location[k][i]='Q';
                put_down_queen(k,i,mark);//放置皇后
                generate(k+1,n,location,result,mark);
                mark=temp_mark;
                location[k][i]='.';
            }
        }

    }
    void put_down_queen(int x,int y,vector<vector<int>>& mark){
        //方向数组
        static const int dx[]={-1,1,0,0,-1,-1,1,1};
        static const int dy[]={0,0,-1,1,-1,1,-1,1};
        mark[x][y]=1;//标记皇后
        //延伸的长度为N-1
        for(int i=1;i<mark.size();++i){
            for(int j=0;j<8;++j){
                //延伸方向数组
                int new_x=x+i*dx[j];
                int new_y=y+i*dy[j];
                if(new_x>=0&&new_y>=0&&new_x<mark.size()&&new_y<mark.size()){
                    mark[new_x][new_y]=1;
                }
            }
        }
    }

};

[LeetCode 52]N皇后II
方法一致,count计数

class Solution {
public:
    int totalNQueens(int n) {
        vector<vector<int>>mark;
        for(int i=0;i<n;++i){
            mark.push_back(vector<int>());
            for(int j=0;j<n;++j){
                mark[i].push_back(0);
            }
        }
        int count=0;
        recursive(count,0,n,mark);
        return count;
    }
    void recursive(int& count,int k,int n,vector<vector<int>>& mark){
        if(k==n){
            ++count;
            return;
        }
        for(int i=0;i<n;++i){
            if(mark[k][i]==0){
                vector<vector<int>>temp_mark=mark;
                put_down_queen(k,i,mark);
                recursive(count,k+1,n,mark);
                mark=temp_mark;
            }
        }
    }
    void put_down_queen(int x,int y,vector<vector<int>>& mark){
        static const int dx[]={-1,1,0,0,-1,-1,1,1};
        static const int dy[]={0,0,-1,1,-1,1,-1,1};
        mark[x][y]=1;
        for(int i=1;i<mark.size();++i){
            for(int j=0;j<8;++j){
                int new_x=x+i*dx[j];
                int new_y=y+i*dy[j];
                if(new_x>=0&&new_x<mark.size()&&new_y>=0&&new_y<mark.size()){
                    mark[new_x][new_y]=1;
                }
            }
        }
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值