Bfs + hash table + three cups of water

Source: Internet
Author: User

Three water cups are provided in different sizes. Only the largest water cup is filled with water, and the other two are empty. The three water cups pour water between each other, and the water cup is not identified, it can only be calculated based on the given water cup volume. Now, you need to write a program that outputs the minimum number of times the initial state reaches the target State.
Input
The first line is an integer N (0 <N <50), indicating N groups of test data.
Next, there are two rows of test data in each group. The first row contains three integers V1 V2 V3 (V1> V2> V3 V1 <100 V3> 0), indicating the volume of the three cups.
The second row provides three integers, E1 E2 E3 (the volume is smaller than or equal to the size of the corresponding cup), indicating the final state we need.
Output
The minimum number of times the test data is flushed per row. Output-1 if the target State is not reached

 

 

 


# Include <string. h>

# Include <stdlib. h>
# Include <ctype. h>
# Include <stdio. h>
Int visit [100] [100] [100]; // records the status that has occurred.
Int dis [1000]; // The number of times the water reaches during each State record
Int p [3]; // capacity of three bottles of water
Int a [1000] [3]; // pouring status
Int goal [3]; // target status
Void visited (int tmp [3]);
Int check (int tmp [3]);
Bool pour (int tmp [3], int I, int j)
{
If (tmp [I] = 0) return false; // The bottle has no water
If (tmp [j] = p [j]) return false; // another bottle is filled with water
If (tmp [I]> (p [j]-tmp [j])
{
Tmp [I]-= (p [j]-tmp [j]);
Tmp [j] = p [j];
}
Else
{
Tmp [j] + = tmp [I];
Tmp [I] = 0;
}
If (check (tmp ))
{
Return false;
}
Else
{
Visited (tmp );
Return true;
}
}


Int check (int tmp [3]) // check whether a status exists
{
If (visit [tmp [0] [tmp [1] [tmp [2])
Return 1;
Return 0;
}


Void visited (int tmp [3]) // record this status has occurred
{
Visit [tmp [0] [tmp [1] [tmp [2] = 1;
}


Int bfs ()
{
Int I, j, k;
Int head = 0, tail = 1;
Visited (a [head]); // The initial status of the record is
While (head <tail)
{
For (I = 0; I <3; I ++)
If (a [head] [I]! = Goal [I]) break;
If (I> = 3)
Return dis [head]; // The status is the same as that of the target.
For (I = 0; I <3; I ++)
For (j = 0; j <3; j ++)
{
For (k = 0; k <3; k ++) // view the status of the next node at layer I
A [tail] [k] = a [head] [k];
If (j! = I)
If (pour (a [tail], I, j ))
{
Dis [tail] = dis [head] + 1;
Tail ++; // enters the queue
}
}
Head ++; // output queue
}
Return-1;
}
Int main ()
{
Int count [100];
Int n;
Scanf ("% d", & n );
Int I =-1;
While (++ I <n)
{
Memset (a, 0, sizeof ());
Memset (dis, 0, sizeof (dis ));
Memset (visit, 0, sizeof (visit ));


Scanf ("% d", & p [0], & p [1], & p [2]);
Scanf ("% d", & goal [0], & goal [1], & goal [2]);
A [0] [0] = p [0];


Int min = bfs ();
Count [I] = min;
}
For (I = 0; I <n; I ++)
Printf ("% d \ n", count [I]);
Return 1;
}
 

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.