ZOJ-1005-Jugs

本文解析了一道源自电影《虎胆龙威3》的数学谜题,即如何使用3加仑和5加仑的水壶准确测量出4加仑的水。通过使用回溯算法,作者详细介绍了实现这一目标的具体步骤和代码实现。

    这道题我做的有点晕,虽然第一次提交是Compile Error,但改掉了与std中同名的函数再Submit了一下就AC了。

    但是这道题我做的不好,整体的程序结构还算清晰,与上一题1004也很像,当然比较而言在这道里:回溯更加复杂一些,多了几个其他干别的的函数,plus还有一个自己设计的类。

    但是在solve();函数里,对于每项动作的判断太复杂了,一眼根本看不出来,要不是VC里有watch可看,就是逻辑判断写错也很难检查出来。我总感觉有一个更加简洁明了的方法在不远处存在,我一直在向它致意,可惜它不理我。。。

    总得过程还是很清楚的,就不多写了有一点需要交待的是:我用了一个step[]数组来记录动作,因为可能的动作一共有6种,于是我把step[]的类型定为int,具体指带看下void PrintStep(int target[],int tp)里的switch就一清二楚了。另外利用rstep[]来存储最短成功步骤,最后输出

总结:1.拿到题后思路清晰,这种看似纷繁的题目最适合用回溯不断试探。

        2.不足是判断逻辑过于复杂,稍有不细心就会产生大错,添加的逻辑判断我基本都是设了断点然后一步一步跟踪才检查出来的。

 

Jugs
Time limit: 1 Seconds   Memory limit: 32768K   Special Judge
Total Submit: 2058   Accepted Submit: 960  

In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle.

You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.

A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps are

fill A
fill B
empty A
empty B
pour A B
pour B A
success

where "pour A B" means "pour the contents of jug A into jug B", and "success" means that the goal has been accomplished.

You may assume that the input you are given does have a solution.

Input

Input to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another.

Output

Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line "success". Output lines start in column 1 and there should be no empty lines nor any trailing spaces.

 

Sample Input
3 5 4
5 7 3

Sample Output
fill B
pour B A
empty A
pour B A
fill B
pour B A
success
fill A
pour A B
fill A
pour A B
empty B
pour A B
success
Problem Source: Zhejiang University Local Contest 2001
#include <iostream>
using namespace std;

int step[1000],sp;//历史步骤及其当前步骤指针
int rstep[1000],rp;
int N;//目标水量
int temp;
int bpa,bpb;//存储执行pour之前的A,B水量
int flag,minstep;

class CupClass
{
public:
    int cx;//最大容量
    int now;//当前水量
    int name;//jug名字(A-0 or B-1)
    
    int fill()//装满,返回装满前水量
    {
        temp=now;
        now=cx;
        if(name==0)
        {
            step[sp++]=1;
        }
        else
        {
            step[sp++]=2;
        }
        return temp;
    };
    
    int empty()//清空,返回装满前水量
    {
        temp=now;
        now=0;        
        if(name==0)
        {
            step[sp++]=3;
        }
        else
        {
            step[sp++]=4;
        }
        return temp;
    };
};

////////////////////公共函数声明////////////////////////////
void PrintStep(int target[],int tp);//根据step内容,输出步骤
void pour(CupClass &x,CupClass &y);//从x向y倒水
void solve();//递归&回溯解决,输出至rstep
////////////////////////////////////////////////////////////

CupClass a,b;//建立a,b两个水杯(罐)全局对象

void PrintStep(int target[],int tp)
{
    int i;
    for(i=0;i<tp;i++)
    {
        switch(target[i])
        {
        case 1: cout<<"fill A"<<endl;break;
        case 2: cout<<"fill B"<<endl;break;
        case 3: cout<<"empty A"<<endl;break;
        case 4: cout<<"empty B"<<endl;break;
        case 5: cout<<"pour A B"<<endl;break;
        case 6: cout<<"pour B A"<<endl;break;
        }
    }
}

void pour(CupClass &x,CupClass &y)
{
    int diff;
    bpa=x.now;
    bpb=y.now;

    diff=y.cx-y.now;
    if(x.now<=diff)
    {
        
        y.now+=x.now;
        x.now=0;
    }
    else
    {
        x.now-=diff;
        y.now=y.cx;
    }

    if(x.name==0 && y.name==1)
    {
        step[sp++]=5;
    }
    else
    {
        step[sp++]=6;
    }
}

void solve()
{
    int t,i;    
    if(!flag)
    {    
        if(b.now==N)
        {        
            if(sp<minstep && minstep!=0)
            {
                if(sp==0 || sp==1 || sp==2)
                    flag=1;
                minstep=sp;
                rp=sp;
                for(i=0;i<rp;i++)
                {
                    rstep[i]=step[i];
                }
            }
        }
    }
    
    if(!flag)
    {
        
        if(a.now<a.cx && (step[sp-1]!=3) && 
            (b.now!=b.cx) && !(a.now!=0 && b.now==0) && b.now!=b.cx)    
        {/////填满a
            t=a.fill();
            solve();
            sp--;
            a.now=t;
        }
        
        if(b.now<b.cx && (step[sp-1]!=4) && (a.now!=a.cx) && 
            !(b.now!=0 && a.now==0) && a.now!=b.cx)
        {//////填满b
            t=b.fill();
            solve();
            sp--;
            b.now=t;
        }        
        
        if(a.now>0 && b.now<b.cx && (step[sp-1]!=6) && 
            (step[sp-1]!=5) && sp!=0)
        {////从a倒给b
            pour(a,b);
            solve();
            sp--;
            a.now=bpa;
            b.now=bpb;
        }
        
        if(b.now>0 && a.now<a.cx && (step[sp-1]!=5) && 
            (step[sp-1]!=6) && sp!=0)
        {////从b倒给a
            pour(b,a);
            solve();
            sp--;
            a.now=bpa;
            b.now=bpb;
        }            
        
        if(a.now>0 && (step[sp-1]!=1) && b.now!=0 && 
            !(a.now==N) && sp!=0)
        {//////清空a
            t=a.empty();
            solve();
            sp--;
            a.now=t;
        }
        
        if(b.now>0 && (step[sp-1]!=2) && a.now!=0 && 
            sp!=0 || (b.now==b.cx && a.now==N))
        {/////清空b
            t=b.empty();
            solve();
            sp--;
            b.now=t;
        }
    }
}

int main()
{    
    while(cin>>a.cx>>b.cx>>N)            
    {    
        a.now=0;b.now=0;
        a.name=0;b.name=1;
        sp=0;flag=0;minstep=99999;
        rp=0;    
        if(N>0)
            solve();
        if(N!=0)
            PrintStep(rstep,rp);
        cout<<"success"<<endl;
    }
        return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值