SDUT Pots 2780

来源:互联网 发布:access 导入excel sql 编辑:程序博客网 时间:2024/06/06 07:16

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