C + + wide search Classic-Three water cups (a problem to understand wide search!) )

Source: Internet
Author: User

Recently reviewed the wide search, the feeling is actually searching is a kind of spirit, and this spirit is not afraid of difficulties to go to the end, mastering this essence can help you to solve some of the problems need examples.

Today to find a problem, it is worth learning, hope that through this problem to understand the wide search routines.

Source: http://acm.nyist.net/JudgeOnline/problem.php?pid=21 (Thanks to the author here!) )

By the way attach the topic:

Three cups of water

time limit: Ms | Memory limit: 65535 KB
Difficulty: 4

Description
give three cups, different sizes, and only the largest cup of water is filled, the remaining two are empty cups. Three cups of water are poured between each other, and the cups are not identified and can only be calculated according to the volume of the cups given. You are now asked to write a program that outputs the minimum number of times that the initial state reaches the target State.

input
The first line an integer n (0<n<50) represents the n set of test data
Next, each set of test data has two lines, the first line gives three integers V1 V2 V3 (v1>v2>v3 v1<100 v3>0) represents the volume of three cups.
the second line gives three integers E1 E2 E3 (volume less than equal to the corresponding Cup volume) represents the final state we need
Output
each row outputs the minimum number of water pours for the corresponding test data. If the target status output is not reached-1
Sample Input
2
6 3 1
4 1 1
9 3 2
7 1 1
Sample Output
3
-1


Attach the AC code in detail, may seem a bit cumbersome, in fact, it is not as difficult as you think, to understand, three cups pour water with each other 6 kinds of situation! I believe you can understand!
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace Std;
struct node{//struct represents the state in the process of pouring water
int a,b,c;
int step; Step indicates the number of steps required to present this state
}b,e; B, E indicates the start state and the target State, respectively
int v1,v2,v3;
BOOL f[105][105][105]; F (flag) indicates whether this state of three cups has been discussed and can be weighed
Wide search other questions such as to use the hash value is also to avoid repeated judgment state, save time

int BFS (Node B)
{
queue<node> Q;//General wide search is implemented by queue
Q.push (b);
f[b.a][b.b][b.c]=true;

node cur;
while (!q.empty ())//6 kind of pour water situation: patience, actually understand a other can be copied paste add a little change
{
Cur=q.front ();
if (cur.a==e.a && cur.b==e.b && cur.c==e.c)//Search to return, also count a pruning
return cur.step;
Q.pop ();

if (cur.a>0 && cur.b<v2)//v1 pour water into v2
{
int t=min (CUR.A,V2-CUR.B);//focus on understanding the change in water in two cups the minimum value of all the water in the bottle and the remainder of the container being poured .
node temp;
temp.a=cur.a-t;
temp.b=cur.b+t;
temp.c=cur.c;
temp.step=cur.step+1;//steps plus 1
if (!f[temp.a][temp.b][temp.c])
{
Q.push (temp);
f[temp.a][temp.b][temp.c]=true;
}
}

if (cur.a>0 && cur.c<v3)//v1 pour water into v3
{
int t=min (CUR.A,V3-CUR.C);//Key Understanding
node temp;
temp.a=cur.a-t;
temp.b=cur.b;
temp.c=cur.c+t;
temp.step=cur.step+1;//number of steps plus 1
if (!f[temp.a][temp.b][temp.c])
{
Q.push (temp);
f[temp.a][temp.b][temp.c]=true;
}
}

if (cur.b>0 && cur.a<v1)//v2 pour water into v1
{
int t=min (CUR.B,V1-CUR.A);//Key Understanding
node temp;
temp.a=cur.a+t;
temp.b=cur.b-t;
temp.c=cur.c;
temp.step=cur.step+1;//number of steps plus 1
if (!f[temp.a][temp.b][temp.c])
{
Q.push (temp);
f[temp.a][temp.b][temp.c]=true;
}
}

if (cur.b>0 && cur.c<v3)//v2 pour water into v3
{
int t=min (CUR.B,V3-CUR.C);//Key Understanding
node temp;
Temp.a=cur.a;
temp.b=cur.b-t;
temp.c=cur.c+t;
temp.step=cur.step+1;//number of steps plus 1
if (!f[temp.a][temp.b][temp.c])
{
Q.push (temp);
f[temp.a][temp.b][temp.c]=true;
}
}

if (cur.c>0 && cur.a<v1)//v3 pour water into v1
{
int t=min (CUR.C,V1-CUR.A);//Key Understanding
node temp;
temp.a=cur.a+t;
temp.b=cur.b;
temp.c=cur.c-t;
temp.step=cur.step+1;//number of steps plus 1
if (!f[temp.a][temp.b][temp.c])
{
Q.push (temp);
f[temp.a][temp.b][temp.c]=true;
}
}

if (cur.c>0 && cur.b<v2)//v3 pour water into v2
{
int t=min (CUR.C,V2-CUR.B);//Key Understanding
node temp;
Temp.a=cur.a;
temp.b=cur.b+t;
temp.c=cur.c-t;
temp.step=cur.step+1;//number of steps plus 1
if (!f[temp.a][temp.b][temp.c])
{
Q.push (temp);
f[temp.a][temp.b][temp.c]=true;
}
}
}
return-1;//indicates that the target State is still not found
}
int main ()
{
int n;
cin>>n;
While (n--)//Multiple queries
{
cin>>v1>>v2>>v3;
b.a=v1; b.b=0; b.c=0; b.step=0;//Initialize
cin>>e.a>>e.b>>e.c;//target status
memset (f,0,sizeof (f));//each time the mark is zeroed, it is placed in the loop
if (v1!=e.a+e.b+e.c)//Because this is definitely not found
cout<< "-1" <<endl;
Else Cout<<bfs (b) <<endl;//otherwise wide search
}
return 0;
}

Because the first share, there will be many unsatisfactory, but also please understand!

C + + wide search Classic-Three water cups (a problem to understand wide search!) )

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.