POJ 3414 Pots (bfs+ recursive printing)

Source: Internet
Author: User

Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10255 Accepted: 4333 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)


Test instructions: Give you two known volumes of cups A, B, and a C, for a minimum number of steps to derive c capacity of water. Fill (i) Fill I DROP (i): pour light I pour (i,j): pour i to J
The following: BFS, a total of 6 cases: Fill (a), fill (b), drop (a), drop (b), pour (a), pour (b,a). Among them, pour (i,j) is divided into two kinds of situation and pour full.
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <string > #include <algorithm> #include <cstdlib> #include <set> #include <queue> #include <stack > #include <vector> #include <map> #define N 100010#define Mod 10000007#define Lson l,mid,idx<<1#  Define Rson mid+1,r,idx<<1|1#define LC idx<<1#define RC Idx<<1|1const Double EPS = 1e-11;const double PI = ACOs ( -1.0); const double E = 2.718281828;typedef long long ll;const int INF = 1000010;using namespace Std;int n,m,x;s Tring s[3]= {"Fill", "DROP", "pour"};bool vis[110][110];struct node{int num,nex;///num==:1 (fill), 2 (Drop), 3 (pour) int t;///full, guang bottle int x1,x2;///num=3, by x1->x2 int a,b;///a,b State int step;///Steps} mp[1010];queue<node>que;in    T Fro,nex;bool BFs () {while (Que.size ()) Que.pop ();    memset (vis,0,sizeof Vis);    Node A;    Fro=0,nex=1; MP[FRO]. A=0,MP[FRO].    b=0;    Mp[fro].nex=-1; Mp[fro].step=0;    Que.push (Mp[fro]);    Vis[0][0]=1;        while (Que.size ()) {A=que.front ();        Que.pop (); if (a.a==x| |            a.b==x) {printf ("%d\n", a.step);        return 1;        } mp[nex].nex=-1;            if (!vis[n][a.b])///Pour full a {mp[nex].nex=fro; Mp[nex]. A=n,mp[nex].            B=A.B;            Mp[nex].num=1;            Mp[nex].t=1;            mp[nex].step=a.step+1;            Que.push (Mp[nex]);            Vis[n][a.b]=1;        nex++;            } if (!vis[a.a][m])///pour full B {mp[nex].nex=fro; Mp[nex]. A=a.a,mp[nex].            B=m;            Mp[nex].num=1;            mp[nex].t=2;            mp[nex].step=a.step+1;            Que.push (Mp[nex]);            Vis[a.a][m]=1;        nex++;            } if (!vis[0][a.b])///pour light a {mp[nex].nex=fro; Mp[nex]. A=0,mp[nex].            B=A.B;            mp[nex].num=2;            Mp[nex].t=1;            mp[nex].step=a.step+1;           Que.push (Mp[nex]); Vis[0][a.b]=1;        nex++;            } if (!vis[a.a][0])///pour light B {mp[nex].nex=fro; Mp[nex]. A=a.a,mp[nex].            b=0;            mp[nex].num=2;            mp[nex].t=2;            mp[nex].step=a.step+1;            Que.push (Mp[nex]);            Vis[a.a][m]=1;        nex++; } if (a.a>0)///a->b {int t=m-a.            B if (a.a>t)//{Mp[nex].                A=a.a-t; Mp[nex].            B=m; } else//{Mp[nex].                a=0; Mp[nex].            B=A.A+A.B; } if (!vis[mp[nex]. A][mp[nex].                B]) {mp[nex].num=3;                mp[nex].x1=1,mp[nex].x2=2;                Mp[nex].nex=fro;                mp[nex].step=a.step+1;                Que.push (Mp[nex]); Vis[mp[nex]. A][mp[nex].                B]=1;            nex++; }} if (a.b>0)///b->a {int t=n-a.            A if (a.b&GT;T)//{Mp[nex].                A=n; Mp[nex].            B=a.b-t; } else//{Mp[nex].                A=A.A+A.B; Mp[nex].            b=0; } if (!vis[mp[nex]. A][mp[nex].                B]) {mp[nex].num=3;                Mp[nex].x1=2,mp[nex].x2=1;                Mp[nex].nex=fro;                mp[nex].step=a.step+1;                Que.push (Mp[nex]); Vis[mp[nex]. A][mp[nex].                B]=1;            nex++;    }} fro++; } return 0;}        void print (int x) {if (mp[x].nex!=-1) {print (Mp[x].nex);        if (mp[x].num==1) printf ("FILL (%d) \ n", mp[x].t);        else if (mp[x].num==2) printf ("DROP (%d) \ n", mp[x].t);    else printf ("pour (%d,%d) \ n", mp[x].x1,mp[x].x2); }}int Main () {while (cin>>n>>m>>x) {if (!bfs ()) cout<< "Impossible" <<e        Ndl else {print (fro);       }} return 0;} 


POJ 3414 Pots (bfs+ recursive printing)

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.