POJ 3414 Pots

简单$bfs$。

仔细一些就能过。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-8;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c = getchar(); x = 0;while(!isdigit(c)) c = getchar();
    while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar();  }
}

const int INF=0x7FFFFFFF;
const int maxn=110;
struct X
{
    int a,b;
    int op;
    int fa;
}s[1000000];
int A,B,C,t,sz;
int f[maxn][maxn];

void bfs()
{
    queue<int>q; sz=0;

    for(int i=0;i<=A;i++) for(int j=0;j<=B;j++) f[i][j]=INF;
    f[0][0]=0;

    s[sz].a=0; s[sz].b=0; s[sz].fa=-1; s[sz].op=-1;
    q.push(0);

    t=-1;
    while(!q.empty())
    {

        int h=q.front(); q.pop();
        if(s[h].a==C||s[h].b==C) { t=h; break; }

        //FILL(a) 1
        if(s[h].a!=A)
        {
            if(f[s[h].a][s[h].b]+1<f[A][s[h].b])
            {
                f[A][s[h].b]=f[s[h].a][s[h].b]+1;
                sz++;
                s[sz].a=A;
                s[sz].b=s[h].b;
                s[sz].fa=h;
                s[sz].op=1;
                q.push(sz);
            }
        }

        //FILL(b) 4
        if(s[h].b!=B)
        {
            if(f[s[h].a][s[h].b]+1<f[s[h].a][B])
            {
                f[s[h].a][B]=f[s[h].a][s[h].b]+1;
                sz++;
                s[sz].a=s[h].a;
                s[sz].b=B;
                s[sz].fa=h;
                s[sz].op=4;
                q.push(sz);
            }
        }

        //DROP(a) 2
        if(s[h].a!=0)
        {
            if(f[s[h].a][s[h].b]+1<f[0][s[h].b])
            {
                f[0][s[h].b]=f[s[h].a][s[h].b]+1;
                sz++;
                s[sz].a=0;
                s[sz].b=s[h].b;
                s[sz].fa=h;
                s[sz].op=2;
                q.push(sz);
            }
        }

        //DROP(b) 5
        if(s[h].b!=0)
        {
            if(f[s[h].a][s[h].b]+1<f[s[h].a][0])
            {
                f[s[h].a][0]=f[s[h].a][s[h].b]+1;
                sz++;
                s[sz].a=s[h].a;
                s[sz].b=0;
                s[sz].fa=h;
                s[sz].op=5;
                q.push(sz);
            }
        }

        //POUR(a,b) 3
        if(s[h].a!=0&&s[h].b!=B)
        {
            int f1,f2;
            if(s[h].a>=B-s[h].b) f1=s[h].a-(B-s[h].b), f2=B;
            else f1=0, f2=s[h].b+s[h].a;

            if(f[s[h].a][s[h].b]+1<f[f1][f2])
            {
                f[f1][f2]=f[s[h].a][s[h].b]+1;
                sz++;
                s[sz].a=f1;
                s[sz].b=f2;
                s[sz].fa=h;
                s[sz].op=3;
                q.push(sz);
            }
        }

        //POUR(b,a) 6
        if(s[h].b!=0&&s[h].a!=A)
        {
            int f1,f2;
            if(s[h].b>=A-s[h].a) f1=A, f2=s[h].b-(A-s[h].a);
            else f1=s[h].a+s[h].b, f2=0;

            if(f[s[h].a][s[h].b]+1<f[f1][f2])
            {
                f[f1][f2]=f[s[h].a][s[h].b]+1;
                sz++;
                s[sz].a=f1;
                s[sz].b=f2;
                s[sz].fa=h;
                s[sz].op=6;
                q.push(sz);
            }
        }
    }
}

void out(int op)
{
    if(op==-1) return ;
    if(op==1) printf("FILL(1)\n");
    if(op==2) printf("DROP(1)\n");
    if(op==3) printf("POUR(1,2)\n");
    if(op==4) printf("FILL(2)\n");
    if(op==5) printf("DROP(2)\n");
    if(op==6) printf("POUR(2,1)\n");

}
int main()
{
    while(~scanf("%d%d%d",&A,&B,&C))
    {
        bfs();
        if(t==-1) printf("impossible\n");
        else
        {
            printf("%d\n",f[s[t].a][s[t].b]);

            int now=t; stack<int>st;
            while(now!=-1) st.push(now), now=s[now].fa;

            while(!st.empty())
            {
                out(s[st.top()].op);
                st.pop();
            }
        }
    }
    return 0;
}

 

posted @ 2016-09-01 22:22  Fighting_Heart  阅读(138)  评论(0编辑  收藏  举报