Nyoj 213 cups of water (BFS)

Source: Internet
Author: User

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

  1. #include <stdio.h>
  2. #include <string.h>
  3. #define MAX 1000
  4. typedef struct state{
  5. int x;
  6. int y;
  7. int Step;
  8. }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
  9. State que[max],p,q; //que for Queues
  10. int f,r;
  11. int N,a,b,c,x,y,z,d,newx,newy;
  12. int Vis[max][max]; //tag array records whether a state has been included
  13. void push () {
  14. QUE[R].X=NEWX;
  15. Que[r].y=newy;
  16. que[r++].step=q.step+1;
  17. Vis[newx][newy]=1;
  18. }
  19. int BFs () {
  20. Que[r].x=a;
  21. que[r].y=0;
  22. Que[r++].step=0;
  23. Vis[a][0]=1;
  24. while (F<r) {
  25. q.x=que[f].x;
  26. Q.Y=QUE[F].Y;
  27. Q.step=que[f].step;
  28. if (q.x = = X&&q.y = = y) return Q.step; //Output steps when conditions are met
  29. f++;
  30. 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
  31. if (b-q.y>=q.x) {
  32. newx=0;
  33. newy=q.y+q.x;
  34. if (!vis[newx][newy]) push ();
  35. }
  36. Else {
  37. NEWX=Q.X-B+Q.Y;
  38. Newy=b;
  39. if (!vis[newx][newy]) push ();
  40. }
  41. if (C-(A-Q.Y) >=0) {
  42. newx=0;
  43. NEWY=Q.Y;
  44. if (!vis[newx][newy]) push ();
  45. }
  46. Else {
  47. newx= (A-Q.Y)-C;
  48. NEWY=Q.Y;
  49. if (!vis[newx][newy])
  50. Push ();
  51. }
  52. }
  53. if (q.y>0) {//b in the Water Cup pour a must be finished, so do not judge, pour C in Need
  54. newy=0;
  55. NEWX=Q.X+Q.Y;
  56. if (!vis[newx][newy])
  57. Push ();
  58. if (C-(a-q.x) >=0) {
  59. newx=q.x;
  60. newy=0;
  61. if (!vis[newx][newy]) push ();
  62. }
  63. Else {
  64. newx=q.x;
  65. Newy= (a-q.x)-C;
  66. if (!vis[newx][newy]) push ();
  67. }
  68. }
  69. if (a-q.x-q.y>0) {//pour in a cup the same situation does not need to be judged
  70. NEWX=A-Q.Y;
  71. NEWY=Q.Y;
  72. if (!vis[newx][newy]) push ();
  73. if (b>=a-q.x) {
  74. newx=q.x;
  75. newy=a-q.x;
  76. if (!vis[newx][newy]) push ();
  77. }
  78. Else {
  79. newx=q.x;
  80. Newy=b;
  81. if (!vis[newx][newy]) push ();
  82. }
  83. }
  84. }
  85. return -1;
  86. }
  87. int Main () {
  88. scanf ("%d", &n);
  89. while (n--) {
  90. scanf ("%d%d%d", &a,&b,&c);
  91. scanf ("%d%d%d", &x,&y,&z);
  92. printf ("%d\n", BFS ());
  93. memset (vis,0,sizeof(VIS));
  94. f=r=0;
  95. }
  96. return 0;
  97. }

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)

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.