poj 1010 STAMPS

思路:DFS

注意:空间和时间的限制,已经输出格式一定要认真仔细

  1 #include <iostream>
  2 #include <vector>
  3 #include <algorithm>
  4 using namespace std;
  5 
  6 #define MAXDEEP 4
  7 #define MAX_INF 9999
  8 
  9 
 10 typedef struct  
 11 {
 12     int category;
 13     int amount;
 14     int maxnum;
 15 } cons;
 16 vector<int> bestnums;
 17 
 18 vector<int> itpvec;
 19 vector<int> icusvec;
 20 //vector<cons> cvec;
 21 vector<int> icategory;              //种类
 22 int iamount;                //数量
 23 vector<int> imaxnum;                //最大面额
 24 vector<int> numtemp;
 25 cons contemp, conbest;
 26 int tag;                           //是否出现平值
 27 
 28 int isTie(const cons& c1, const cons& c2)
 29 {
 30     //return c1.category > c2.category;
 31     int flag;
 32     if(c1.category > c2.category) flag = 1;
 33     else if(c1.category < c2.category) flag = -1;
 34     else 
 35     {
 36         if(c1.amount < c2.amount) flag = 1;
 37         else if(c1.amount > c2.amount) flag = -1;
 38         else 
 39         {
 40             if(c1.maxnum > c2.maxnum) flag = 1;
 41             else if(c1.maxnum == c2.maxnum) flag = 0;
 42                  else flag = -1;
 43             
 44         }
 45     }
 46     return flag;
 47 }
 48 
 49 void DFS(int n, int sum, int step, int deep)
 50 {
 51     int i, flag;
 52     vector<int>::iterator iter;
 53     
 54     if(tag && (conbest.category == 4)) return;
 55 
 56     if(sum == n)
 57     {
 58         contemp.category = icategory.back();
 59         contemp.amount = iamount;
 60         contemp.maxnum = imaxnum.back();
 61         flag = isTie(conbest, contemp);
 62         if(!flag) 
 63         {
 64             tag = 1;
 65         }
 66         else if(flag == -1)
 67         {
 68             conbest = contemp;
 69             bestnums = numtemp;
 70             tag = 0;
 71         }
 72         icategory.pop_back();
 73         iamount--;
 74         imaxnum.pop_back();
 75         numtemp.pop_back();
 76         return;
 77     }
 78     if(deep > MAXDEEP) 
 79     {
 80         icategory.pop_back();
 81         iamount--;
 82         imaxnum.pop_back();
 83         numtemp.pop_back();
 84         return;
 85     }
 86     for(i = step; i < itpvec.size(); i++)                       ///开始选择个体
 87     {
 88         if((sum + itpvec[i]) <= n)
 89         {
 90             iamount++;
 91             numtemp.push_back(itpvec[i]);
 92             if(icategory.empty()) icategory.push_back(1);    //判断是否为空
 93             else 
 94             {
 95                 if(i == step) icategory.push_back(icategory.back());                  ///还是第一枚
 96                 else icategory.push_back(icategory.back() + 1);          //新的一枚
 97             }
 98             if(imaxnum.empty() || (itpvec[i] > imaxnum.back())) imaxnum.push_back(itpvec[i]);
 99             else imaxnum.push_back(imaxnum.back());
100 
101             DFS(n, sum + itpvec[i], i, deep + 1);
102         }
103         
104     }
105     if(i == itpvec.size())
106     {
107         if(!icategory.empty())
108         {
109             icategory.pop_back();
110             iamount--;
111             imaxnum.pop_back();
112             numtemp.pop_back();
113         }
114         return;
115     }
116     return;
117 }
118 
119 void choose(int n)
120 {
121     icategory.clear();                       //初始化
122     iamount = 0;
123     imaxnum.clear();
124     numtemp.clear();
125     conbest.category = -1;
126     conbest.amount = MAX_INF;
127     conbest.maxnum = -1;
128     bestnums.clear();
129     tag = 0;
130 
131     DFS(n, 0, 0, 1);
132 
133 }
134 
135 void printOut(int n)
136 {
137     vector<int>::iterator iter;
138     printf("%d ", n);
139     if(tag == 1) printf("(%d): tie\n", conbest.category);
140     else if(conbest.category == -1) printf("---- none\n");
141         else 
142         {
143             printf("(%d):", conbest.category);
144             for(iter = bestnums.begin(); iter != bestnums.end(); iter++)
145                 printf(" %d", *iter);
146             printf("\n");
147         }
148 
149 }
150 
151 void calculate()
152 {
153     vector<int>::iterator iter;
154     for(iter = icusvec.begin(); iter != icusvec.end(); iter++)
155     {
156         choose(*iter);
157 //        sort(cvec.begin(), cvec.end(), mark);
158         printOut(*iter);
159     }
160     itpvec.clear();
161     icusvec.clear();
162 }
163 
164 int main()
165 {
166     int tp;
167 
168     while((scanf("%d", &tp)) != EOF && tp)
169     {
170         itpvec.push_back(tp);
171         while(scanf("%d", &tp) && tp) itpvec.push_back(tp);           
172         while(scanf("%d", &tp) && tp) icusvec.push_back(tp);
173         calculate();
174     }
175 
176     return 0;
177 }

 

posted @ 2013-04-19 13:20  ggbailei  阅读(248)  评论(0)    收藏  举报