1226 water pouring problem, 1226 water pouring Problem

Source: Internet
Author: User

1226 water pouring problem, 1226 water pouring Problem
1226 water pouring Problems

 

Time Limit: 1 s space limit: 128000 KB title level: Gold Title Description Description

Two flags without scale are available for both x and y litre (x and y are integers and are not greater than 100. There is also a water tank, which can be used to fill the water bottle or take the water poured out from the water bottle. The water can also be dumped between the two water bottles. It is known that x is an empty pot, and y is an empty pot. Ask how to pour water or fill water, and use the minimum number of steps to generate a z (z ≤ 100) litre of water in the x or y litre pot.

Input description Input Description

One row, three data records, x, y, and z;

Output description Output Description

A row outputs the minimum number of steps. If the target cannot be reached, the output is "impossible"

Sample Input Sample Input

3 22 1

Sample output Sample Output

14

Data range and prompt Data Size & HintCATEGORY tag Tags click here to expand

 

This question, is indeed done by me...

First, the question is too disgusting... There are eight situations... At the beginning, I only considered four types ....

Then, when recording the number of steps, step 2 is the correct answer...

The face of AC is awesome .....

1 # include <iostream> 2 # include <cstdio> 3 # include <queue> 4 # include <cstdlib> 5 using namespace std; 6 const int MAXN = 1001; 7 int vis [MAXN] [MAXN]; 8 int x, y, z; 9 int step = 0; 10 int p1, p2; 11 void bfs () 12 {13 queue <int> qx; queue <int> qy; 14 qx. push (x); qy. push (y); 15 while (qx. size ()! = 0 & qy. size ()! = 0) 16 {17 int wx = qx. front (); int wy = qy. front (); 18 if (wx = z | wy = z) 19 {printf ("% d", step/2); exit (0 );} 20 qx. pop (); 21 qy. pop (); 22 if (wx <0 | wy <0 | wx> x | wy> y | vis [wx] [wy] = 1) continue; 23 vis [wx] [wy] = 1; 24 step ++; 25 qx. push (x); qy. push (wy); // x filled with 26 qx. push (0); qy. push (wy); // empty x 27 qx. push (wx); qy. push (y); // y filled with 28 qx. push (wx); qy. push (0); // empty y 29 qx. push (0); qy. push (wy + wx); // x-> y no shenyu30 qx. push (wx-y + wy); qy. push (y); // you shengyu31 qx. push (wx + wy); qy. push (0); // y-> x noshengyu32 qx. push (x); qy. push (wy-x + wx); // youshengyu... 33} 34} 35 int main () 36 {37 scanf ("% d", & x, & y, & z); 38 bfs (); 39 printf ("impossible"); 40 return 0; 41}

 

Related Article

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.