poj 3414 Pots

题目:

有二个水壶,对水壶有三种操作:

1)FILL(i),将i水壶的水填满;

2)DROP(i),将水壶i中的水全部倒掉;

3)POUR(i,j)将水壶i中的水倒到水壶j中,若水壶 j 满了,则 i 剩下的就不倒了,问进行多少步操作,并且怎么操作,输出操作的步骤,两个水壶中的水可以达到C这个水量。如果不可能则输出impossible。初始时两个水壶是空的,没有水。

两个水壶三种操作,所以共六种操作,用广搜来做,需要注意的是记录路径,记录路径时结构体中需要多定义一个指针指向前一个结构体,目的是为了得到这个结构体中的flag即操作。另外这里定义了一个中间变量 t数组,必须得是数组,不然你的指针地址就没得意义了。

#include <iostream>
#include <cstdio>
#include <queue>
#include <stack>
#include <cstring>
using namespace std;
const int Max = 1e3+10;
int vis[Max][Max];
struct cup{
    int x,y;
    int flag;
    int step;
    cup *pre;
};
int a,b,e,ans;
queue<cup> Q;
stack<int> R;
void bfs()
{
    cup c;
    c.x = 0;
    c.y = 0;
    c.step = 1;
    c.flag = -1;
    c.pre = NULL;
    vis[0][0]=1;
    Q.push(c);
    cup t[Max];
    int cnt  = -1;
    while(!Q.empty())
    {
        cnt++;
        t[cnt] = Q.front();Q.pop();
        for(int i=1;i<=6;i++)
        {
            switch(i)
            {
                case 1:         //fill a
                    c.x = a;
                    c.y = t[cnt].y;
                    c.flag = 1;
                    break;
                case 2:         //fill b
                    c.x = t[cnt].x;
                    c.y = b;
                    c.flag = 2;
                    break;
                case 3:     //drop a
                    c.x = 0;
                    c.y = t[cnt].y;
                    c.flag = 3;
                    break;
                case 4:         //drop b
                    c.x = t[cnt].x;
                    c.y = 0;
                    c.flag = 4;
                    break;
                case 5:         //pour a 2 b
                    if(t[cnt].x > b-t[cnt].y)
                    {
                        c.x = t[cnt].x-(b-t[cnt].y);
                        c.y = b;
                    }else{
                        c.x = 0;
                        c.y = t[cnt].x + t[cnt].y;
                    }
                    c.flag = 5;
                    break;
                case 6:     //pour b 2 a
                    if(t[cnt].y > (a-t[cnt].x))
                    {
                        c.x = a;
                        c.y = t[cnt].y - (a-t[cnt].x);
                    }else{
                        c.x=t[cnt].x+t[cnt].y;
                        c.y = 0;
                    }
                    c.flag = 6;
                    break;
            }
            c.step = t[cnt].step+1;
            c.pre = &t[cnt];
            if(vis[c.x][c.y]) continue;
            vis[c.x][c.y] = 1;
            if(c.x==e||c.y==e)
            {
                ans = t[cnt].step;
                while(c.pre)
                {
                    R.push(c.flag);
                    c = *c.pre;
                }
                return ;
            }
            Q.push(c);
        }
    }
}
void print()
{
    while(!R.empty())
    {
        int t = R.top();R.pop();
        switch(t)
        {
            case 1: cout<<"FILL(1)"<<endl;break;
            case 2: cout<<"FILL(2)"<<endl;break;
            case 3: cout<<"DROP(1)"<<endl;break;
            case 4: cout<<"DROP(2)"<<endl;break;
            case 5: cout<<"POUR(1,2)"<<endl;break;
            case 6: cout<<"POUR(2,1)"<<endl;break;
        }
    }
}
int main()
{
    memset(vis,0,sizeof(vis));
    cin>>a>>b>>e;
    bfs();
    if(ans==0) cout<<"impossible"<<endl;
    else {
        cout<<ans<<endl;
        print();
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值