HDU 1495 非常可乐 BFS搜索

题意:有个为三个杯子(杯子没有刻度),体积为s,n,m,s=m+n,

刚开始只有体积为s的杯子装满可乐,可以互相倒,问你最少的次数使可乐均分,如果没有结果,输出-1;

分析:直接互相倒就完了,BFS模拟

注:写的很丑

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <set>
#include <queue>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn=100+5;
const int INF=0x3f3f3f3f;
int a[maxn][maxn][maxn];
struct asd
{
    int x,y,z;
} o,t;
queue<asd>q;
int main()
{
    int s,n,m;
    while(~scanf("%d%d%d",&s,&n,&m),s)
    {
        if(s%2)
        {
            printf("NO\n");
            continue;
        }
        memset(a,-1,sizeof(a));
        a[s][0][0]=0;
        o.x=s,o.y=0,o.z=0;
        while(!q.empty())q.pop();
        q.push(o);
        int ans=-1;
        while(!q.empty())
        {
            o=q.front();
            q.pop();
            int cnt=0;
            if(o.x==s/2)cnt++;
            if(o.y==s/2)cnt++;
            if(o.z==s/2)cnt++;
            if(cnt==2)
            {
                ans=a[o.x][o.y][o.z];
                break;
            }
            if(o.x)
            {
                if(o.y<n)
                {
                    int k=n-o.y;
                    if(o.x<=k)t.y=o.y+o.x,t.x=0;
                    else t.y=n,t.x=o.x-k;
                    t.z=o.z;
                    if(a[t.x][t.y][t.z]==-1)
                        a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+1,q.push(t);
                }
                if(o.z<m)
                {
                    int k=m-o.z;
                    if(o.x<=k)t.z=o.z+o.x,t.x=0;
                    else t.z=m,t.x=o.x-k;
                    t.y=o.y;
                    if(a[t.x][t.y][t.z]==-1)
                        a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+1,q.push(t);
                }
            }
            if(o.y)
            {
                if(o.x<s)
                {
                    int k=s-o.x;
                    if(o.y<=k)t.x=o.x+o.y,t.y=0;
                    else t.x=s,t.y=o.y-k;
                    t.z=o.z;
                    if(a[t.x][t.y][t.z]==-1)
                        a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+1,q.push(t);
                }
                if(o.z<m)
                {
                    int k=m-o.z;
                    if(o.y<=k)t.z=o.z+o.y,t.y=0;
                    else t.z=m,t.y=o.y-k;
                    t.x=o.x;
                    if(a[t.x][t.y][t.z]==-1)
                        a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+1,q.push(t);
                }
            }
            if(o.z)
            {
                if(o.y<n)
                {
                    int k=n-o.y;
                    if(o.z<=k)t.y=o.y+o.z,t.z=0;
                    else t.y=n,t.z=o.z-k;
                    t.x=o.x;
                    if(a[t.x][t.y][t.z]==-1)
                        a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+1,q.push(t);
                }
                if(o.x<s)
                {
                    int k=s-o.x;
                    if(o.z<=k)t.x=o.z+o.x,t.z=0;
                    else t.x=s,t.z=o.z-k;
                    t.y=o.y;
                    if(a[t.x][t.y][t.z]==-1)
                        a[t.x][t.y][t.z]=a[o.x][o.y][o.z]+1,q.push(t);
                }
            }
        }
        if(ans==-1)printf("NO\n");
        else printf("%d\n",ans);
    }
    return 0;
}
View Code

 

posted @ 2016-01-31 11:36  shuguangzw  阅读(159)  评论(0编辑  收藏  举报