Three cups of water
time limit: ms | Memory limit:65535 KB
Difficulty: 4
Describe
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
first line an integer N (0<n<50) represents N Group 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 a three cup of water.
the second line gives three integers E1 E2 E3 (volume less than equal to the corresponding cup volume) indicates 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
- #include <stdio.h>
- #include <string.h>
- #define MAX 1000
- typedef struct state{
- int x;
- int y;
- int Step;
- }state; The //status indicates that the first Cup has an X-liter of water, the Second Cup has an ascending water, and step indicates the number of steps required to the current state
- State que[max],p,q; //que for Queues
- int f,r;
- int N,a,b,c,x,y,z,d,newx,newy;
- int Vis[max][max]; //tag array records whether a state has been included
- void push () {
- QUE[R].X=NEWX;
- Que[r].y=newy;
- que[r++].step=q.step+1;
- Vis[newx][newy]=1;
- }
- int BFs () {
- Que[r].x=a;
- que[r].y=0;
- Que[r++].step=0;
- Vis[a][0]=1;
- while (F<r) {
- q.x=que[f].x;
- Q.Y=QUE[F].Y;
- Q.step=que[f].step;
- if (q.x = = X&&q.y = = y) return Q.step; //Output steps when conditions are met
- f++;
- if (q.x>0) {//If there is water in a cup, determine if the state of pouring water into the B,c Cup is present and received in the queue
- if (b-q.y>=q.x) {
- newx=0;
- newy=q.y+q.x;
- if (!vis[newx][newy]) push ();
- }
- Else {
- NEWX=Q.X-B+Q.Y;
- Newy=b;
- if (!vis[newx][newy]) push ();
- }
- if (C-(A-Q.Y) >=0) {
- newx=0;
- NEWY=Q.Y;
- if (!vis[newx][newy]) push ();
- }
- Else {
- newx= (A-Q.Y)-C;
- NEWY=Q.Y;
- if (!vis[newx][newy])
- Push ();
- }
- }
- if (q.y>0) {//b in the Water Cup pour a must be finished, so do not judge, pour C in Need
- newy=0;
- NEWX=Q.X+Q.Y;
- if (!vis[newx][newy])
- Push ();
- if (C-(a-q.x) >=0) {
- newx=q.x;
- newy=0;
- if (!vis[newx][newy]) push ();
- }
- Else {
- newx=q.x;
- Newy= (a-q.x)-C;
- if (!vis[newx][newy]) push ();
- }
- }
- if (a-q.x-q.y>0) {//pour in a cup the same situation does not need to be judged
- NEWX=A-Q.Y;
- NEWY=Q.Y;
- if (!vis[newx][newy]) push ();
- if (b>=a-q.x) {
- newx=q.x;
- newy=a-q.x;
- if (!vis[newx][newy]) push ();
- }
- Else {
- newx=q.x;
- Newy=b;
- if (!vis[newx][newy]) push ();
- }
- }
- }
- return -1;
- }
- int Main () {
- scanf ("%d", &n);
- while (n--) {
- scanf ("%d%d%d", &a,&b,&c);
- scanf ("%d%d%d", &x,&y,&z);
- printf ("%d\n", BFS ());
- memset (vis,0,sizeof(VIS));
- f=r=0;
- }
- return 0;
- }
This topic is actually the BFS of the solution diagram, transfer from one state to another state, the representation of the State and that no brain doctor is the same, when the two problems are not the same solution so the search method is also different, the problem requires a number of state solutions, and this requires a special state solution, which can be seen in two ways to apply the problem. To solve the problem only two, the state of the use of BFS, no brain that problem solved, and the second problem for starting from 1 o'clock in the morning, to 6 o'clock in the afternoon has been to do with BFS solve the maze problem (although the last one did not do it). is no longer a problem, so the topic naturally AC out, and this problem has also returned to a condition v1>v2>v3. This will require less judgment (here mistakenly thought C poured into B must be full, WA once), at the beginning of Max is too big, the result is MLE, this is the first time in life. , said, the problem with 72ms, and looked at the next ranking found, a lot of the 0ms of the death of venereal disease .... , as if using the black magic of the STL, that temporarily learn not to come, this time I have been very satisfied, yesterday, the home power outage, the morning of the computer was not electricity, so the afternoon can only be quiet reading, finally the second chapter of the C++prime to finish, read very high, ready to quote, pointer, const, There are those c++11 new features, that is, the second chapter of the content to do a summary, more than 9 start to write, to 2 points, and finally quickly finished, the results of temporary preservation area exploded, suddenly appear strange bug,2000 more than the words of pure hand hit the content is not (self-brain to fill the face), almost cried out, but I slept a sleep The mood calmed down to accept this fact, began the day before yesterday's plan to practice BFS, the results, in the garlic customers do two maze class problems, Leng did not do out together, always time out, mood old uncomfortable, then turn to Nyoj want to find other topics to do, casually open together, did not think just is BFS, do up, At first did not want to be able to do, because it is difficult 4 ah, but did find out, originally not difficult, ac after the mood finally reply, this problem is my first way to do out of the difficulty of the problem 4, is also the first successful AC BFS category, or the first MLE problem ... , sure enough to practice the algorithm or to find a classic topic to do better Ah, O (* ̄▽ ̄*) ブ.
Nyoj 213 cups of water (BFS)