D - Pots


  
  
D - Pots
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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)

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int p[100][100];
int i,j;
int a,b,c;

struct node
{
    int x,y,count;
}q[5000];

void BFS()
{
    int s = 0,e = 0;
    struct node t,f;
    t.x = 0;
    t.y = 0;
    t.count = 0;
    q[e++] = t;
    p[t.x][t.y] = 1;
    while(s<e)
    {
        t = q[s++];
        if(t.x == c || t.y == c)
        {
            printf("%d\n",t.count);
            return ;
        }
        for(i=0;i<6;i++)
        {
            if(i == 0)
            {
                f.x = a;
                f.y = t.y;
            }
            else if(i == 1)
            {
                f.x = t.x;
                f.y = b;
            }
            else if(i == 2)
            {
                f.x = 0;
                f.y = t.y;
            }
            else if(i == 3)
            {
                f.x = t.x;
                f.y = 0;
            }
            else if(i == 4)
            {
                f.x = t.x + t.y;
                if(f.x>a)
                {
                    f.y = f.x - a;
                    f.x = a;

                }
                else
                {
                    f.y = 0;
                }
            }
            else if(i == 5)
            {
                f.y = t.x + t.y;
                if(f.y>b)
                {
                    f.x = f.y - b;
                    f.y = b;
                }
                else
                {
                    f.x = 0;
                }
            }
            if(p[f.x][f.y] == 0)
            {
                f.count = t.count + 1;
                q[e++] = f;
                p[f.x][f.y] = 1;
            }
        }
    }
    printf("impossible\n");
    return ;

}
int main()
{
    while(scanf("%d%d%d",&a,&b,&c)!=EOF)
    {
        memset(p,0,sizeof(p));
        BFS();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值