Leecode 51. N 皇后 递归+回溯

news/2024/7/20 21:35:50 标签: c++, leetcode, dfs, 深度优先

原题链接:Leecode 51. N 皇后

同样的问题(有注释版):Acwing 3472. 八皇后 递归回溯优化在这里插入图片描述
在这里插入图片描述

class Solution {
public:
    vector<vector<string>> res;
    bool f[10];
    int p[10];
    void dfs(int n,int now)
    {
        if(now==n+1)
        {
            vector<string> t;
            for(int i=1;i<=n;i++)
            {
                string s;
                for(int j=1;j<=n;j++)
                {
                    if(j!=p[i])
                        s+=".";
                    else 
                        s+="Q";
                }
                t.push_back(s);
            }
            res.push_back(t);
            return ;
        }
        for(int x=1;x<=n;x++)
        {
            if(!f[x])
            {
                bool flag=true;
                for(int pre=1;pre<now;pre++)
                {
                    if(abs(now-pre)==abs(x-p[pre]))
                    {
                        flag=false;
                        break;
                    }
                }
                if(flag)
                {
                    f[x]=true;
                    p[now]=x;
                    dfs(n,now+1);
                    f[x]=false;
                }
            }
        }
    }
    vector<vector<string>> solveNQueens(int n) {
        dfs(n,1);
        return res;
    }
};

http://www.niftyadmin.cn/n/1265016.html

相关文章

python3安装scrapy报错:error: Microsoft Visual C++ 14.0 is required. Get it with Microsoft Visual C++

在安装爬虫框架包scrapy时&#xff0c;输入指令 &#xff1a;pip3 install scrapy 时出现下面的报错。 error: Microsoft Visual C 14.0 is required. Get it with "Microsoft Visual C Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools…

Leecode 52. N皇后 II 递归+回溯

原题链接&#xff1a;Leecode 52. N皇后 II 和这道题是一样的&#xff1a;Leecode 51. N 皇后 递归回溯 class Solution { public:bool f[10];int p[10];int res0;void dfs(int n,int now){if(nown){res;return ;}for(int i0;i<n;i){if(!f[i]){bool flagtrue;for(int j0…

Leecode 37. 解数独 dfs+回溯

原题链接&#xff1a;Leecode 37. 解数独 巧用pair<int,int>存储空格的位置&#xff1a; 解法一&#xff1a;数组存储 class Solution { public:int row[10][10];//行int col[10][10];//列int box[10][10];//宫vector<pair<int, int>> t;bool validfals…

Leecode 38. 外观数列 字符串

原题链接&#xff1a;Leecode 38. 外观数列 class Solution { public:string a[31];string fun(string s){int n1;string res"";int i1;for(;i<s.size();i){if(s[i]s[i-1])n;else {res(n0);ress[i-1];n1;}}res(n0);ress[i-1];return res;}string countAndSay(i…

(Leetcode) 有效的括号 - Python实现

题目&#xff1a; 有效的括号: 给定一个只包括 (&#xff0c;)&#xff0c;{&#xff0c;}&#xff0c;[&#xff0c;] 的字符串&#xff0c;判断字符串是否有效。 有效字符串需满足&#xff1a; 左括号必须用相同类型的右括号闭合。 左括号必须以正确的顺序闭合。 …

Leecode 39. 组合总和 dfs+回溯+剪枝

原题链接&#xff1a;Leecode 39. 组合总和 dfs回溯剪枝 class Solution { public:vector<int> tmp;vector<vector<int>> res;void dfs(vector<int>& candidates, int target,int now){if(target0){res.push_back(tmp);return ;}if(target<c…

链表Linked List

为什么需要链表 顺序表的构建需要预先知道数据大小来申请连续的存储空间&#xff0c;而在进行扩充时又需要进行数据的搬迁&#xff0c;所以使用起来并不是很灵活。 链表结构可以充分利用计算机内存空间&#xff0c;实现灵活的内存动态管理。 链表的定义 链表&#xff08;Li…

leecode 40. 组合总和 II dfs+回溯+剪枝+去重

原题链接&#xff1a;leecode 40. 组合总和 II 去重这一句很巧妙&#xff1a;if(i>now && candidates[i]candidates[i-1])&#xff0c;这句规定了在同一层次上&#xff0c;相同的数只能选一次。 class Solution { public:vector<vector<int>> res;ve…