Mother's Milk (milk) (BFS)

Source: Internet
Author: User

Question A: Mother's Milk (milk) time limit: 1 Sec memory limit: MB
Submitted by: + Resolution: 8
Submitted State [Discussion Version] Title Description

Farmer John has three volumes of a, B, c-liter barrels, a, B, and C are three from 1 to 20 of integers, initially, a and B barrels are empty, and the C bucket is filled with milk. Sometimes John poured milk from one barrel to another until it was filled with barrels or the original bucket was empty. Of course every infusion is complete, because of the savings, the milk will not be lost. Write a program to help John find out when a bucket is empty, the amount of milk left in the C bucket is all that is possible.

Input

A separate line of 1, including three integers, a, B, and C.

Output

There are only 1 rows, which list all the possibilities for the amount of milk left in the C bucket when the A bucket is empty.

Sample input
8 9 10
Sample output
1 2 8) 9 10
The "analysis" uses a structure to store three barrels of state, then violent BFS, and then sets the result with set.
#include <iostream>#include<cstring>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<time.h>#include<string>#include<map>#include<stack>#include<vector>#include<Set>#include<queue>#defineINF 0x3f3f3f3f#defineMoD 1000000007typedefLong Longll;using namespacestd;Const intn=100010;intN;intw[ +][ +];intg[3];inta,b,c;Set<int>p;structMan {intx, Y, z;};voidBFsintAaintBbintcc) {W[AA][BB]=1; Queue<man>Q;    Man S; S.x=0; S.y=0; S.z=cc;    Q.push (s);  while(!Q.empty ()) {Man T=Q.front ();        Q.pop (); if(t.x==0) P.insert (T.Z); intf[3];  for(intI=0; i<3; i++) {f[0]=t.x;f[1]=t.y; f[2]=t.z; if(f[i]==0)Continue;  for(intj=0; j<3; J + +) {f[0]=t.x; f[1]=t.y; f[2]=t.z;                Man K; if(i==j| | F[J]==G[J])Continue; if(f[i]+f[j]>=G[j]) {F[i]=f[i]-(g[j]-F[j]); F[J]=G[j]; } Else if(f[i]+f[j]<G[j]) {F[j]+=F[i]; F[i]=0; } k.x=f[0]; K.y=f[1]; K.z=f[2]; if(w[k.x][k.y]==0) {Q.push (k); W[K.X][K.Y]=1; }            }        }    }}intMain () {memset (W,0,sizeof(w)); CIN>>a>>b>>C; g[0]=A; g[1]=b; g[2]=C;    BFS (A,B,C); intr[ -],mm=0;  for(Set<int>::iterator It=p.begin (); It!=p.end (); it++) {r[mm++]=*it; } sort (R,r+MM); cout<<r[0];  for(intI=1; i<mm;i++) cout<<" "<<r[i];cout<<Endl; return 0;}
View Code

Mother's Milk (milk) (BFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.