Milk pails (BFS)

Source: Internet
Author: User

Milk pails

Time limit: 1 Sec memory limit: up to MB
Submitted: 4 Resolution:
Submitted State [Discussion Version]

Title DescriptionFarmer John has received a order for exactly M units of milk (1≤m≤200) that he needs to fill right away. Unfortunately, his fancy milking machine had just become broken, and all he had is is both milk pails of integer sizes X and Y (1≤x,y≤100) with which he can measure milk. Both Pails is initially empty. Using These-pails, he can perform up to K of the following types of operations (1≤K≤100):

-He can fill either pail completely to the top.

-He can empty either pail.

-He can pour the contents of one pail into the other, stopping when the former becomes empty or the latter becomes full ( Whichever of these happens first).

Although FJ realizes he is not being able to end up with exactly M total units of milk in the both pails, please help him Compute the minimum amount of error between M and the total amount of milk in the pails. That's, please compute the minimum value of | m−m′| such that FJ can construct m′units of milk collectively between the pails.
InputThe first, and only line of input, contains X, Y, K, and M.
OutputOutput the smallest distance from M to an amount of milk FJ can produce.
Sample input
14 50 2 32
Sample output
18
"Analysis" This is particularly similar to yesterday's mother's milk, two bottles, initially empty, there are three kinds of operations, a bottle filled with a bottle empty, and then will be a bottle net another bottle pour, until the end
or to full. Changed yesterday's code a little bit.
#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= About;intN,dp[n],len;intW[n][n];intg[3];inta,b,c;intx,y,k,m;Set<int>p;intminn=inf;structMan {intx, y; intstep;};voidBFs () {w[0][0]=1; Queue<man>Q;    Man S; S.x=0; S.y=0; S.step=0;    Q.push (s);  while(!Q.empty ()) {Man T=Q.front ();        Q.pop (); Minn=min (Minn,abs (T.X+T.Y)-M)); if(t.step==k)Continue; intf[3]; f[0]=T.x; f[1]=T.y; if(!w[f[0]][0]) {man kk;kk.x=f[0];kk.y=0; kk.step=t.step+1; w[f[0]][0]=1; Q.push (KK);} if(!w[f[0]][g[1]]) {man kk;kk.x=f[0];kk.y=g[1];kk.step=t.step+1; w[f[0]][g[1]]=1; Q.push (KK);} if(!w[0][f[1]]) {man kk;kk.x=0; kk.y=f[1];kk.step=t.step+1; w[0][f[1]]=1; Q.push (KK);} if(!w[g[0]][f[1]]) {man kk;kk.x=g[0];kk.y=f[1];kk.step=t.step+1; w[g[0]][f[1]]=1; Q.push (KK);}  for(intI=0; i<2; i++) {f[0]=T.x; f[1]=T.y; if(f[i]==0)Continue;  for(intj=0; j<2; J + +) {f[0]=T.x; f[1]=T.y;                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]; //printf ("@%d%d\n", F[i],f[j]);}Else if(f[i]+f[j]<G[j]) {F[j]+=F[i]; F[i]=0; } k.x=f[0]; K.y=f[1]; //printf ("!!! %d%d%d\n ", k.x,k.y,k.z);                if(w[k.x][k.y]==0) {K.step=t.step+1;                    Q.push (k); W[K.X][K.Y]=1; }            }        }    }}intMain () {memset (W,0,sizeof(w)); CIN>>X>>Y>>K>>M; g[0]=y; g[1]=x;   BFS (); cout<<minn<<Endl; return 0;}
View Code

Milk pails (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.