POJ 3414 Pots(模拟倒水)

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)

题意:给出两个水杯的容量A B,问怎么操作可以使A B中一个的水量到C,题目保证AB中有一个大于C

    其实也就是一个简单的BFS,因为把顺序写错了,导致自己看了两天都没知道到错误,真的是脑残

     倒水也就有六种情况,分别让他们入队就行了。详解见代码


#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
int maxcup[2],endcup;//被子的容量和最终状态
int vis[110][110];//标记,vis[i][j]就是杯子1的水是i,杯子2的水是j
struct cupwater
{
    int curcup[2],step;//当前水量,步数
    int op[10000];//记录上一个的节点
};
queue<cupwater>Q;
cupwater p,q;
void Fillwater(int i)//往杯子里面倒水
{
    q=p;
    if(q.curcup[i]<maxcup[i])//如果杯子里面的水是满的就不用倒水了
    {
        q.curcup[i]=maxcup[i];
        if(i) q.op[q.step]=2;//记录倒水的杯子,方便输出
        else q.op[q.step]=1;
        q.step=q.step+1;
        if(vis[q.curcup[0]][q.curcup[1]]==0)//一种状态只能出现一次
        {
            Q.push(q);//入队
            vis[q.curcup[0]][q.curcup[1]]=1;//下次在遇到相同状态不处理
        }
    }
}
void Dropwater(int i)//把杯子里面的水倒出来
{
    q=p;
    if(q.curcup[i]>0&&q.curcup[i]<=maxcup[i])//其实只有当杯子一个杯子里面的水满了才会倒出,这样写完全是为了安全起见
    {
        q.curcup[i]=0;
        if(i) q.op[q.step]=4;
        else q.op[q.step]=3;
        q.step=q.step+1;
        if(vis[q.curcup[0]][q.curcup[1]]==0)
        {
            Q.push(q);
            vis[q.curcup[0]][q.curcup[1]]=1;
        }
    }
}
void  Pourwater(int i,int j)//杯子i里面的水倒到j里面
{
    q=p;
    if(q.curcup[i]==0||q.curcup[j]==maxcup[j])//杯子i是空的或者杯子j是满的,直接跳出
        return ;
    if(q.curcup[i]>(maxcup[j]-q.curcup[j]))//杯子i里面的水多用不完的情况
    {
        q.curcup[i]-=(maxcup[j]-q.curcup[j]);
        q.curcup[j]=maxcup[j];
        if(i)
            q.op[q.step]=6;
        else q.op[q.step]=5;
        q.step=q.step+1;
        if(vis[q.curcup[0]][q.curcup[1]]==0)
        {
            Q.push(q);
            vis[q.curcup[0]][q.curcup[1]]=1;
        }
    }
    else if(q.curcup[i]<=(maxcup[j]-q.curcup[j]))//杯子i里面的水不够倒满杯子j
    {
        q.curcup[j]=q.curcup[i]+q.curcup[j];//我把这两行写颠倒了,课害苦我了
        q.curcup[i]=0;
        if(i)
            q.op[q.step]=6;
        else q.op[q.step]=5;
        q.step=q.step+1;
        if(vis[q.curcup[0]][q.curcup[1]]==0)
        {
            Q.push(q);
            vis[q.curcup[0]][q.curcup[1]]=1;
        }
    }
}

int main()
{
    int i,j,f;
    while(~scanf("%d%d%d",&maxcup[0],&maxcup[1],&endcup))
    {
        memset(vis,0,sizeof(vis));
        f=0;
        p.curcup[0]=0;
        p.curcup[1]=0;
        p.step=0;
        Q.push(p);
        while(!Q.empty())
        {
            p=Q.front();
            Q.pop();
            if(p.curcup[0]==endcup||p.curcup[1]==endcup)
            {
                f=1;
                printf("%d\n",p.step);
                for(i=0; i<p.step; i++)
                {
                    if(p.op[i]==1)
                        printf("FILL(1)\n");
                    if(p.op[i]==2)
                        printf("FILL(2)\n");
                    if(p.op[i]==3)
                        printf("DROP(1)\n");
                    if(p.op[i]==4)
                        printf("DROP(2)\n");
                    if(p.op[i]==5)
                        printf("POUR(1,2)\n");
                    if(p.op[i]==6)
                        printf("POUR(2,1)\n");
                }
                break;
            }
            //六种情况分别考虑,能入队就入队
            Fillwater(0);
            Fillwater(1);
            Dropwater(0);
            Dropwater(1);
            Pourwater(0,1);
            Pourwater(1,0);
            vis[p.curcup[0]][p.curcup[1]]=1;
        }
        if(!f) printf("impossible\n");
        while(!Q.empty())
            Q.pop();
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值