1226 pour water problem

Source: Internet
Author: User

1226 pour water problem

time limit: 1 sspace limit: 128000 KBtitle level: Golden Gold SolvingView Run ResultsTitle Description Description

There are two water kettles with no tick marks, each of which can be fitted with an X-liter and a Y-liter (an integer with x, Y and no more than 100). There is another water tank which can be used for watering the kettle or pouring it out from the kettle, and the water can also be dumped between the two kettles. The X-liter pot is known as an empty pot, and the Y-liter pot is an empty pot. Ask how to use the water or irrigation operation, with a minimum number of steps can be in the X or y liters of the pot to measure the Z (z≤100) liters.

Enter a description Input Description

One row, three data, representing X, Y and z, respectively;

Output description Output Description

One row, outputs the minimum number of steps, and outputs "impossible" if the target cannot be reached

Sample input Sample Input

3 22 1

Sample output Sample Output

14

Data range and Tips Data Size & HintCategory labels Tags Click here to expandBreadth-First search depth-first search iterative search search
#include <cstdio>#include<iostream>#defineMAXN 200#defineINF 10000000using namespacestd;intF[maxn][maxn],a,b,z;//F[x][y]: The number of steps required to reach a bucket of water in the x,b bucket of water in the state of YvoidDfsintXintYintStep) {//x:a barrels of water, y:b barrels of water, step: The number of steps in the current    if(f[x][y]!=0&&step+1>=f[x][y])return;//the current state already has a solution and now the solution must be worse than the previous solution, exitf[x][y]=step+1;//minimum number of steps required to update the current stateDFS (x,0, step+1);//1. Empty the B-bucketDfs0, y,step+1);//2. Empty a bucketDFS (x,b,step+1);//3, filled with b barrelsDFS (a,y,step+1);//4. Fill a bucket//5. Pour the b bucket into a bucket    if(x+y<=a) DFS (x+y,0, step+1);//(i) b barrels will not overflow after emptying    ElseDFS (a,x+y-a,step+1);//(ii) b barrels will overflow after emptying, so there is residue in B -bucket//6. Pour a barrel into B-bucket    if(x+y<=b) DFS (0, x+y,step+1);//(i) a bucket will not overflow after emptying the B bucket    ElseDFS (x+y-b,b,step+1);//(ii) a barrel will overflow after emptying, so there is residue in a bucket}intMain () {intans=INF; scanf ("%d%d%d",&a,&b,&z); DFS (0,0,0);  for(intI=1; i<=a;i++)        if(F[i][z]) ans=min (Ans,f[i][z]);//The optimal solution is obtained by traversing the z of water in all B-buckets .     for(intI=1; i<=b;i++)        if(F[z][i]) ans=min (Ans,f[z][i]);//The optimal solution is obtained by traversing the z of water in all B-buckets .    if(Ans==inf) printf ("impossible\n"); Elseprintf"%d\n", ans-1); return 0;}

1226 pour water problem

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.