POJ 3414 Pots

Source: Internet
Author: User

Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 12022 Accepted: 5078 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 potJ was full (and there may be some water left in the pot I), or the pot 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 would yield exactlyC liters of W Ater in one of the pots.

Input

On the first and only line is the numbers A, B, andC. 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 operationsK. 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)


The main topic: there are two cups of the capacity of a, B, ask how many steps, you can let C-liter of water into a cup.

Idea: Ask the shortest steps of course is BFS, but how to mark down its path is difficult, and how a search method.

You can set a string in node to specifically install the current situation, and then map to STR, for how the search is definitely divided into fill,drop,pour three categories



#include <iostream> #include <cstdio> #include <string.h> #include <string> #include <cmath    > #include <queue> #define LL long longusing namespace std;int a,b,c;struct node{int n1,n2,tep; String a;//saves the current operation};bool Vis[100][100];char str[6][10]= {"Fill (1)", "Fill (2)", "pour (2,1)", "pour", "DROP (1)", "    DROP (2) "};//specifically has six implementations of void BFs () {queue<node>q;    while (!q.empty ()) Q.pop ();    Node F1,f2;    int t;    f1.n1=0;    f1.n2=0;    F1.tep=0;    F1.a= ""; Start with an empty string Q.push (F1);        while (!q.empty ()) {F1=q.front ();        Q.pop ();       if (VIS[F1.N1][F1.N2])//paper-cut continue;        else vis[f1.n1][f1.n2]=true; if (f1.n1==c| |            F1.n2==c) {printf ("%d\n", f1.tep);            for (int j=0;j<f1.tep;j++) {printf ("%s\n", str[f1.a[j]-' 0 ");//corresponding to STR}        return;            } for (int i=0;i<4;i++) {f2=f1;            f2.tep++; if (i==0)           {if (f2.n1==0) {f2.n1=a;                    f2.n2=f1.n2;//Note that this must be f2.x=f1.x because it is not possible to affect the next if (), although the beginning of the part of the F2=F1, in the bottom side of the section is not the f2.a=f1.a+ ' 0 ';                Q.push (F2);                    } if (f2.n2==0) {f2.n2=b;                    f2.n1=f1.n1;//remains relatively constant f2.a=f1.a+ ' 1 ';                Q.push (F2);                    }} if (I==1) {if (f2.n1!=0) {f2.n1=0;                    F2.N2=F1.N2;                    f2.a=f1.a+ ' 4 ';                Q.push (F2);                    } if (f2.n2!=0) {f2.n2=0;                    F2.N1=F1.N1;                    f2.a=f1.a+ ' 5 ';                Q.push (F2);          }} if (i==3) {if (f2.n2!=0&&f2.n1!=a) {          T=A-F2.N1;                        if (f2.n2>=t) {f2.n2-=t;                    F2.n1=a;                        } else {f2.n1+=f2.n2;                    f2.n2=0;                    } f2.a+= ' 2 ';                Q.push (F2);                    }} if (i==2) {if (f2.n1!=0&&f2.n2!=b) {                    T=B-F2.N2;                        if (f2.n1>=t) {f2.n1-=t;                    F2.n2=b;                        } else {f2.n2+=f2.n1;                    f2.n1=0;                    } f2.a+= ' 3 ';                Q.push (F2); }}}} printf ("impossible\n");}    int main () {int n,m,i,j,k,s; while (~SCANF ("%d%d%d",&a,&b,&C) {memset (vis,false,sizeof (VIS));    BFS (); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.