"CodeVS1226" pour water problem

Source: Internet
Author: User

Title 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

Exercises

BFS, a total of 6 possible:

1, a barrel empty 2, b barrels empty

3, a barrel full 4, B barrels full

5, a barrel into the B-barrel

1) Overflow

2) No overflow

6, b barrels poured into a barrel

1) Overflow

2) No overflow

It was wrong to write it.

    // 5 x->y    inch ;     = P.x-min (Y-p.y,p.x)    ; = p.y + min (Y-p.y,p.x);    Q.push (p);     // 6 y->x    inch ;     = p.x + min (x-p.x,p.y)    ; = P.y-min (x-p.x,p.y);    Q.push (p);

#include <iostream>#include<queue>#include<cstdlib>#include<cstdio>#defineN 1000using namespacestd;structcup{intX,y,step;} First,now;intX,y,z;queue<cup>Q;BOOLF[n][n];voidExtend (Cupinch){    if(f[inch. x][inch. Y])return; f[inch. x][inch. y] =1; inch. step++; //1Cup p =inch; P.x=0;    Q.push (P); //2p =inch; P.Y=0;    Q.push (P); //3p =inch; P.x=x;    Q.push (P); //4p =inch; P.Y=y;    Q.push (P); //5 X->yp =inch; P.x=inch. X-min (Yinch. Y,inch. x); P.Y=inch. Y + min (yinch. Y,inch. x);    Q.push (P); //6 y->xp =inch; P.x=inch. x + min (x-inch. x,inch. Y); P.Y=inch. y-min (Xinch. x,inch. Y); Q.push (P);}voidBFs () {Q.push (first);  while(!Q.empty ()) { Now=Q.front ();        Q.pop (); if(now.x = = Z | | now.y = =z) {printf ("%d", Now.step); Exit (0);    } extend (now); } printf ("Impossible");}intMain () {scanf ("%d%d%d",&x,&y,&z); First.step= First.x = First.y =0; BFS ();}

"CodeVS1226" 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.