poj 3414 pots (bfs 倒水问题)

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int vis[120][120];
int vl,vr;
int ok;
int key;
struct node
{
    int l,r;
    int fa;
    int op;
};
node str[100000];
int ans[100000];
void bfs()
{
    int l=0,r=1;
    node next;
    str[0].l=0;
    str[0].r=0;
    str[0].fa=0;
    while(l<r)
    {
        //printf("%d %d...\n",str[l].l,str[l].r);
        vis[str[l].l][str[l].r]=1;
        if(str[l].l==key||str[l].r==key)
        {
            ok=1;
            int len=0;
            while(str[l].fa!=0)
            {
                ans[len++]=str[l].op;
                l=str[l].fa;
            }
            ans[len]=str[l].op;
            printf("%d\n",len+1);
            for(int i=len;i>=0;i--)
            {
                if(ans[i]==1) printf("FILL(1)\n");
                else if(ans[i]==2)  printf("FILL(2)\n");
                    else if(ans[i]==3) printf("DROP(1)\n");
                    else if(ans[i]==4) printf("DROP(2)\n");
                    else if(ans[i]==5) printf("POUR(1,2)\n");
                    else if(ans[i]==6) printf("POUR(2,1)\n");
            }
            return ;
        }
        if(str[l].l<vl)
        {
            next.l=vl;
            next.r=str[l].r;
            next.fa=l;
            next.op=1;
            if(vis[next.l][next.r]==0)
            str[r++]=next;

            if(str[l].r>0)
            {
               next.l=str[l].l+str[l].r;
               if(next.l>vl)
               {
                   next.r=next.l-vl;
                   next.l=vl;
               }
               else next.r=0;
               next.fa=l;
               next.op=6;
              if(vis[next.l][next.r]==0)
               str[r++]=next;
            }

        }
        if(str[l].r<vr)
        {
            next.l=str[l].l;
            next.r=vr;
            next.fa=l;
            next.op=2;
            if(vis[next.l][next.r]==0)
            str[r++]=next;

            if(str[l].l>0)
            {
               next.r=str[l].l+str[l].r;
               if(next.r>vr)
               {
                   next.l=next.r-vr;
                   next.r=vr;
               }
               else next.l=0;
               next.fa=l;
               next.op=5;
              if(vis[next.l][next.r]==0)
               str[r++]=next;
            }
        }
        if(str[l].l>0)
        {
            next.l=0;
            next.r=str[l].r;
            next.fa=l;
            next.op=3;
            if(vis[next.l][next.r]==0)
            str[r++]=next;
        }
        if(str[l].r>0)
        {
            next.l=str[l].l;
            next.r=0;
            next.fa=l;
            next.op=4;
            if(vis[next.l][next.r]==0)
            str[r++]=next;
        }
        l++;
    }
}
int main()
{
    while(scanf("%d%d%d",&vl,&vr,&key)!=EOF)
    {
        memset(vis,0,sizeof(vis));
        ok=0;
        bfs();
        if(ok==0)
            printf("impossible\n");
    }
    return 0;
}

  

posted @ 2015-02-12 01:37  sola94  阅读(118)  评论(0编辑  收藏  举报