POJ-3414-Pots(BFS)

Pots

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i) empty the pot i to the drain;
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 A, B, 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)

题意:
分别给你两个杯子的容量 和水的体积,这个体积是要通过那两个杯子来实现,具体的实现方式有六种,分别是:
1、倒满1,而2不动;
2、倒满2,而1不动;
3、清空1,而2不动;
4、清空2,而1不动;
5、从1往2倒水;
6、从2往1倒水。
让1,2杯子的任意一个杯子里的水为C,求最少用多少步。

思路:
BFS+记忆路径

完整代码:

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

int A,B,C;
int v[105][105];    //标记是否位访问过状态
struct node
{
    int prea;       //记录容器A前一个的状态
    int preb;       //记录容器B前一个的状态
    int a;          //A,B为此事状态
    int b;
    int step;       //走的步数
    int operation;  //六种操作
};
node way[105][105]; //记忆路径

void Output(node s)     //输出函数打印路径
{
    if(s.prea==-1 && s.preb==-1)
    {
        return;
    }
    else
    {
        Output(way[s.prea][s.preb]);    //递归,打印操作
        switch(s.operation)             //这六种情况。注意:单词不要打错
        {
            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;
        }
    }
}
void bfs()      //广度优先搜素
{
    //初始化
    node s;
    s.prea=-1;
    s.preb=-1;
    s.a=0;
    s.b=0;
    s.step=0;
    s.operation=-1;

    queue<node>q;
    while(!q.empty()) q.pop();      //记得先清空在存
    q.push(s);
    while(!q.empty())
    {
        s=q.front();
        q.pop();
        way[s.a][s.b]=s;
        if(s.a==C || s.b==C)
        {
            cout<<s.step<<endl;
            Output(s);
            return ;
        }
        v[s.a][s.b]=1;
        for(int i=1;i<=6;i++)           //六种操作情况
        {
            //第一种   给1注满水
            if(i==1 && !v[A][s.b])
            {
                v[A][s.b]=1;
                node nex;
                nex.a=A;
                nex.b=s.b;
                nex.prea=s.a;
                nex.preb=s.b;
                nex.step=s.step+1;
                nex.operation=i;
                q.push(nex);
            }
            //第二种   给2注满水
            if(i==2 && !v[s.a][B])
            {
                v[s.a][B]=1;
                node nex;
                nex.a=s.a;
                nex.b=B;
                nex.prea=s.a;
                nex.preb=s.b;
                nex.step=s.step+1;
                nex.operation=i;
                q.push(nex);
            }
            //第三种   倒掉1中的水
            if(i==3 && !v[0][s.b])
            {
                v[0][s.b]=1;
                node nex;
                nex.a=0;
                nex.b=s.b;
                nex.prea=s.a;
                nex.preb=s.b;
                nex.step=s.step+1;
                nex.operation=i;
                q.push(nex);
            }
            //第四种   倒掉2中的水
            if(i==4 && !v[s.a][0])
            {
                v[s.a][0]=1;
                node nex;
                nex.a=s.a;
                nex.b=0;
                nex.prea=s.a;
                nex.preb=s.b;
                nex.step=s.step+1;
                nex.operation=i;
                q.push(nex);
            }
            //第五种   将1倒入2中
            if(i==5)
            {
                int dx,dy;
                if(s.a+s.b>B)       //满了还有剩余
                {
                    dx=s.a+s.b-B;
                    dy=B;
                }
                else if(s.a+s.b<=B) //没满或者正好
                {
                    dx=0;
                    dy=s.a+s.b;
                }
                if(!v[dx][dy])
                {
                    v[dx][dy]=1;
                    node nex;
                    nex.a=dx;
                    nex.b=dy;
                    nex.prea=s.a;
                    nex.preb=s.b;
                    nex.step=s.step+1;
                    nex.operation=i;
                    q.push(nex);
                }
            }
            //第六种   将2倒入1中
            if(i==6)
            {
                int dx,dy;
                if(s.a+s.b>A)       //满了还有剩余
                {
                    dy=s.a+s.b-A;
                    dx=A;
                }
                else if(s.a+s.b<=A) //没满或者正好
                {
                    dy=0;
                    dx=s.a+s.b;
                }
                if(!v[dx][dy])
                {
                    v[dx][dy]=1;
                    node nex;
                    nex.a=dx;
                    nex.b=dy;
                    nex.prea=s.a;
                    nex.preb=s.b;
                    nex.step=s.step+1;
                    nex.operation=i;
                    q.push(nex);
                }
            }
        }
    }
    cout<<"impossible"<<endl;       //注意不要忘记"impossible"
    return;
}

int main()
{
    while(cin>>A>>B>>C)
    {
        memset(v,0,sizeof(v));
        bfs();
    }
    return 0;
}

原题链接:
https://vjudge.net/problem/POJ-3414
http://poj.org/problem?id=3414

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值