Spoj Pouring water

Source: Internet
Author: User

Transmission Door

Pour1-pouring WATER#GCD #recursion

Given vessels, one of which can accommodate a litres of water and the other- b litres of water, det Ermine the number of steps required to obtain exactly C litres of water in one of the vessels.

At the beginning both vessels is empty. The following operations is counted as ' steps ':

    • Emptying a vessel,
    • Filling a vessel,
    • Pouring water from one vessel to the other, without spilling, until one of the vessels are either full or empty.

Input

An integer t, 1<=t<=100, denoting the number of testcases, followed by T sets of input da TA, each consisting of three positive integers A, B, C, not larger than 40000, given in separate lines.

Output

For each set of input data, output the minimum number of steps required to obtain C litres, or-1 if this is impossible.

Examplesample Input:
2523234

Sample output:
2-1
------------------------

Solution
BFS
The most important thing to write BFS is to avoid repeating the same state of the queue.
Also check that the target status should be in each state out of the team, because the exit of the state is "unique", and the entrance generally has a number of situations (that is, the team head state can generally be transferred to a number of new States), note the bold line in the code.
In addition, because the initial state of the problem can be transferred to the state is not many (also because the two-dimensional array is not open), you should use the map to save each state to the initial state of the distance (and the minimum number of transfer steps required).
Another trick is to write Enqueue as a function that avoids the duplication of code that comes with multiple state transitions.
#include <bits/stdc++.h>using namespaceStd;typedef pair<int,int>P;intgcdintAintb) {returnB?GCD (b, a%b): A;} Map<p,int>Mp;queue<P>que;void Enque(intAintBintd) {    inttmp=mp[{a, b}]; if(!tmp| | Tmp>d) {mp[{a, B}]=D;    Que.push ({A, b}); }}BOOLOkintAintBintc) {returna==c| | b==C;}//How BFS works?voidBFsintXintYintAintBintc)    {mp.clear ();  while(!que.empty ()) Que.pop (); Mp[{x, y}]=1;    Que.push ({x, y});  while(!Que.empty ()) {P Top=que.front (); Que.pop ();intD=Mp[top]; intX=top.first, y=Top.second;  if(x==c| | Y==c) {printf ("%d\n", D-1); return ;} if(x) Enque (0, Y, d+1); if(y) enque (x,0, d+1); if(x!=a) {Enque (A, Y, D+1); if(y) {intAdd=min (Y, A-x); Enque (x+add, Y-add, d+1); }        }        if(y!=b) {Enque (x, B, D+1); if(x) {intAdd=min (x, by); Enque (x-add, Y+add, d+1); }}} puts ("-1");}intMain () {intT scanf"%d", &T);  for(intA, B, C; t--;) {scanf ("%d%d%d", &a, &b, &c); if(C>max (A, B)) {Puts ("-1");Continue;} if(c==a| | C==b) {puts ("1");Continue;} if(a==b) {Puts ("-1");Continue;} if(C%GCD (A, B)) {Puts ("-1");Continue;} BFS (0,0, A, b, c); }}

----------------------------------------

Write this question to the main () in a few continue all write return, unexpectedly over a sample, sure enough, the sample did not hang. In the future debug should pay attention to there is no continue write return place.



Spoj Pouring water

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.