poj3414 Pots (BFS)

Source: Internet
Author: User

Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12198 Accepted: 5147 Special Judge

Description

You are given the pots of the volume of A and B liters respectively. The following operations can be performed:

    1. Fill (i) Fill the pot i (1≤ i ≤ 2) from the tap;
    2. DROP (i) empty the pot I to the drain;
    3. Pour (i,j) pour from pot i to pot J; After this operation either the pot J was full (and there may be some water left in the pot I), or the PO T I was empty (and all its contents has been moved to the pot J).

Write a program to find the shortest possible sequence of these operations that'll yield exactly C liters of Water in one of the pots.

Input

On the first and only line is the numbers A, B, and C. These is all integers in the range from 1 to + C≤max (A,B).

Output

The 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 Input

3 5 4

Sample Output

6FILL (2) pour (2,1) DROP (1) pour (2,1) FILL (2) pour (2,1)

Source

Northeastern Europe 2002, Western subregion

Empty cups pour water problem, this question is relatively simple, only two cups, the initial time is empty, to pour out the specified amount of water there are three kinds of operation:

1. Fill (i) Fill the first cup (i=0,1)

2. Drop (i) empty the first cup

3, pour (i,j) pour the water of I into J, until J full or I pour out

My idea: Put A->b,b->c, .... A total of 6 kinds of water-pouring method one by one, and each is the same discussion method,

Although very good to do, but for me such an entry-level sailor can not write too good-looking code, so ....

Take a look at it and see the ==| of others online |

#include <stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<queue>using namespacestd;Const intmaxn= -;BOOLVIS[MAXN][MAXN];intFA[MAXN][MAXN];intOP[MAXN][MAXN];intSA,SB,SC;structnode{intA,b,step;};Charans[6][ -]={"FILL (1)","FILL (2)","DROP (1)","DROP (2)","pour (ON)","pour (2,1)"};voidPrintintAintb) {     if(op[a][b]==-1)     return; Else{print (Fa[a][b]/ +, fa[a][b]% +); printf ("%s\n", Ans[op[a][b]]); }}BOOLBFS () {node u,v; U.A=0, u.b=0; U.step=0; Queue<node>Q;   Q.push (U); Memset (OP,0,sizeof(OP)); memset (Vis,false,sizeof(VIS)); memset (FA,0,sizeof(FA)); op[0][0]=-1; fa[0][0]=0; vis[0][0]=true;  while(!Q.empty ()) {u=Q.front ();       Q.pop (); if(u.a==sc| | u.b==SC) {printf ("%d\n", U.step);          Print (U.A,U.B); return true; } v=u; if(u.a!=sa) {V.A=sa; if(!VIS[V.A][V.B]) {V.step++;              Q.push (v); OP[V.A][V.B]=0; VIS[V.A][V.B]=true; FA[V.A][V.B]=u.a* ++u.b; }}v=T; if(U.B!=SB) {v.b=SB; if(!VIS[V.A][V.B]) {V.step=u.step+1;                Q.push (v); OP[V.A][V.B]=1; VIS[V.A][V.B]=1; FA[V.A][V.B]=u.a* ++u.b; }}v=u; if(U.A) {v.a=0; if(!VIS[V.A][V.B]) {V.step++;                Q.push (v); OP[V.A][V.B]=2; VIS[V.A][V.B]=true; FA[V.A][V.B]=u.a* ++u.b; }}v=u; if(U.B) {v.b=0; if(!VIS[V.A][V.B]) {V.step++;                Q.push (v); OP[V.A][V.B]=3; VIS[V.A][V.B]=true; FA[V.A][V.B]=u.a* ++u.b; }}v=T; if(u.a) {if(V.A&GT;=SB-U.B&AMP;&AMP;U.B!=SB) {v.a-= (sb-u.b); v.b=SB;} Else if(V.A&LT;SB-U.B) {v.a=0; v.b+=u.a;} if(!VIS[V.A][V.B]) {V.step++;                Q.push (v); OP[V.A][V.B]=4; VIS[V.A][V.B]=true; FA[V.A][V.B]=u.a* ++u.b; }}v=u; if(u.b) {if(V.B&GT;=SA-U.A&AMP;&AMP;U.A!=SA) {v.b-= (SA-U.A); v.a=sa;} Else if(V.B&LT;SA-U.A) {v.b=0; v.a+=u.b;} if(!VIS[V.A][V.B]) {V.step++;                Q.push (v); OP[V.A][V.B]=5; VIS[V.A][V.B]=true; FA[V.A][V.B]=u.a* ++u.b; }        }   }   return false;}intMain () { while(SCANF ("%d%d%d", &AMP;SA,&AMP;SB,&AMP;SC)! =EOF) {       BOOLflag=BFS (); if(!flag) printf ("impossible\n"); }    return 0;}

poj3414 Pots (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.