[算法] 深搜整理

深搜

之前在leetcode上刷题一直都对这个知识点比较模糊,最近,集中写了几道深搜的题目,将自己的思路和题目做了整理写下此篇博客。博客很多题目是网上提供的一些深搜题目,另外一些是leetcode上关于深搜的题目。

六角填数

如下图所示六角形中,填入1~12的数字。使得每条直线上的数字之和都相同。 图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少?
在这里插入图片描述
针对下面代码,将整个六角形的每个元素加上标号,标号的规则是从上往下,如下图:
在这里插入图片描述
最终12个点的结果:1 8 9 2 7 10 12 6 5 4 11 3

#include <iostream>
#include <vector>

using namespace std;

class Solution {
private:
    int n;
    int ff;
    vector<int> Star; // 记录每个点的值
    vector<bool> visit; // 记录当前值是否被用过
public:
    Solution(int n_, int find_index) {
        // n为点的个数,ff为我们最终需要找的点的序号(序号从1开始,到12截至,包括12)
        n = n_;
        ff = find_index;
        for (int i = 0; i <= n; i++) {
            Star.push_back(0);
            visit.push_back(false);
        }
        Star[1] = 1;
        Star[2] = 8;
        Star[12] = 3;
        visit[1] = true;
        visit[8] = true;
        visit[3] = true;
    };

    void dfs(int i);

    void check();

    void printInfo();
};

void Solution::printInfo() {
    cout << "Result: " << Star[ff] << endl;
}

void Solution::check() {
    vector<int> tmp;
    tmp.push_back(Star[1] + Star[3] + Star[6] + Star[8]);
    tmp.push_back(Star[1] + Star[4] + Star[7] + Star[11]);
    tmp.push_back(Star[8] + Star[9] + Star[10] + Star[11]);
    tmp.push_back(Star[2] + Star[3] + Star[4] + Star[5]);
    tmp.push_back(Star[2] + Star[6] + Star[9] + Star[12]);
    tmp.push_back(Star[5] + Star[7] + Star[10] + Star[12]);
    for (int i = 1; i < 6; i++) {
        if (tmp[i] != tmp[i - 1])
            return;
    }
    printInfo();
}

void Solution::dfs(int i) {
    if (i == n + 1) {
        check();
    }
    if (i == 1 || i == 2 || i == 12) {
        dfs(i + 1);
    }
    for (int j = 1; j < n + 1; ++j) {
        if (!visit[j]) {
            visit[j] = true;
            Star[i] = j;
            dfs(i + 1);
            visit[j] = false;
        }
    }
}


int main() {
    Solution s(12, 6);
    s.dfs(1);
}

复原IP地址(Leetcode)

给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Solution {
public:
    bool check(vector<string> strs, string s) {
        for (int i = 0; i < 4; i++) {
            if (stoi(strs[i]) > 255 || (stoi(strs[i]) == 0 && strs[i] != "0") ||
                (strs[i][0] == '0' && stoi(strs[i]) != 0))
                return false;
        }
        return true;
    }

    void dfs(int step, int begin, const string &s, vector<string> &strs, vector<string> &result) {
        if (step == 4) {
            if (begin == s.size() && check(strs, s)) {
                string strRes = "";
                for (int i = 0; i < 3; i++) {
                    strRes.append(strs[i]);
                    strRes.append(".");
                }
                strRes.append(strs[3]);
                result.push_back(strRes);
            }
        }
        for (int i = 1; i <= 3; i++) {
            if (begin + i <= s.size()) {
                strs.push_back(s.substr(begin, i));
                dfs(step + 1, begin + i, s, strs, result);
                strs.pop_back();
            }
        }
    }

    vector<string> restoreIpAddresses(string s) {
        vector<string> result;
        if (s.size() > 12 || s.size() < 4)
            return result;
        vector<string> strs;
        dfs(0, 0, s, strs, result);
        return result;
    }
};

int main(){
    Solution s;
    string ss = "25525511135";
    vector<string> strs = s.restoreIpAddresses(ss);
    for (int i = 0; i < strs.size(); ++i) {
        cout<< strs[i]<< endl;
    }
}

结果为:
255.255.11.135
255.255.111.35

N皇后问题

在N*N格的国际象棋上摆放N个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行、同一列或同一斜线上,问有多少种摆法

#include <iostream>
#include <vector>

using namespace std;

class Solution {
private:
    vector<bool> visited;
    vector<int> checkerboard;
    int count = 0; //存放共有多少种放法
    vector<vector<int>> record; // 存放不同方法
public:
    void solverNQueens(int);

    void dfs(int, int, vector<bool> &, vector<int> &);

    bool check(const vector<int> &);

    void printInfo();
};

void Solution::printInfo() {
    cout << "count: " << count << endl;
    for (int i = 0; i < record.size(); i++) {
        for (int j = 0; j < record[i].size(); j++) {
            cout << record[i][j] << " ";
        }
        cout << endl;
    }
}

bool Solution::check(const vector<int> &checkerboard) {
    // 检查是否有同行,同列, 对角线
    for (int i = 0; i < checkerboard.size(); ++i) {
        for (int j = i + 1; j < checkerboard.size(); ++j) {
            if (checkerboard[i] == checkerboard[j] || (abs(checkerboard[i] - checkerboard[j]) == abs(i - j)))
                return false;
        }
    }
    return true;
}

void Solution::dfs(int begin, int n, vector<bool> &visit, vector<int> &checkerboard) {
    if (begin == n) {
        if (check(checkerboard)) {
            count++;
            record.push_back(checkerboard);
        }
        return;
    }
    for (int i = 0; i < n; i++) {
        if (!visit[i]) {
            visit[i] = true;
            checkerboard[begin] = i;
            dfs(begin + 1, n, visit, checkerboard);
            visit[i] = false;
        }
    }
}

void Solution::solverNQueens(int n) {
    for (int i = 0; i < n; i++) {
        visited.push_back(false);
        checkerboard.push_back(-1);
    }
    dfs(0, n, visited, checkerboard);
}

int main() {
    Solution s;
    s.solverNQueens(9);
    s.printInfo();
}


子集(Leetcode)

给定一组不含重复元素的整数数组 nums

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值