HDU 1495 非常可乐

简单的bfs。。 复制啊复制啊。。
其实做了好久。。

#include<stdio.h>
#include<string.h>
int front,end;
int all;
int b1,b2,b3;
int way[101][101][101];
int queue[101*101*101+101];
int s,n,m;
int bfs(int b1,int b2,int b3)
{
    front=end=0;
    queue[end++]=b1;
    queue[end++]=b2;
    queue[end++]=b3;
    memset(way,-1,sizeof(way));
    way[b1][b2][b3]=0;
    while(front!=end)
    {
        b1=queue[front++];
        b2=queue[front++];
        b3=queue[front++];
        if((b1==all/2&&b2==all/2)||(b1==all/2&&b3==all/2)||(b2==all/2&&b3==all/2))
            return way[b1][b2][b3];
        for(int i=1;i<=6;i++)
        {
            if(i==1)
            {
                if(b1>=(n-b2))
                {
                    int eb1=b1-(n-b2);
                    int eb2=n;
                    int eb3=b3;
                    if(way[eb1][eb2][eb3]>=0||eb1>s||eb2>n||eb3>m)
                        continue;
                    queue[end++]=eb1;
                    queue[end++]=eb2;
                    queue[end++]=eb3;
                    way[eb1][eb2][eb3]=way[b1][b2][b3]+1;
                }
                else
                {
                    int eb2=b2+b1;
                    int eb1=0;
                    int eb3=b3;
                    if(way[eb1][eb2][eb3]>=0||eb1>s||eb2>n||eb3>m)
                        continue;
                    queue[end++]=eb1;
                    queue[end++]=eb2;
                    queue[end++]=eb3;
                    way[eb1][eb2][eb3]=way[b1][b2][b3]+1;
                }
            }
            if(i==2)
            {
                if(b1>=(m-b3))
                {
                    int eb1=b1-(m-b3);
                    int eb3=m;
                    int eb2=b2;
                    if(way[eb1][eb2][eb3]>=0||eb1>s||eb2>n||eb3>m)
                        continue;
                    queue[end++]=eb1;
                    queue[end++]=eb2;
                    queue[end++]=eb3;
                    way[eb1][eb2][eb3]=way[b1][b2][b3]+1;
                }
                else
                {
                    int eb3=b3+b1;
                    int eb1=0;
                    int eb2=b2;
                    if(way[eb1][eb2][eb3]>=0||eb1>s||eb2>n||eb3>m)
                        continue;
                    queue[end++]=eb1;
                    queue[end++]=eb2;
                    queue[end++]=eb3;
                    way[eb1][eb2][eb3]=way[b1][b2][b3]+1;
                }
            }
            if(i==3)
            {
                if(b2>=(s-b1))
                {
                    int eb2=b2-(s-b1);
                    int eb1=s;
                    int eb3=b3;
                    if(way[eb1][eb2][eb3]>=0||eb1>s||eb2>n||eb3>m)
                        continue;
                    queue[end++]=eb1;
                    queue[end++]=eb2;
                    queue[end++]=eb3;
                    way[eb1][eb2][eb3]=way[b1][b2][b3]+1;
                }
                else
                {
                    int eb1=b1+b2;
                    int eb2=0;
                    int eb3=b3;
                    if(way[eb1][eb2][eb3]>=0||eb1>s||eb2>n||eb3>m)
                        continue;
                    queue[end++]=eb1;
                    queue[end++]=eb2;
                    queue[end++]=eb3;
                    way[eb1][eb2][eb3]=way[b1][b2][b3]+1;
                }
            }
            if(i==4)
            {
                if(b2>=(m-b3))
                {
                    int eb2=b2-(m-b3);
                    int eb3=m;
                    int eb1=b1;
                    if(way[eb1][eb2][eb3]>=0||eb1>s||eb2>n||eb3>m)
                        continue;
                    queue[end++]=eb1;
                    queue[end++]=eb2;
                    queue[end++]=eb3;
                    way[eb1][eb2][eb3]=way[b1][b2][b3]+1;
                }
                else
                {
                    int eb3=b2+b3;
                    int eb2=0;
                    int eb1=b1;
                    if(way[eb1][eb2][eb3]>=0||eb1>s||eb2>n||eb3>m)
                        continue;
                    queue[end++]=eb1;
                    queue[end++]=eb2;
                    queue[end++]=eb3;
                    way[eb1][eb2][eb3]=way[b1][b2][b3]+1;
                }
            }
            if(i==5)
            {
                if(b3>=(s-b1))
                {
                    int eb3=b3-(s-b1);
                    int eb1=s;
                    int eb2=b2;
                    if(way[eb1][eb2][eb3]>=0||eb1>s||eb2>n||eb3>m)
                        continue;
                    queue[end++]=eb1;
                    queue[end++]=eb2;
                    queue[end++]=eb3;
                    way[eb1][eb2][eb3]=way[b1][b2][b3]+1;
                }
                else
                {
                    int eb1=b1+b3;
                    int eb3=0;
                    int eb2=b2;
                    if(way[eb1][eb2][eb3]>=0||eb1>s||eb2>n||eb3>m)
                        continue;
                    queue[end++]=eb1;
                    queue[end++]=eb2;
                    queue[end++]=eb3;
                    way[eb1][eb2][eb3]=way[b1][b2][b3]+1;
                }
            }
            if(i==6)
            {
                if(b3>=(n-b2))
                {
                    int eb3=b3-(n-b2);
                    int eb2=n;
                    int eb1=b1;
                    if(way[eb1][eb2][eb3]>=0||eb1>s||eb2>n||eb3>m)
                        continue;
                    queue[end++]=eb1;
                    queue[end++]=eb2;
                    queue[end++]=eb3;
                    way[eb1][eb2][eb3]=way[b1][b2][b3]+1;
                }
                else
                {
                    int eb2=b2+b3;
                    int eb3=0;
                    int eb1=b1;
                    if(way[eb1][eb2][eb3]>=0||eb1>s||eb2>n||eb3>m)
                        continue;
                    queue[end++]=eb1;
                    queue[end++]=eb2;
                    queue[end++]=eb3;
                    way[eb1][eb2][eb3]=way[b1][b2][b3]+1;
                }
            }
        }
    }
return-1;
}
int main()
{
     while(scanf("%d%d%d",&s,&n,&m)!=EOF)
     {
        if(s==0)
            break;
        if(s%2==1)
        {
            printf("NO\n");
            continue;
        }
        all=s;
        int ans=bfs(s,0,0);
        if(ans==-1)
            printf("NO\n");
        else
            printf("%d\n",ans);
     }
    return 0;
}

你可能感兴趣的:(HDU,bfs)