Mother‘s Milk
Farmer John has three milking buckets of capacity A, B, and C liters. Each of the numbers A, B, and C is an integer from 1 through 20, inclusive. Initially, buckets A and B are empty while bucket C is full of milk. Sometimes, FJ pours milk from one bucket to another until the second bucket is filled or the first bucket is empty. Once begun, a pour must be completed, of course. Being thrifty, no milk may be tossed out.
Write a program to help FJ determine what amounts of milk he can leave in bucket C when he begins with three buckets as above, pours milk among the buckets for a while, and then notes that bucket A is empty.
PROGRAM NAME: milk3
INPUT FORMAT
A single line with the three integers A, B, and C.
SAMPLE INPUT (file milk3.in)
8 9 10
OUTPUT FORMAT
A single line with a sorted list of all the possible amounts of milk that can be in bucket C when bucket A is empty.
SAMPLE OUTPUT (file milk3.out)
1 2 8 9 10
SAMPLE INPUT (file milk3.in)
2 5 10
SAMPLE OUTPUT (file milk3.out)
5 6 7 8 9 10 题意: Farmer John有三个桶A,B,C,输入三个桶的容积;开始时桶A和第桶B为空,桶C为满。Farmer John要做pours milk操作,把桶x的牛奶导入桶y中,除非桶y被倒满或桶x已倒空,一次操作算作结束。进行n次倒牛奶操作改变C桶中牛奶的余量,但要求终止时A桶要为空。问:C桶中牛奶的余量一共有多少种情况。 题解: 一共有6种操作 A->B;A->C; B->A;B->C; C->A;C->B;用宽搜就可以了做出来,但感觉这次我写的代码好丑,过于冗余。 ps:本人大三狗一枚,正在持续更新博客,文章里有任何问题,希望各位网友可以指出。若有疑问也可在评论区留言,我会尽快回复。希望能与各位网友互相学习,谢谢
/* ID: cxq_xia1 PROG: milk3 LANG: C++ */ #include <iostream> #include <cstdio> #include <queue> #include <cstring> using namespace std; struct node { int a,b,c; friend bool operator < (node x,node y) { return x.c > y.c; } }; bool flag[21][21][21]; queue<node> q; priority_queue<node> ans; int A,B,C,cnt=0; node tmp1,tmp2; void BFS() { tmp1.a=0;tmp1.b=0;tmp1.c=C; flag[tmp1.a][tmp1.b][tmp1.c]=true; q.push(tmp1); while(!q.empty()) { tmp1=q.front(); q.pop(); if(tmp1.a==0) { ans.push(tmp1); cnt++; } if(tmp1.a!=0) { if(tmp1.b+tmp1.a<=B) { tmp2.a=0; tmp2.b=tmp1.b+tmp1.a; tmp2.c=tmp1.c; } else { tmp2.a=tmp1.a-(B-tmp1.b); tmp2.b=B; tmp2.c=tmp1.c; } if(!flag[tmp2.a][tmp2.b][tmp2.c]) { flag[tmp2.a][tmp2.b][tmp2.c]=true; q.push(tmp2); } if(tmp1.c+tmp1.a<=C) { tmp2.a=0; tmp2.b=tmp1.b; tmp2.c=tmp1.c+tmp1.a; } else { tmp2.a=tmp1.a-(C-tmp1.c); tmp2.b=tmp1.b; tmp2.c=C; } if(!flag[tmp2.a][tmp2.b][tmp2.c]) { flag[tmp2.a][tmp2.b][tmp2.c]=true; q.push(tmp2); } } if(tmp1.b!=0) { if(tmp1.a+tmp1.b<=A) { tmp2.a=tmp1.a+tmp1.b; tmp2.b=0; tmp2.c=tmp1.c; } else { tmp2.a=A; tmp2.b=tmp1.b-(A-tmp1.a); tmp2.c=tmp1.c; } if(!flag[tmp2.a][tmp2.b][tmp2.c]) { flag[tmp2.a][tmp2.b][tmp2.c]=true; q.push(tmp2); } if(tmp1.c+tmp1.b<=C) { tmp2.a=tmp1.a; tmp2.b=0; tmp2.c=tmp1.c+tmp1.b; } else { tmp2.a=tmp1.a; tmp2.b=tmp1.b-(C-tmp1.c); tmp2.c=C; } if(!flag[tmp2.a][tmp2.b][tmp2.c]) { flag[tmp2.a][tmp2.b][tmp2.c]=true; q.push(tmp2); } } if(tmp1.c!=0) { if(tmp1.a+tmp1.c<=A) { tmp2.a=tmp1.a+tmp1.c; tmp2.b=tmp1.b; tmp2.c=0; } else { tmp2.a=A; tmp2.b=tmp1.b; tmp2.c=tmp1.c-(A-tmp1.a); } if(!flag[tmp2.a][tmp2.b][tmp2.c]) { flag[tmp2.a][tmp2.b][tmp2.c]=true; q.push(tmp2); } if(tmp1.b+tmp1.c<=B) { tmp2.a=tmp1.a; tmp2.b=tmp1.b+tmp1.c; tmp2.c=0; } else { tmp2.a=tmp1.a; tmp2.b=B; tmp2.c=tmp1.c-(B-tmp1.b); } if(!flag[tmp2.a][tmp2.b][tmp2.c]) { flag[tmp2.a][tmp2.b][tmp2.c]=true; q.push(tmp2); } } } } int main() { freopen("milk3.in","r",stdin); freopen("milk3.out","w",stdout); memset(flag,false,sizeof(flag)); cin >> A >> B >> C; BFS(); node tmp; int i; while(!ans.empty()) { tmp=ans.top(); ans.pop(); i++; if(i!=cnt) cout << tmp.c << " "; else cout << tmp.c <<endl ; } return 0; }
USACO 1.4 Mother's Milk