(POJ) 3414 Pots (wide search of output path)

Source: Internet
Author: User

Descriptionyou is given the pots, having the volume of A and B liters respectively. The following operations can be Performed:fill (i) FILL the pot I (1≤i≤2) fromThe tap;drop (i) empty the pot I to the drain; Pour (i,j) pour fromPot I to pot J; After ThisOperation either the pot J isFull (and there is some water leftinchThe pot i), or the pot I isempty (and all its contents has been moved to the pot J). Write a program to find the shortest possible sequence of these operations that wouldyieldExactly C liters of waterinchOne of the pots. Inputon the first and only line is the numbers A, B, and C. These is all integersinchThe range from 1To -and C≤max (A, B). Outputthe first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If There is several sequences of minimal length, output any one of them. If the desired result can ' t is achieved, the first and only line of the file must contain the word ' impossible '. Sample Input3 5 4Sample Output6FILL (2) pour (2,1) DROP (1) pour (2,1) FILL (2) pour (2,1)

Test instructions has two cups volume A and b liters, six operation, after several operations can make at least one cup of water has C l, and output operation method

Methods use wide search, each state by the operation of the state down search, find one for the least number of operations, find the output operation method

#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<algorithm>#include<vector>#include<math.h>#include<queue>#definell Long Long#defineMet (A, b) memset (A,b,sizeof (a));#defineN 405using namespacestd;Charstr[6][ -]={"FILL (2)","FILL (1)","pour (2,1)","pour (ON)","DROP (1)","DROP (2)"};intn,m,k,f;intVis[n][n];structnode{intx,y,t; Chars[ -];///record path Process s[i] I for step I do str[s[i]] this operation};voidDfs () {Queue<node>Q;    Node q,p; q.x=0; Q.y=0; Q.T=0; strcpy (Q.s,"0");///Remember to initializevis[0][0]=1;    Q.push (q);  while(Q.size ()) {p=Q.front ();        Q.pop (); if(P.x==k | | p.y==k) {f=1; printf ("%d\n", P.T);  for(intI=1; i<=p.t; i++) printf ("%s\n", str[p.s[i]-'0']);///based on the output path stored by the P.S             Break; }         for(intI=0; i<6; i++)///every time there are six kinds of operation can be done, meet the requirements of the implementation{q.t=p.t+1; q.x=0; Q.y=0;            strcpy (Q.S,P.S); Q.S[Q.T]=i+'0'; if(i==0&&!p.y)///The Second Cup is empty, filled{q.x=p.x; Q.y=m; }            Else if(i==1&&!p.x)///The first cup is empty, filled{q.x=N; Q.y=p.y; }            Else if(i==2&& p.x<n&&p.y)///The first cup is dissatisfied, the second Cup is not empty, the water of the second Cup is poured into the first cup            {                if(p.x+p.y<=N) {q.x=p.x+p.y; Q.y=0; }                Else{q.x=N; Q.y=p.x+p.y-N; }            }            Else if(i==3&& p.y<m && p.x)///Second Cup dissatisfaction, the first cup is not empty, the first cup of water into a second Cup            {                if(p.x+p.y<=m) {q.x=0; Q.y=p.x+p.y; }                Else{q.y=m; q.x=p.x+p.y-m; }            }            Else if(i==4&& p.x)///the first cup is not empty, pour it out{q.x=0; Q.y=p.y; }            Else if(i==5&& p.y)///the second Cup is not empty, pour it out{q.y=0; q.x=p.x; }            if(!vis[q.x][q.y])///whether or not there has been, to prevent recurrence, the cycle of death{VIS[Q.X][Q.Y]=1;            Q.push (q); }        }    }    return ;}intMain () { while(SCANF (" %d%d%d", &n,&m,&k)! =EOF) {Met (Vis,0); F=0;        DFS (); if(f==0) printf ("impossible\n"); }    return 0;}

(POJ) 3414 Pots (wide search of output path)

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.