POJ 3414 Pots

Source: Internet
Author: User

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 #defineSC (x) scanf ("%d",& (x))6 #definePF (x) printf ("%d\n", X)7 #defineCL (x, y) memset (x, y, sizeof (x))8 using namespacestd;9 Const intMAX = the;Ten intfront, rear; One intUsed[max][max]; A Charstr[6][Ten]= {"FILL (1)","FILL (2)","DROP (1)","DROP (2)","pour (ON)","pour (2,1)"}; - structnode - { the     intA, B; -     intN; -     intStep; -Node *Pre; +} Q[max*max];//array analog queue queue<node> q; - voidBack (Node a); + voidBFS (intAintBintc); A intMain () at { -     inta,b,c; -CL (Used,0); - SC (a); - SC (b); - SC (c); in BFS (A, B, c); - PF (q[rear].step); to Back (Q[rear]); + } - voidBack (Node A) the { *     if(A.pre! =NULL) $     {Panax Notoginseng         //printf ("%s\n", STR[A.N]); -Back (* (A.pre));//recursion, find out the initial operation theprintf"%s\n", STR[A.N]); +     } A     return ; the } + voidBFS (intAintBintc) - { $Front =0; $Rear =0; -Q[FRONT].A =0; -Q[FRONT].B =0; theQ[front].pre =NULL; -Q[front].step =0;Wuyiused[0][0] =1; therear++; -      while(Front! =rear) Wu     { -Node now = Q[front];//{0, 0, NULL, 0} AboutUSED[NOW.A][NOW.B] =1; $         if(!used[a][now.b] && now.a!=a)//Fill (1) Add a to fill -         { -Q[REAR].N =0;//record the actions performed -Q[rear].step = Q[front].step +1;//Step +1 AQ[REAR].A =A; +Q[REAR].B =now.b; theQ[rear].pre = &Q[front]; -USED[A][NOW.B] =1; $             if(A==c | | now.b==c) Break;//Complete the Operation therear++; the         } the         if(!used[now.a][b] && now.b!=b)//Fill (2) Add B to full the         { -Q[REAR].N =1; inQ[rear].step = q[front].step+1; theQ[REAR].A =NOW.A; theQ[REAR].B =b; AboutQ[rear].pre = &Q[front]; theUSED[NOW.A][B] =1; the             if(Now.a==c | | b==c) Break; therear++; +         } -         if(!used[0][NOW.B] && now.a!=0)//DROP (1) the         {BayiQ[REAR].N =2; theQ[rear].step = Q[front].step +1; theQ[REAR].A =0; -Q[REAR].B =now.b; -Q[rear].pre = &Q[front]; theused[0][NOW.B] =1; the             if(now.b = = c) Break;//no water in a therear++; the         } -         if(!used[now.a][0] && now.b!=0)//DROP (2) the         { theQ[REAR].N =3; theQ[rear].step = Q[front].step +1;94Q[REAR].A =NOW.A; theQ[REAR].B =0; theQ[rear].pre = &Q[front]; theused[now.a][0] =1;98             if(NOW.A = = c) Break;//There's no water in B. Aboutrear++; -         }101         intx = NOW.A < b-now.b? now.a:b-now.b;102         if(!used[now.a-x][now.b+x])//pour (ON)103         {104Q[REAR].N =4; theQ[rear].step = q[front].step+1;106Q[REAR].A = NOW.A-x;107q[rear].b = now.b +x;108Q[rear].pre = &Q[front];109USED[NOW.A-X][NOW.B+X] =1; the             if(Now.a-x==c | | now.b+x==c) Break;111rear++; the         }113         inty = a-now.a < now.b? a-now.a:now.b;//find the closest to the water to your own pot the         if(!used[now.a+y][now.b-y])//pour (2,1) the         { theQ[REAR].N =5;117Q[rear].step = q[front].step+1;118Q[REAR].A = now.a+y;119q[rear].b = now.b-y; -Q[rear].pre = &Q[front];121USED[NOW.A+Y][NOW.B-Y] =1;122             if(Now.a+y==c | | now.b-y==c) Break;123rear++;124         } thefront++;126}//when jumping out of the loop, Front==rear127     if(front = = rear) printf ("impossible\n");//Conditional Judgment -}
View Code

//bfs pour water problem, for the need to print a broad search problem, you must save the status and status of the Father pointer, and then inverse, based on the relationship between state and state

POJ 3414 Pots

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.