POJ 3414 Pots (bfs+ backtracking)

Source: Internet
Author: User

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



Test Instructions: Enter 3 integers n,m,k, the first two integers represent the capacity of two cups, require two cups to fill, empty, pour into another cup and so on, so that the amount of water in one of the two cups is equal to K, and the output step. idea: The topic is very simple, is the trouble. In particular, the step that part, need to use the back of BFS, search through BFS, record two cups of water, so that can not be repeated. If not, the output impossible


#include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> #include <    Stdlib.h> #include <queue>using namespace std;int n,m,k;int ans;int v[110][110];struct node{int x;    int y;    int z; int cnt;}    A[1000010];void DFS (int kk) {int pt = a[kk].cnt;    if (pt<=0) {return;    } DFS (PT);        if (a[pt].x = = 1) {if (a[pt].y = = 1) {printf ("FILL (1) \ n");        } else {printf ("FILL (2) \ n");        }} else if (a[pt].x = = 2) {if (a[pt].y = = 1) {printf ("DROP (1) \ n");        } else {printf ("DROP (2) \ n");        }} else if (a[pt].x = = 3) {if (a[pt].y = = 1) {printf ("pour" \ n ");        } else {printf ("pour (2,1) \ n");    }}}void BFS () {ans = 1;    queue<node>q;    memset (v,0,sizeof (v));    struct node t,f;    t.x = 0;    T.Y = 0;   t.z = 0; t.cnt = 0;    a[0].x = 0;    A[0].Y = 0;    a[0].cnt = 0;    Q.push (t);    V[T.X][T.Y] = 1;        while (!q.empty ()) {t = Q.front ();        Q.pop ();                for (int i=1, i<=3; i++) {for (int j=1; j<=2; J + +) {f.x = T.x;                F.Y = T.y; if (i = = 1) {if (j = = 1 && f.x!=n) {F.                    x = n;                    } else if (j = = 2 && f.y!=m) {f.y = m;                    }} else if (i = = 2) {if (j = = 1 && f.x!=0)                    {f.x = 0;                    } else if (j = = 2 && f.y!=0) {f.y = 0; }} else if (i = = 3) {if (j = = 1 && (f.x!=0 && f.y!=m)) {if (F.X&GT;=M-F.Y)                            {f.x = f.x-m + f.y;                        F.y = m;                            } else {f.y = f.y + f.x;                        f.x = 0;                        }} else if (j = = 2 && (f.y!=0 && f.x!=n)) {                            if (f.y>=n-f.x) {f.y = f.y-n + f.x;                        f.x = n;                            } else {f.x = f.x + f.y;                        F.Y = 0; }}} if (v[f.x][f.y] = = 0) {f.cnt = an                    S F.z = t.z + 1;                    a[ans].x = i;                    A[ans].y = j;                    a[ans].cnt = t.cnt;                    Q.push (f);                    V[F.X][F.Y] = 1;                        if (f.x = = k | | f.y = = k) {printf ("%d\n", f.z);                        DFS (ANS);                                if (i = = 1) {if (j = = 1) {                            printf ("FILL (1) \ n");                            } else {printf ("FILL (2) \ n");                            }} else if (i = = 2) {                            if (j = = 1) {printf ("DROP (1) \ n"); } else {printf ("DROP (2) \ n ");  }} else if (i = = 3) {if (j                            = = 1) {printf ("pour \ n");                            } else {printf ("pour (2,1) \ n");                    }} return;                } ans++; }}}} printf ("impossible\n");}    int main () {while (scanf ("%d%d%d", &n,&m,&k)!=eof) {BFS (); } return 0;}


Copyright NOTICE: This article is the original blogger article, if you have special needs, please contact Bo Master qq:793977586.

POJ 3414 Pots (bfs+ backtracking)

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.