Poj 1010

算是自己独立完成的一道深搜吧 虽然实现还是感觉有点简略 中间也经历了各种修修补补 数据结构也是改了又改 但总归是不辜负一番努力吧

  1 #include<iostream>
  2 using namespace std;
  3 
  4 int stamp[100];
  5 int customer[100];
  6 int ncus;
  7 int m; 
  8 int number;//总种类数量
  9 int cctype=0;//当前种类数量
 10 int type=0;
 11 int cvalue=0;//当前价值 
 12 int value=0;
 13 int countt=0;
 14 int csche[5]={-1};//当前方案
 15 int sche[5];//方案
 16 bool tie=false;//是否有多种最佳方案
 17 bool none=true;//是否无符合方案
 18 
 19 void cmp(int &a,int &b){
 20     int temp;
 21     if(a>b) {
 22         temp=a;
 23         a=b;
 24         b=temp;
 25     }
 26 } 
 27 
 28 void dps(int k,int cad){//k当前层数 cad当前位置 
 29     cvalue+=stamp[cad];
 30     csche[k]=cad;
 31     if(csche[k]!=csche[k-1]){
 32         cctype++;
 33     }
 34     if(cvalue==m){
 35         if(cctype==type&&k==countt){
 36             int j;
 37             for(j=0;j<=k;j++){
 38                 if(stamp[csche[k]]!=stamp[sche[k]]){
 39                     break;
 40                 }
 41             }
 42             if(j==k+1){
 43                 tie=true;
 44             }
 45         }
 46         if(cctype>type){
 47             tie=false;
 48             type=cctype;
 49             countt=k;
 50             for(int j=1;j<=k;j++){
 51                 sche[j]=csche[j];
 52             }
 53         }
 54         if(cctype==type&&k<countt){
 55             tie=false;
 56             countt=k;
 57             for(int j=1;j<=k;j++){
 58                 sche[j]=csche[j];
 59             }
 60         }
 61         none=false; 
 62         if(csche[k]!=csche[k-1]){
 63             cctype--;
 64         }
 65         cvalue-=stamp[cad];
 66         return; 
 67     } 
 68     if(k==4){
 69         if(csche[k]!=csche[k-1]){
 70             cctype--;
 71         }
 72         cvalue-=stamp[cad];
 73         return;
 74     }
 75     for(int i=cad;i<number;i++){
 76         if((4-k)*stamp[number-1]+cvalue>=m&&number-cad-1+cctype>=type){
 77             dps(k+1,i);
 78         }
 79     }
 80     if(csche[k]!=csche[k-1]){
 81             cctype--;
 82     }
 83     cvalue-=stamp[cad];
 84     return;
 85 }
 86 
 87 int main(){
 88     while(cin>>stamp[0]){
 89         number=1;
 90         while(cin>>stamp[number]){
 91             if(stamp[number]==0){
 92                 break;
 93             }
 94             number++;
 95         }
 96         ncus=0;
 97         while(cin>>customer[ncus]){
 98             if(customer[ncus]==0){
 99                 break;
100             }
101             ncus++;
102         }
103         for(int i=0;i<number-1;i++){
104             for(int j=i+1;j<number;j++){
105                 cmp(stamp[i],stamp[j]);
106             }
107         }
111         for(int p=0;p<ncus;p++){
112             m=customer[p];
113             cctype=0;//当前种类数量
114             type=0;
115             cvalue=0;//当前价值 
116             value=0;
117             countt=0;
118             tie=false;//是否有多种最佳方案
119             none=true;
120             for(int i=0;i<number;i++){
121                 dps(1,i);
122             }
123             if(none){
124                 cout<<m<<" ---- none"<<endl;
125             }
126             else if(tie){
127                 cout<<m<<" ("<<type<<"): tie"<<endl;
128             }
129             else{
130                 cout<<m<<" ("<<type<<"):";
131                 for(int q=1;q<=countt;q++){
132                     cout<<' '<<stamp[sche[q]];
133                 }
134                 cout<<endl;
135             }
136         }
137     }
138 } 

 

posted @ 2017-02-25 13:50  水野玛琳  阅读(196)  评论(0编辑  收藏  举报