Pots

本文介绍了一个经典的水罐问题,通过编程求解如何使用两个不同容量的水罐得到指定数量的水。利用队列和字符串操作,程序能够找到达到目标水量所需的最短操作序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 jis 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 operationsK. 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)

这道题的意思大家都能理解,就是给你三个空水杯,看你怎么操作把第三个水杯装满。困扰了我很久的这道题,终于被我解决了,虽然程序长了一点,但是我还是蛮开心能解决它的。

利用string的增添字符方便,用字符去代表操作,然后最后遍历字符,将操作指令输出。

#include<stdio.h>
#include<string>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
    int a,b,n;
    string s;
};
int a,b,c;
int book[110][110];
void cn()
{

    queue<node>Q;
    node st,en;
    st.a=a,st.b=0,st.n=1,st.s="1";
    Q.push(st);
    st.a=0,st.b=b,st.s="2";
    Q.push(st);
    book[a][0]=book[0][b]=1;
    while(!Q.empty())
    {
        en=Q.front();
        Q.pop();
        if(en.a==c||en.b==c)
        {
            printf("%d\n",en.n);
            for(int i=0; i<en.n; i++)
            {
                if(en.s[i]=='1')
                    printf("FILL(1)\n");
                else if(en.s[i]=='2')
                    printf("FILL(2)\n");
                else if(en.s[i]=='3')
                    printf("POUR(1,2)\n");
                else if(en.s[i]=='4')
                    printf("POUR(2,1)\n");
                else if(en.s[i]=='5')
                    printf("DROP(1)\n");
                else if(en.s[i]=='6')
                    printf("DROP(2)\n");
            }
            return;
        }
        for(int i=1; i<=6; i++)
        {
            if(i==1)
            {
                if(en.a!=a&&book[a][en.b]==0)
                {
                    st.a=a;
                    st.b=en.b;
                    st.s=en.s;
                    st.s+='1';
                    st.n=en.n+1;
                    book[a][en.b]=1;
                    Q.push(st);
                }
            }
            else if(i==2)
            {
                if(en.b!=b&&book[en.a][b]==0)
                {
                    st.a=en.a;
                    st.b=b;
                    st.s=en.s;
                    st.s+='2';
                    st.n=en.n+1;
                    book[en.a][b]=1;
                    Q.push(st);
                }
            }
            else if(i==3)
            {
                if(en.a!=0&&en.b!=b)
                {
                    if(en.a>(b-en.b))
                    {
                        st.a=en.a-(b-en.b);
                        st.b=b;
                        if(book[st.a][st.b]==0)
                        {
                            st.n=en.n+1;
                            st.s=en.s;
                            st.s+='3';
                            book[st.a][st.b]=1;
                            Q.push(st);
                        }
                    }
                    else
                    {
                        st.a=0;
                        st.b=en.b+en.a;
                        if(book[st.a][st.b]==0)
                        {
                            st.n=en.n+1;
                            st.s=en.s;
                            st.s+='3';
                            book[st.a][st.b]=1;
                            Q.push(st);
                        }
                    }
                }
            }
            else if(i==4)
            {
                if(en.b!=0&&en.a!=a)
                {
                    if(en.b>(a-en.a))
                    {
                        st.a=a;
                        st.b=en.b-(a-en.a);
                        if(book[st.a][st.b]==0)
                        {
                            st.n=en.n+1;
                            st.s=en.s;
                            st.s+='4';
                            book[st.a][st.b]=1;
                            Q.push(st);
                        }
                    }
                    else
                    {
                        st.a=en.a+en.b;
                        st.b=0;
                        if(book[st.a][st.b]==0)
                        {
                            st.n=en.n+1;
                            st.s=en.s;
                            st.s+='4';
                            book[st.a][st.b]=1;
                            Q.push(st);
                        }
                    }
                }
            }
            else if(i==5)
            {
                if(en.a!=0&&book[0][en.b]==0)
                {
                    st.a=0;
                    st.b=en.b;
                    st.n=en.n+1;
                    st.s=en.s;
                    st.s+='5';
                    book[0][en.b]=1;
                    Q.push(st);
                }
            }
            else if(i==6)
            {
                if(en.b!=0&&book[en.a][0]==0)
                {
                    st.a=en.a;
                    st.b=0;
                    st.n=en.n+1;
                    st.s=en.s;
                    st.s+='6';
                    book[en.a][0]=1;
                    Q.push(st);
                }
            }
        }
    }
    printf("impossible\n");
}
int main()
{
    while(~scanf("%d%d%d",&a,&b,&c)&&a+b+c)
    {
        memset(book,0,sizeof(book));
        if(c>max(a,b))
            printf("impossible\n");
        else
           cn();
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值