dfs和bfs——leetcode

797. 所有可能的路径 - 力扣(LeetCode)

class Solution {
public:
    vector<vector<int>> ans;
    vector<int> stk;

    void dfs(vector<vector<int>>& graph, int x, int n) {
        if (x == n) {
            ans.push_back(stk);
            return;
        }
        for (auto& y : graph[x]) {
            stk.push_back(y);
            dfs(graph, y, n);
            stk.pop_back();
        }
    }

    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        stk.push_back(0);
        dfs(graph, 0, graph.size() - 1);
        return ans;
    }
};

1723. 完成所有工作的最短时间 - 力扣(LeetCode)

class Solution {
public:

    static constexpr int N = 15;

    int kk, n;
    int w[N], s[N];
    int ans = 2e9;

    int calc() {
        for (int i = 0; i < kk; i ++) s[i] = 0;
        for (int i = 0; i < n; i ++) {
            int k = 0;
            for (int j = 1; j < kk; j ++) {
                if (s[j] < s[k]) k = j;
            }
            s[k] += w[i];
        }
        int res = 0;
        for (int i = 0; i < kk; i ++) res = max(res, s[i]);
        ans = min(ans, res);
        return res;
    }

    void simulate_anneal() {


        random_shuffle(w, w + n);
        for (double t = 1e4; t > 1e-4; t *= 0.92) {
            int a = rand() % n, b = rand() % n;
            int resa = calc();
            swap(w[a], w[b]);
            int resb = calc();
            int dt = resb - resa;
            if (exp(-dt / t) > rand() / RAND_MAX) continue;
            swap(w[a], w[b]);
        }


    }

    int minimumTimeRequired(vector<int>& jobs, int k) {
        n = jobs.size();
        kk = k;
        for (int i = 0; i < n; i ++) w[i] = jobs[i];
        for (int i = 0; i < 18; i ++) {
            simulate_anneal();
        }
        return ans;
    }
};

BFS

838. 推多米诺 - 力扣(LeetCode)
class Solution {
public:
    string pushDominoes(string dominoes) {
        //通过bfs去扩展,其隐藏的顺序是先扩展t时间的,然后扩展t + 1时间的
        int n = dominoes.size();
        queue<int> q;k
        vector<string> force(n); 
        vector<int> time(n, -1);
        for(int i = 0; i < n; i ++ )
        {
            if(dominoes[i] != '.')
            {
                q.push(i);
                time[i] = 0;
                force[i].push_back(dominoes[i]);//有一个这样的力
            }
        }
        string res(n, '.');
        while(q.size())
        {
            //为什么出栈的时候是状态确定的呢?因为出栈时t - 1时刻的bfs已经更新完了。
            int u = q.front();
            int t = time[u];
            char f = force[u][0];
            q.pop();
            if(force[u].size() == 1)//只有一个
            {
                res[u] = f;
                int nx = (f == 'R') ? u + 1 : u - 1;
                if(nx < 0 || nx >= n) continue;
                if(time[nx] == -1)//这说明这个位置是新的,需要入栈
                {
                    time[nx] = t + 1;
                    q.push(nx);
                    force[nx].push_back(f);
                }
                else if(time[nx] == t + 1)//是这个时刻的
                {
                    force[nx].push_back(f);
                }
            }
        }
        return res;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值