poj 3414 搜索BFS

题意:有A,B两个瓶子,开始是空的,有六种操作,问最少几步能让其中一个瓶子是C的体积;

#include<stdio.h>
#include<string.h>
struct node
{
    int a;
    int b;
    int parent;
    int level;
    int op;
} pot[10005];
int stk[10005];
int visit[105][105];
void output(int p)
{
    int top=0;
    stk[0]=p;
    while(pot[stk[top]].parent!=-1)
    {
        top++;
        stk[top]=pot[stk[top-1]].parent;
    }
    for(int i=top; i>=0; i--)
    {
        switch(pot[stk[i]].op)
        {
        case 0:
        {
            printf("DROP(1)\n");
        }
        break;
        case 1:
        {
            printf("FILL(1)\n");
        }
        break;
        case 2:
        {
            printf("DROP(2)\n");
        }
        break;
        case 3:
        {
            printf("FILL(2)\n");
        }
        break;
        case 4:
        {
            printf("POUR(1,2)\n");
        }
        break;
        case 5:
        {
            printf("POUR(2,1)\n");
        }
        break;
        }
    }
}

void bfs(int A,int B,int C)
{

    node cur;
    pot[0].a=0;
    pot[0].b=0;
    pot[0].parent=-1;
    pot[0].level=0;
    pot[0].op=-1;
    int front=0;
    int rear=1;
    visit[0][0]=1;
    int locate=-1,ans;
    while(front<rear)
    {
        cur=pot[front++];
        if(cur.a==C||cur.b==C)
        {
            ans=cur.level;
            locate=front-1;
            break ;
        }
        int ta,tb,tp;
        for(int i=0; i<6; i++)
        {

            if(i==0)
            {
                ta=0;
                tb=cur.b;
                tp=i;
            }
            if(i==1)
            {
                ta=A;
                tb=cur.b;
                tp=i;
            }
            if(i==2)
            {
                tb=0;
                ta=cur.a;
                tp=i;
            }
            if(i==3)
            {
                tb=B;
                ta=cur.a;
                tp=i;
            }

            if(i==4)
            {
                if((B-cur.b)>=cur.a)
                {
                    ta=0;
                    tb=cur.a+cur.b;
                    tp=i;
                }
                else
                {
                    ta=cur.a-(B-cur.b);
                    tb=B;
                    tp=i;
                }
            }
            if(i==5)
            {
                if((A-cur.a)>=cur.b)
                {
                    tb=0;
                    ta=cur.a+cur.b;
                    tp=i;
                }
                else
                {
                    ta=A;
                    tb=cur.b-(A-cur.a);
                    tp=i;
                }
            }
            if(visit[ta][tb]==0)
            {
                visit[ta][tb]=1;
                node change;
                change.a=ta;
                change.b=tb;
                change.parent=front-1;
                change.level=cur.level+1;
                change.op=tp;
                pot[rear++]=change;

            }
        }
    }
    if(locate==-1)
    {
        printf("impossible\n");
    }
    else
    {
        printf("%d\n",ans);
        output(locate);
    }
}
int main()
{
    int A,B,C;
    scanf("%d%d%d",&A,&B,&C);
    memset(visit,0,sizeof(visit));
    bfs(A,B,C);
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值