SDUT Pots 2780

Pots

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
 
FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i)      empty the pot i to the drain;
POUR(i,j)    pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

输入

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

输出

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

示例输入

3 5 4

示例输出

6
 
   
 
   
 
   
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct node
{
    int x,y;
    int sum;
} q[200000];
int b[1001][1001];
int n,m,v;
void bfs()
{
    int k=0,l=0;
    memset(b,0,sizeof(b));
    b[0][0]=1;
    struct node t,r;
    int i;
    t.x=0;
    t.y=0;
    t.sum=0;


    q[l++]=t;
    while(k<l)
    {
        t=q[k++];

        if(t.x==v || t.y==v)
        {
            printf("%d\n",t.sum);
            return ;
        }
        for(i=0; i<6; i++)
        {
            if(i==0)
                {
                    r.x=n;
                    r.y=t.y;
                }
            if(i==1)
                {
                    r.x=t.x;
                    r.y=m;
                }
            if(i==2)
            {
                r.y = t.x + t.y;
                if(r.y>=m)
                {
                    r.x=r.y-m;
                    r.y=m;

                }
                else
                    r.x=0;
            }
            if(i==3)
            {
                r.x=t.y+t.x;
                if(r.x>=n)
                {
                    r.y=r.x-n;
                    r.x=n;

                }
                else
                    r.y=0;
            }
            if(i==4)
               {
                   r.x=0;
                   r.y=t.y;
               }
            if (i==5)
                {
                    r.x=t.x;
                    r.y=0;
                }

            if(b[r.x][r.y]==0)
            {
                r.sum=t.sum+1;
                q[l++]=r;
                b[r.x][r.y]=1;
            }
        }
    }
    printf("impossible\n");
}
int main()
{
    while(scanf("%d%d%d",&n,&m,&v)!=EOF)
    {
        bfs();
    }

    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值