hdu 1495 非常可乐 广搜

#include<iostream>

#include<cstdio>

#include<cstring>

#include<queue>

int vi[102][102][102];

using namespace std;

struct Point{

    int a,b,c,v;

    Point(int x,int y,int z,int vv){a=x,b=y,c=z,v=vv;}

};

void pu(int &a,int &b,int c)

{

    if(b==c)

        return ;

    if(a+b<=c)

        b+=a,a=0;

    else

        a-=c-b,b=c;

}

int Min=10000000;

void bfs(int a,int b,int c)

{

    int i,j,x,y,z;

    queue<Point> q;

    Point p(0,0,c,0);

    q.push(p);

    int s=c,n=a,m=b;

    while(!q.empty())

    {

        p=q.front();

        q.pop();

        x=p.a,y=p.b,z=p.c;

        //cout<<x<<" "<<y<<" "<<z<<"* "<<p.v<<endl;

        if(x*2==s)

        {

            if(y*2==s||z*2==s)

            {

                if(p.v<Min)

                    Min=p.v;

            }

            else

            {

                if(p.v+1<Min)

                    Min=p.v+1;

            }

        }

        if(y*2==s)

        {

            if(x*2==s||z*2==s)

            {

                if(p.v<Min)

                    Min=p.v;

            }

            else//cout<<x<<" "<<y<<" "<<" "<<s<<" "<<Min<<" "<<p.v+1<<endl;

            if(p.v+1<Min)

            {

                Min=p.v+1;

                    

            }

        }

        if(z*2==s)

        {

            if(x*2==s||y*2==s)

            {

                if(p.v<Min)

                    Min=p.v;

            }

            else

            {

                if(p.v+1<Min)

                    Min=p.v+1;

            }

        }

        vi[x][y][z]=1;

        int t1,t2;

        t1=x,t2=y;

        pu(t1,t2,m);

        if(!vi[t1][t2][z])

            q.push(Point(t1,t2,z,p.v+1));

        t1=x,t2=z;

        pu(t1,t2,s);

        if(!vi[t1][y][t2])

            q.push(Point(t1,y,t2,p.v+1));

        t1=y,t2=x;

        pu(t1,t2,n);

        if(!vi[t2][t1][z])

            q.push(Point(t2,t1,z,p.v+1));

        t1=y,t2=z;

        pu(t1,t2,s);

        if(!vi[x][t1][t2])

            q.push(Point(x,t1,t2,p.v+1));

        t1=z,t2=x;

        pu(t1,t2,n);

        if(!vi[t2][y][t1])

            q.push(Point(t2,y,t1,p.v+1));

        t1=z,t2=y;

        pu(t1,t2,m);

        if(!vi[x][t2][t1])

            q.push(Point(x,t2,t1,p.v+1));

    }

}

int main()

{

    int a,b,c;

    while(scanf("%d%d%d",&a,&b,&c),a||b||c)

    {

        if(a%2==1||a==0||b==0||c==0)

        {

            printf("NO\n");

            continue;

        }

        memset(vi,0,sizeof(vi));

        Min=100000000;

        bfs(b,c,a);

        if(Min==100000000)

            printf("NO\n");

        else

        printf("%d\n",Min);

    }

    return 0;

}

 

你可能感兴趣的:(HDU)