Pots(POJ--3414

来源:互联网 发布:sql视图添加数据 编辑:程序博客网 时间:2024/06/06 17:53

Description

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

  1. FILL(i)        fill the pot i (1 ≤ ≤ 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 is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).

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

Input

On the first and only line are the numbers AB, and C. These are all integers in the range from 1 to 100 and 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 are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

题意:有两个容器1和2,体积分别是A和B,操作有3种:“FILL(i)”即倒满i号容器;“DROP(i)”即将i号容器里的水全部倒出;“POUR(i,j)”即将i号容器的水倒到j号容器里,如果j号容器的水已经满了则i号容器里会留有一些水,否则i号容器会为空。求用这两个容器装出体积为C的水,如果能则输出最少步数和每步的操作;如果不能则输出“impossible”。

Sample Input

3 5 4

Sample Output

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

#include <cstdio>#include <cstring>#include <algorithm>#include <iostream>#include <queue>#define MAX 0x3f3f3f3fusing namespace std;struct node{    int A,B;                   //记录此时两个容器内水的体积    int method;           //记录该步的操作    int steps;               //记录步数    node *next;           //链接一系列操作的指针} q[1200000];bool flag[110][110];  //标记已经执行过的操作char output[6][20]= {"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};void pri(node key)      //输出函数{    if(key.next!=NULL)    //第0步的next才为空但这一步并不用操作也不用输出        pri(*(key.next));    if(key.steps!=0)           //所以要加一个判断,如果是第0步则不输出    printf("%s\n",output[key.method]);}void BFS(int a,int b,int c){    int s=0,e=1;    q[0].A=0;                //两个容器的初始状态    q[0].B=0;    q[0].steps=0;    q[0].next=NULL;    while(s<e)    {        if(q[s].A==c||q[s].B==c)    //如果符合要求了就输出步数和操作        {            printf("%d\n",q[s].steps);            pri(q[s]);            return ;        }        if(!flag[a][q[s].B])            //将容器1倒满水        {            flag[a][q[s].B]=true;            q[e].A=a;            q[e].B=q[s].B;            q[e].steps=q[s].steps+1;            q[e].method=0;            q[e++].next=&q[s];        }        if(!flag[q[s].A][b])            //将容器2倒满水        {            flag[q[s].A][b]=true;            q[e].A=q[s].A;            q[e].B=b;            q[e].steps=q[s].steps+1;            q[e].method=1;            q[e++].next=&q[s];        }        if(!flag[0][q[s].B])            //使容器1为空        {            flag[0][q[s].B]=true;            q[e].A=0;            q[e].B=q[s].B;            q[e].steps=q[s].steps+1;            q[e].method=2;            q[e++].next=&q[s];        }        if(!flag[q[s].A][0])            //使容器2为空        {            flag[q[s].A][0]=true;            q[e].A=q[s].A;            q[e].B=0;            q[e].steps=q[s].steps+1;            q[e].method=3;            q[e++].next=&q[s];        }        if(q[s].A+q[s].B>b)        //将容器1里的水倒入容器2        {            if(!flag[q[s].A-b+q[s].B][b])            {                flag[q[s].A-b+q[s].B][b]=true;                q[e].A=q[s].A-b+q[s].B;                q[e].B=b;                q[e].steps=q[s].steps+1;                q[e].method=4;                q[e++].next=&q[s];            }        }        else        {            if(!flag[0][q[s].A+q[s].B])            {                flag[0][q[s].A+q[s].B]=true;                q[e].A=0;                q[e].B=q[s].A+q[s].B;                q[e].steps=q[s].steps+1;                q[e].method=4;                q[e++].next=&q[s];            }        }        if(q[s].A+q[s].B>a)             //将容器2里的水倒入容器1        {            if(!flag[a][q[s].A-a+q[s].B])            {                flag[a][q[s].A-a+q[s].B]=true;                q[e].A=a;                q[e].B=q[s].A-a+q[s].B;                q[e].steps=q[s].steps+1;                q[e].method=5;                q[e++].next=&q[s];            }        }        else        {            if(!flag[q[s].A+q[s].B][0])            {                flag[q[s].A+q[s].B][0]=true;                q[e].A=q[s].A+q[s].B;                q[e].B=0;                q[e].steps=q[s].steps+1;                q[e].method=5;                q[e++].next=&q[s];            }        }        s++;    }    printf("impossible\n");}int main(){    //freopen("lalala.text","r",stdin);    int a,b,c;    while(~scanf("%d %d %d",&a,&b,&c))    {        memset(flag,0,sizeof(flag));        flag[0][0]=true;        BFS(a,b,c);    }    return 0;}<strong></strong>


0 0