Please point me in the question.
The following:
Water Cup pour water problem is classic, the same routine, BFS find all the state. The key to this problem is that each time should be carried out six times to try to pour water, careful. PS: three-dimensional array representation of the state is really convenient.
Code implementation:
/*id:eashionlang:c++task:milk3*/#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <queue>#include <algorithm>#define MAXUsing namespaceSTD;struct state{int A,b,c;};int A,b,c;int Save[max];Queue<state> Q;int Show[max][max][max];int main () {Freopen ("Milk3.in","R", stdin);Freopen ("Milk3.out","W", stdout);while (scanf ("%d%d%d", &a,&b,&c)! = EOF) {int pos =0;memset (Save,0, sizeof (SAVE));Memset (Show,0, sizeof (show));show[0][0][C] =1;State tmp;Tmp. A=0;Tmp. b=0;Tmp. C= C;Q. Push(TMP);while (! Q. Empty()) {State cur;Cur = Q. Front();Q. Pop();if (cur. A==0) {Save[pos] = cur. C;pos++;} State Nstate;int TA,TB,TC;if (cur. A!=0) {TA = cur. A;TB = cur. b;TC = cur. C;if (TA+TB <= B) {tb + = TA;TA =0;TC = cur. C;} else{ta = ta+tb-b;TB = B;TC = cur. C;} if (SHOW[TA][TB][TC]! =1) {nstate. A= Ta;Nstate. b= TB;Nstate. C= TC;Q. Push(nstate);SHOW[TA][TB][TC] =1;} TA = cur. A;TB = cur. b;TC = cur. C;if (TA+TC <= C) {tc + = TA;TA =0;TB = cur. b;} else{ta = ta+tc-c;TC = C;TB = cur. b;} if (SHOW[TA][TB][TC]! =1) {nstate. A= Ta;Nstate. b= TB;Nstate. C= TC;Q. Push(nstate);SHOW[TA][TB][TC] =1;}} if (cur. b!=0) {TA = cur. A;TB = cur. b;TC = cur. C;if (TA+TB <= A) {ta + = TB;TB =0;TC = cur. C;} else{TB = ta+tb-a;TA = A;TC = cur. C;} if (SHOW[TA][TB][TC]! =1) {nstate. A= Ta;Nstate. b= TB;Nstate. C= TC;Q. Push(nstate);SHOW[TA][TB][TC] =1;} TA = cur. A;TB = cur. b;TC = cur. C;if (TB+TC <= C) {TC + = TB;TB =0;TA = cur. A;} else{TB = tb+tc-c;TC = C;TA = cur. A;} if (SHOW[TA][TB][TC]! =1) {nstate. A= Ta;Nstate. b= TB;Nstate. C= TC;Q. Push(nstate);SHOW[TA][TB][TC] =1;}} if (cur. C!=0) {TA = cur. A;TB = cur. b;TC = cur. C;if (TC+TB <= B) {TB + = TC;TC =0;TA = cur. A;} else{TC = Tc+tb-b;TB = B;TA = cur. A;} if (SHOW[TA][TB][TC]! =1) {nstate. A= Ta;Nstate. b= TB;Nstate. C= TC;Q. Push(nstate);SHOW[TA][TB][TC] =1;} TA = cur. A;TB = cur. b;TC = cur. C;if (TA+TC <= A) {ta + = TC;TC =0;TB = cur. b;} else{TC = ta+tc-a;TA = A;TB = cur. b;} if (SHOW[TA][TB][TC]! =1) {nstate. A= Ta;Nstate. b= TB;Nstate. C= TC;Q. Push(nstate);SHOW[TA][TB][TC] =1;}}} sort (Save,save+pos);for (int i =0; i < POS; i++) {if (i! = pos-1) {printf ("%d", Save[i]);} else{printf ("%d\n", Save[i]);}}} return0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Usaco mother ' s Milk (BFS)