POJ 3414 Pots(BFS记录路径)

Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10085 Accepted: 4245 Special Judge

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’.

Sample Input

3 5 4

Sample Output

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
 
原来在学校OJ上做过的一道BFS水题,只输出最少的操作补数,不用输出每一步的操作,后来在POJ上看到这道题,思考了一段时间也没想到如何记录下来每一步的操作,后来无意在网上看见了一大牛的做法,挺好理解的。。。
题意为给你a,b两个容器的容量,六种操作方法,问最少多少次可以使其中一个容器里的水达到体积v,如若不能,输出impossible
六种操作为 1 倒满a;2 倒满b ;3 把a倒给b(会出现两种情况 分别是 b满了a还有剩余或倒空  和  b没满,a已经倒空);4  把b倒给a(同3); 5 倒空a ; 6 倒空b 。。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int N =101;
using namespace std;
struct node
{
    int x,y;
} q[N*N*N];
int b[N][N];
int ans[N][N][N];  //记录每一步的操作
int n,m,v;

void prints(int s)
{
    switch(s)
    {
    case 0:
        printf("FILL(1)\n");
        break;
    case 1:
        printf("FILL(2)\n");
        break;
    case 2:
        printf("POUR(1,2)\n");
        break;
    case 3:
        printf("POUR(2,1)\n");
        break;
    case 4:
        printf("DROP(1)\n");
        break;
    case 5:
        printf("DROP(2)\n");
        break;
    }
}
void bfs()
{
    int k=0,l=0;
    memset(b,-1,sizeof(b));  //记录操作步数
    b[0][0]=0;
    struct node t,r;
    int i;
    t.x=0;
    t.y=0;
    q[l++]=t;
    while(k<l)
    {
        t=q[k++];
        if(t.x==v || t.y==v)
        {
            printf("%d\n",b[t.x][t.y]);
            for(int i=0; i<b[t.x][t.y]; i++)
            {
                prints(ans[t.x][t.y][i]);
            }
            return ;

        }
        for(i=0; i<6; i++)
        {


            if(i==0)          //倒满a
            {
                r.x=n;
                r.y=t.y;
            }
            if(i==1)    //倒满b
            {
                r.x=t.x;
                r.y=m;
            }
            if(i==2)    //把a倒给b
            {
                r.y = t.x + t.y;
                if(r.y>=m)
                {
                    r.x=r.y-m;
                    r.y=m;

                }
                else
                    r.x=0;
            }
            if(i==3)    //把b倒给a
            {
                r.x=t.y+t.x;
                if(r.x>=n)
                {
                    r.y=r.x-n;
                    r.x=n;

                }
                else
                    r.y=0;
            }
            if(i==4)  //倒空a
            {
                r.x=0;
                r.y=t.y;
            }
            if (i==5)    //倒空b
            {
                r.x=t.x;
                r.y=0;
            }

            if(b[r.x][r.y]==-1)
            {
                b[r.x][r.y]=b[t.x][t.y]+1;
                for(int j=0; j<b[t.x][t.y]; j++)   //更新倒水的步数
                {
                    ans[r.x][r.y][j]=ans[t.x][t.y][j];
                }
                ans[r.x][r.y][b[t.x][t.y]]=i;       //新的操作
                q[l++]=r;
            }
        }
    }
    printf("impossible\n");
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&v)!=EOF)
    {
        bfs();
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值