SDUTOJ【2780】 pots---bfs

Description

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

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.  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

Hint

FILL(2)
POUR(2,1)



code:
#include<stdio.h>
#include<string.h>
struct node
{
    int a,b;
    int cs;
}stack[100005],qq;

int vt[105][105],a,b,c;

void bfs()
{
    int v=0,u=0,i,j,k,s,e,z,x;
    stack[v].a=0;
    stack[v].b=0;
    stack[v].cs=0;
    v++;
    while(u<v)
    {
        qq=stack[u];
        if(qq.a==c || qq.b==c)
        {
            printf("%d\n",stack[u].cs);
            return ;
        }
        {//把b倒入a
            if(qq.b>=a-qq.a)
            {
                x=qq.b-a+qq.a;
                z=a;
            }
            else
            {
                x=0;
                z=qq.a+qq.b;
            }
                if(vt[z][x]==0 && z>=0 && z<=a && x>=0 && x<=b)
                {
                    stack[v].a=z;
                    stack[v].b=x;
                    stack[v].cs=stack[u].cs+1;
                    v++;
                    vt[z][x]=1;
                }
        }

        {//把b倒满
            z=qq.a;
            x=b;
                if(vt[z][x]==0 && z>=0 && z<=a && x>=0 && x<=b)
                {
                    stack[v].a=z;
                    stack[v].b=x;
                    stack[v].cs=stack[u].cs+1;
                    v++;
                    vt[z][x]=1;
                }
        }

        {//把b倒掉
            z=qq.a;
            x=0;
                if(vt[z][x]==0 && z>=0 && z<=a && x>=0 && x<=b)
                {
                    stack[v].a=z;
                    stack[v].b=x;
                    stack[v].cs=stack[u].cs+1;
                    v++;
                    vt[z][x]=1;
                }
        }

        {//把a倒入b
            if(qq.a>=b-qq.b)
            {
                z=qq.a-b+qq.b;
                x=b;
            }
            else
            {
                z=0;
                x=qq.a+qq.b;
            }
                if(vt[z][x]==0 && z>=0 && z<=a && x>=0 && x<=b)
                {
                    stack[v].a=z;
                    stack[v].b=x;
                    stack[v].cs=stack[u].cs+1;
                    v++;
                    vt[z][x]=1;
                }
        }

        {//把a倒满
            z=a;
            x=qq.b;
                if(vt[z][x]==0 && z>=0 && z<=a && x>=0 && x<=b)
                {
                    stack[v].a=z;
                    stack[v].b=x;
                    stack[v].cs=stack[u].cs+1;
                    v++;
                    vt[z][x]=1;
                }
        }

        {//把a倒掉
            z=0;
            x=qq.b;
                if(vt[z][x]==0 && z>=0 && z<=a && x>=0 && x<=b)
                {
                    stack[v].a=z;
                    stack[v].b=x;
                    stack[v].cs=stack[u].cs+1;
                    v++;
                    vt[z][x]=1;
                }
        }
        //printf("%d  \n",u);
        u++;
    }
printf("impossible\n");
}
int main()
{
    int n,m,i,j,k;
    while(scanf("%d%d%d",&a,&b,&c)!=EOF)
    {
        memset(vt,0,sizeof(vt));
        vt[0][0]=1;
        bfs();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值