SDUTOJ【2780】 pots---bfs

来源:互联网 发布:mac怎么循环播放视频 编辑:程序博客网 时间:2024/06/06 23:17

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;}


0 0