pots(倒水问题)

C - C
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)

<pre name="code" class="html">#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int vis[110][110];
int a,b,c;
struct node
{
    int k1,k2,step;
};
void bfs()
{
    queue<node>Q;
node next={0,0,0};
vis[0][0]=1;
Q.push(next);
while(!Q.empty())
{
    node f=Q.front();Q.pop();
    if(f.k1==c||f.k2==c)

    {
        next.step=f.step;
        printf("%d\n",next.step);
        return ;

    }
    for(int i=1;i<=6;i++)
    {
        if(i==1)//fill(1)
        {
            next.k1=a;
            next.k2=f.k2;
        }
        if(i==2)
        {
            next.k1=f.k1;
            next.k2=b;
        }
        if(i==3)//drop(1)
        {
            next.k1=0;
            next.k2=f.k2;

        }
        if(i==4)
        {
            next.k2=0;
            next.k1=f.k1;

        }
        if(i==5)//pour(1,2)
        {
            if(f.k1+f.k2<=b)
            {
                next.k1=0;
                next.k2=f.k1+f.k2;
            }
            else
            {
                next.k1=f.k1+f.k2-b;
                next.k2=b;
            }
        }
        if(i==6)

        {
            if(f.k1+f.k2<=a)
            {
                next.k1=f.k1+f.k2;
                next.k2=0;

            }
            else
            {
                next.k2=f.k1+f.k2-a;
                next.k1=a;
            }
        }
        if(!vis[next.k1][next.k2])
        {
            vis[next.k1][next.k2]=1;
            next.step=f.step+1;
            Q.push(next);
        }
    }
}
puts("impossible");


}
int main()
{
    while(scanf("%d%d%d",&a,&b,&c)!=EOF)
          {
              memset(vis,0,sizeof(vis));
              bfs();
          }
          return 0;
}

或者
 
    
</pre><pre code_snippet_id="454159" snippet_file_name="blog_20140819_3_1876495" name="code" class="html"><pre name="code" class="html">#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int vis[110][110];
int a,b,c;
struct node
{
    int k1,k2,step;
};
void bfs()
{
    queue<node>Q;
    node next= {0,0,0};
    vis[0][0]=1;
    Q.push(next);
    while(!Q.empty())
    {
        node f=Q.front();
        Q.pop();
        if(f.k1==c||f.k2==c)

        {

            printf("%d\n",f.step);
            return ;

        }


        if(f.k1!=a)//fill(1)
        {
            next.k1=a;
            next.k2=f.k2;
            next.step=f.step+1;
            if(!vis[next.k1][next.k2])
            {
                vis[next.k1][next.k2]=1;

                Q.push(next);
            }

        }
        if(f.k2!=b)
        {
            next.k1=f.k1;
            next.k2=b;
            next.step=f.step+1;
            if(!vis[next.k1][next.k2])
            {
                vis[next.k1][next.k2]=1;

                Q.push(next);
            }

        }
        if(f.k1!=0)//drop(1)
        {
            next.k1=0;
            next.k2=f.k2;
            next.step=f.step+1;
            if(!vis[next.k1][next.k2])
            {
                vis[next.k1][next.k2]=1;

                Q.push(next);
            }


        }
        if(f.k2!=0)
        {
            next.k2=0;
            next.k1=f.k1;
            next.step=f.step+1;
            if(!vis[next.k1][next.k2])
            {
                vis[next.k1][next.k2]=1;

                Q.push(next);
            }


        }
        if(f.k1!=0&&f.k2!=b)//pour(1,2)
        {
            if(f.k1+f.k2<=b)
            {
                next.k1=0;
                next.k2=f.k1+f.k2;
                next.step=f.step+1;


            }
            else
            {
                next.k1=f.k1+f.k2-b;
                next.k2=b;
                next.step=f.step+1;


            }
            if(!vis[next.k1][next.k2])
                {
                    vis[next.k1][next.k2]=1;

                    Q.push(next);
                }


        }
        if(f.k1!=a&&f.k2!=0)

        {
            if(f.k1+f.k2<=a)
            {
                next.k1=f.k1+f.k2;
                next.k2=0;
                next.step=f.step+1;


            }
            else
            {
                next.k2=f.k1+f.k2-a;
                next.k1=a;
                next.step=f.step+1;


            }
            if(!vis[next.k1][next.k2])
                {
                    vis[next.k1][next.k2]=1;

                    Q.push(next);
                }
        }

    }
    puts("impossible");
}





int main()
{
    while(scanf("%d%d%d",&a,&b,&c)!=EOF)
    {
        memset(vis,0,sizeof(vis));
        bfs();
    }
    return 0;
}


 
   
 
   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值