萌新不会背包dp,用bfs算作弊嘛?
  • 板块B3635 硬币问题
  • 楼主b1tset
  • 当前回复8
  • 已保存回复8
  • 发布时间2023/2/18 13:09
  • 上次更新2023/10/24 00:29:19
查看原帖
萌新不会背包dp,用bfs算作弊嘛?
697898
b1tset楼主2023/2/18 13:09

bfs代码ac,不会背包dp QnQ

#include <bits/stdc++.h>
using namespace std;

bool vis[1000010];
int cnt[1000010];
queue<int> q;

int main()
{
    int n;
    cin >> n;
    vis[0] = 0;
    cnt[0] = 0;
    q.push(0);
    while (q.size())
    {
        int x = q.front();
        q.pop();
        if (x == n)
        {
            cout << cnt[n] << endl;
            return 0;
        }
        
        int kx = x + 1;
        if (kx <= n && !vis[kx])
        {
            vis[kx] = 1;
            q.push(kx);
            cnt[kx] = cnt[x] + 1;
        }
        kx = x + 5;
        if (kx <= n && !vis[kx])
        {
            vis[kx] = 1;
            q.push(kx);
            cnt[kx] = cnt[x] + 1;
        }
        kx = x + 11;
        if (kx <= n && !vis[kx])
        {
            vis[kx] = 1;
            q.push(kx);
            cnt[kx] = cnt[x] + 1;
        }
    }
    return 0;
}
2023/2/18 13:09
加载中...