1. 程式人生 > >Pots(POJ--3414

Pots(POJ--3414

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 A

B, 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

6
FILL(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 0x3f3f3f3f
using 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>