BFS/poj 3414 pots

本文介绍了一种使用广度优先搜索(BFS)解决水罐问题的算法实现。通过定义结构体存储水罐的状态,并利用队列进行状态扩展,最终找到使其中一个水罐水量达到目标值的解决方案。代码中详细展示了每一步操作的具体实现。
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
struct point
{
    int a,b;
    int step;
    int way[110];
};
int aa,bb,cc;
int v[110][110];
void bfs()
{
    memset(v,0,sizeof(v));
    queue<point>que;
    point now,next;
    now.a=0;
    now.b=0;
    now.step=0;
    v[0][0]=true;
    que.push(now);
    while (!que.empty())
    {
        now=que.front();
        que.pop();
        if (now.a==cc || now.b==cc)
        {
            printf("%d\n",now.step);
            for (int i=1;i<=now.step;i++)
            {
                if (now.way[i]==1) printf("FILL(1)\n");
                if (now.way[i]==2) printf("FILL(2)\n");
                if (now.way[i]==3) printf("DROP(1)\n");
                if (now.way[i]==4) printf("DROP(2)\n");
                if (now.way[i]==5) printf("POUR(1,2)\n");
                if (now.way[i]==6) printf("POUR(2,1)\n");
            }
            return ;
        }
        //FILL A 111111111
        if (now.a!=aa)
        {
            next=now;
            next.a=aa;
            next.b=now.b;
            if (!v[next.a][next.b])
            {
                v[next.a][next.b]=1;
                next.step=now.step+1;
                next.way[next.step]=1;
                que.push(next);
            }
        }

        //FILL B 2222222222
        if (now.b!=bb)
        {
            next=now;
            next.a=now.a;
            next.b=bb;
            if (!v[next.a][next.b])
            {
                v[next.a][next.b]=1;
                next.step=now.step+1;
                next.way[next.step]=2;
                que.push(next);
            }
        }

        //DROP A 3333333333
        if (now.a!=0)
        {
            next=now;
            next.a=0;
            next.b=now.b;
            if (!v[next.a][next.b])
            {
                v[next.a][next.b]=1;
                next.step=now.step+1;
                next.way[next.step]=3;
                que.push(next);
            }
        }

        //DROP B 444444444444
        if (now.b!=0)
        {
            next=now;
            next.a=now.a;
            next.b=0;
            if (!v[next.a][next.b])
            {
                v[next.a][next.b]=1;
                next.step=now.step+1;
                next.way[next.step]=4;
                que.push(next);
            }
        }

        //pour(1,2)  5555555555555
        if (now.a!=0)
        {
            if (now.a>=bb-now.b)  //a 倒满 b
            {
                next=now;
                next.a=now.a-(bb-now.b);
                next.b=bb;
                if (!v[next.a][next.b])
                {
                    v[next.a][next.b]=1;
                    next.step=now.step+1;
                    next.way[next.step]=5;
                    que.push(next);
                }
            }else if (now.a<bb-now.b)   //a 空了  没倒满 b
            {
                next=now;
                next.a=0;
                next.b=now.b+now.a;
                if (!v[next.a][next.b])
                {
                    v[next.a][next.b]=1;
                    next.step=now.step+1;
                    next.way[next.step]=5;
                    que.push(next);
                }
            }
        }

        //POUR(2,1)  6666666666666
        if (now.b!=0)
        {
            if (now.b>=aa-now.a)
            {
                next=now;
                next.a=aa;
                next.b=now.b-(aa-now.a);
                if (!v[next.a][next.b])
                {
                    v[next.a][next.b]=1;
                    next.step=now.step+1;
                    next.way[next.step]=6;
                    que.push(next);
                }
            }else if (now.b<aa-now.a)
            {
                next=now;
                next.a=now.a+now.b;
                next.b=0;
                if (!v[next.a][next.b])
                {
                    v[next.a][next.b]=1;
                    next.step=now.step+1;
                    next.way[next.step]=6;
                    que.push(next);
                }
            }
        }

    }
    printf("impossible\n");
}
int main()
{
    scanf("%d%d%d",&aa,&bb,&cc);
    bfs();
    return 0;
}

 

转载于:https://www.cnblogs.com/NicoleLam/p/4150123.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值