【小猫爬山】萌新求助搜索qwq~

小猫爬山

不知道搜索哪里写的有问题,似乎会死循环qwq,求助大佬!

#include<cstdio>
#include<algorithm>
#include<cstdlib>
using namespace std;
int a[20],n,W,ans=0x7fffffff;
bool b[20],dep;
void dfs(int now,int rest,int cnt) {//当前要安排第now只小猫,剩余rest,需用缆车cnt个
    if (cnt>=ans) {
        return;
    }
    if (now>n) {
        ans=cnt;
        return;
    }

    bool flag=0;
    for(int i=n,last=-1; i; i--)
        if (!b[i] && rest>=a[i] && a[i]^last) { //a[i]^last是与上一次选的重量不同
            flag=1,b[i]=1,last=a[i];
            if (a[i]==rest) dfs(now+1,W,cnt+(now<n));
            else dfs(now+1,rest-a[i],cnt);
            b[i]=0;
        }
    if (!flag && now<=n) dfs(now,W,cnt+1);//如果没有可选的重量则重新换辆缆车
}
int main() {
    //freopen("a.in","r",stdin);
    //freopen("a.out","w",stdout);
    scanf("%d%d",&n,&W);
    for (int i=1; i<=n; i++) scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    dfs(1,W,1);
    printf("%d",ans);
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-07 12:04
毕业生招你惹你了,问一个发薪日来一句别看网上乱七八糟的你看哪个工作没有固定发薪日扭头就取消了面试就问了一句公司都是这个态度吗还搞上人身攻击了...
程序员小白条:呃呃呃,都还没面试,我都不会问这么细,何况通不通过,去不去都另说,你没实力和学历的话,在外面就这样,说实话没直接已读不回就不错了,浪费时间基本上
点赞 评论 收藏
分享
05-11 20:45
门头沟学院 Java
有担当的灰太狼又在摸...:零帧起手查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务