POJ1010(dfs)

题目意思就是,给你一些邮票,要组合出K面值的,优先级:组成种类尽可能多>所需邮票数尽可能少>最大面值尽可能大。如果都相等,那么就输出tie。

这题目我一开始一直在想怎么可以用一个优秀的姿势在dfs的过程中满足上述要求第筛选…发现号困难,最后看大神的题解了。心碎。

/*************************************************************************
	> OS     : Linux 3.2.0-60-generic #91-Ubuntu
	> Author : yaolong
	> Mail   : dengyaolong@yeah.net 
	> Created Time: 2014年05月23日 星期五 07:58:23
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int a[100];  //存邮票
int v[100];  //数目
int cnt[100];//用于读取时候筛选同一币值超过5的
bool is_tie,first; //是否平局,和第一次得出结果标志
int res[6];       //结果
int best_res[6];  //最佳结果,0,5两个位置分别记录多少张和多少种
int a_i;          //邮票的数目
void set_res(int num,int type){
    best_res[0]=num;
    best_res[5]=type;
    for(int i=1;i<5;i++){
        best_res[i]=res[i];
    }
}
int get_max (int *s){
    return max(max(s[1],s[2]),max(s[3],s[4]));    
}
void dfs(int need,int num,int type,int pre){

    if(num>4){
       return ;
    }
    if(need==0){
        if(first){
            first=0;
            set_res(num,type);
        }else{
               if(type==best_res[5]){
                   if(num==best_res[0]){
                  
                       int a=get_max(res);
                       int b=get_max(best_res);
                       if(a==b){
                           is_tie=1;
                       }else if(a>b){
                           is_tie=0;
                           set_res(num,type);
                       }
                   }else{
                       if(num<best_res[0])
                       {
                           is_tie=0;
                           set_res(num,type);
                       }
                   }
               }else if(type>best_res[5]){
                   is_tie=0;
                   set_res(num,type);
               }
        }
        return ;
    }
    for(int i=pre;i<a_i;i++){
        if(need>=a[i]){
            res[num+1]=a[i];
            if(v[i]!=0){
                v[i]++;
                dfs(need-a[i],num+1,type,i);
            }else{
                v[i]++;
                dfs(need-a[i],num+1,type+1,i);
            }
            res[num+1]=0;
            v[i]--;
        }else return;
    }
    
}
int main(){
    for(;;){
        memset(cnt,0,sizeof(cnt));
        a_i=0;
        int tmp;
        for(;;){
            if(scanf("%d",&tmp)==EOF){
                return 0;
            }
            if(tmp==0){
                break;
            }
            if(cnt[tmp]<5){
                cnt[tmp]++;
                a[a_i++]=tmp;
            }
        }
        sort(a,a+a_i);
        
        while(scanf("%d",&tmp)&&tmp){
            memset(v,0,sizeof(v));
            memset(res,0,sizeof(res));
            memset(best_res,0,sizeof(best_res));
            first=true;is_tie=false;
            dfs(tmp,0,0,0);
            if(first){   //无解
                printf("%d ---- none",tmp);
            }else{
                if(is_tie){  //平
                    printf("%d (%d): tie",tmp,best_res[5]);
                }else{
                    printf("%d (%d):",tmp,best_res[5]);
                    for(int i=1;i<5;i++){
                        if(best_res[i])
                        printf(" %d",best_res[i]);
                    }   
                    
                }
            }
            puts("");
            
        }
    }

    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值