USACO 1.4 Mother's Milk

1 /*
2 ID: jiafeim1
3 PROG: milk3
4 LANG: C++
5 */
6 #include <iostream>
7 #include <fstream>
8 #include <algorithm>
9
10 using namespace std;
11
12
13 #include <queue>
14 #define minN(x,y) ((x)<(y)?(x):(y))
15 bool haveDo[22][22][22]={false};
16
17 struct work
18 {
19 int a;
20 int b;
21 int c;
22 work(int a1,int b1,int c1)
23 {
24 a=a1;
25 b=b1;
26 c=c1;
27 }
28 };
29
30 int res[22];
31 int res_top=0;
32 std::queue<work> wq;
33 int max_a,max_b,max_c;
34 int main()
35 {
36 ofstream fout ("milk3.out");
37 ifstream fin ("milk3.in");
38
39
40 fin>>max_a>>max_b>>max_c;
41 work temp(0,0,max_c);
42 haveDo[0][0][max_c] = true;
43 wq.push(temp);
44
45 while(!wq.empty())
46 {
47 temp = wq.front();
48 wq.pop();
49 if(temp.a==0) res[res_top++] = temp.c;
50 if(temp.a!=0)
51 {
52 if(temp.c!=max_c)
53 {
54 int a_c = min(temp.a,max_c-temp.c);
55 if(!haveDo[temp.a-a_c][temp.b][temp.c+a_c])
56 {
57 haveDo[temp.a-a_c][temp.b][temp.c+a_c] = true;
58 wq.push(work(temp.a-a_c,temp.b,temp.c+a_c));
59 }
60 }
61 if(temp.b!=max_b)
62 {
63 int a_b = min(temp.a,max_b-temp.b);
64 if(!haveDo[temp.a-a_b][temp.b][temp.b+a_b])
65 {
66 haveDo[temp.a-a_b][temp.b+a_b][temp.c] = true;
67 wq.push(work(temp.a-a_b,temp.b+a_b,temp.c));
68 }
69 }
70
71 }
72 if(temp.b!=0)
73 {
74 if(temp.c!=max_c)
75 {
76 int b_c = min(temp.b,max_c-temp.c);
77 if(!haveDo[temp.a][temp.b-b_c][temp.c+b_c])
78 {
79 haveDo[temp.a][temp.b-b_c][temp.c+b_c] = true;
80 wq.push(work(temp.a,temp.b-b_c,temp.c+b_c));
81 }
82 }
83 if(temp.a!=max_a)
84 {
85 int b_a = min(temp.b,max_a-temp.a);
86 if(!haveDo[temp.a+b_a][temp.b-b_a][temp.c])
87 {
88 haveDo[temp.a+b_a][temp.b-b_a][temp.c] = true;
89 wq.push(work(temp.a+b_a,temp.b-b_a,temp.c));
90 }
91 }
92
93 }
94 if(temp.c!=0)
95 {
96 if(temp.a!=max_a)
97 {
98 int c_a = min(temp.c,max_a-temp.a);
99 if(!haveDo[temp.a+c_a][temp.b][temp.c-c_a])
100 {
101 haveDo[temp.a+c_a][temp.b][temp.c-c_a] = true;
102 wq.push(work(temp.a+c_a,temp.b,temp.c-c_a));
103 }
104 }
105 if(temp.b!=max_b)
106 {
107 int c_b = min(temp.c,max_b-temp.b);
108 if(!haveDo[temp.a][temp.b+c_b][temp.c-c_b])
109 {
110 haveDo[temp.a][temp.b+c_b][temp.c-c_b] = true;
111 wq.push(work(temp.a,temp.b+c_b,temp.c-c_b));
112 }
113 }
114
115 }
116
117
118 }
119
120 sort(res,res+res_top);
121 int last = res[0];
122 fout<<res[0];
123 for(int i = 1;i!=res_top;++i)
124 {
125 if(res[i] == last) continue;
126 last = res[i];
127 fout<<" "<<res[i];
128 }
129 fout<<endl;
130 fin.close();
131 fout.close();
132 return 0;
133 }
posted @ 2011-05-02 12:27  幻魇  阅读(201)  评论(0编辑  收藏  举报