HDU 1495(倒水问题+BFS)

大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为。因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多。但seeyou的手中只有两个杯子,它们的容量分别是N 毫升和M 毫升 可乐的体积为S (S<101)毫升 (正好装满一瓶) ,它们三个之间可以相互倒可乐 (都是没有刻度的,且 S==N+M,101>S>0,N>0,M>0) 。聪明的ACMER你们说他们能平分吗?如果能请输出倒可乐的最少的次数,如果不能输出"NO"。

Input

三个整数 : S 可乐的体积 , N 和 M是两个杯子的容量,以"0 0 0"结束。

Output

如果能平分的话请输出最少要倒的次数,否则输出"NO"。

Sample Input

7 4 3
4 1 3
0 0 0

Sample Output

NO
3

思路:我的理解是他们都可以直接喝可乐瓶里的可乐,设三个瓶子为s,n,m,那么平分的情况就有n==m&&s==0,n==s&&m==0,m==s&&n==0,倒水方式有六种s->n,s->m,n->s,n->m,m->n,m->s,每一次倒水尽量使被倒的杯子满,bfs即可。看到有大佬用数学方法解此题并且代码量很少,我只能感叹自己数学能力的不足,还是老老实实bfs吧、、

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=200005;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
int s,n,m,vis[101][101][101];
struct node
{
    int s,n,m,step;
    node(int s,int n,int m,int st):s(s),n(n),m(m),step(st) {}
    node(){}
};
void bfs()
{
    memset(vis,0,sizeof(vis));
    node sta(s,0,0,0);
    queue<node> q;
    q.push(sta);
    while(!q.empty())
    {
        node t=q.front(),u;
        if((t.s==0&&t.m==t.n)||(t.m==0&&t.s==t.n)||(t.n==0&&t.s==t.m))
        {
            cout<<t.step<<endl;
            break;
        }
        q.pop();
        //s->n
        if(t.s&&t.n<n)
        {
            int g=n-t.n;
            if(t.s>=g)
            {
                u.n=n;
                u.s=t.s-g;
                u.m=t.m;
                u.step=t.step+1;
            }
            else
            {
                u.n=t.s+t.n;
                u.s=0;
                u.m=t.m;
                u.step=t.step+1;
            }
            if(!vis[u.s][u.n][u.m])
            {
                q.push(u);
                vis[u.s][u.n][u.m]=1;
            }
        }
        //s->m
        if(t.s&&t.m<m)
        {
            int g=m-t.m;
            if(t.s>=g)
            {
                u.m=m;
                u.s=t.s-g;
                u.n=t.n;
                u.step=t.step+1;
            }
            else
            {
                u.m=t.m+t.s;
                u.s=0;
                u.n=t.n;
                u.step=t.step+1;
            }
            if(!vis[u.s][u.n][u.m])
            {
                q.push(u);
                vis[u.s][u.n][u.m]=1;
            }
        }
        //n->m
        if(t.n&&t.m<m)
        {
            int g=m-t.m;
            if(t.n>=g)
            {
                u.m=m;
                u.n=t.n-g;
                u.s=t.s;
                u.step=t.step+1;
            }
            else
            {
                u.m=t.m+t.n;
                u.n=0;
                u.s=t.s;
                u.step=t.step+1;
            }
            if(!vis[u.s][u.n][u.m])
            {
                q.push(u);
                vis[u.s][u.n][u.m]=1;
            }
        }
        //n->s
        if(t.n&&t.s<s)
        {
            int g=s-t.s;
          /*  if(t.n>=g)
            {
                u.s=s;
                u.n=t.n-s;
                u.m=t.m;
                u.step=t.step+1;
            }*/
          //  else
            //{
                u.s=t.s+t.n;
                u.n=0;
                u.m=t.m;
                u.step=t.step+1;
          //  }
            if(!vis[u.s][u.n][u.m])
            {
                q.push(u);
                vis[u.s][u.n][u.m]=1;
            }
        }
        //m->s
        if(t.m&&t.s<s)
        {
            int g=s-t.s;
           /* if(t.m>=g)
            {
                u.s=s;
                u.m=t.m-s;
                u.n=t.n;
                u.step=t.step+1;
            }
            else
            {*/
                u.s=t.s+t.m;
                u.m=0;
                u.n=t.n;
                u.step=t.step+1;
         //   }
            if(!vis[u.s][u.n][u.m])
            {
                q.push(u);
                vis[u.s][u.n][u.m]=1;
            }
        }
        //m->n
        if(t.m&&t.n<n)
        {
            int g=n-t.n;
            if(t.m>=g)
            {
                u.n=n;
                u.m=t.m-g;
                u.s=t.s;
                u.step=t.step+1;
            }
            else
            {
                u.n=t.n+t.m;
                u.m=0;
                u.s=t.s;
                u.step=t.step+1;
            }
            if(!vis[u.s][u.n][u.m])
            {
                q.push(u);
                vis[u.s][u.n][u.m]=1;
            }
        }
    }
    if(q.empty())
    {
        cout<<"NO"<<endl;
    }
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    while(cin>>s>>n>>m)
    {
        if(!s&&!n&&!m)
        {
            break;
        }
        if(s%2)
        {
            cout<<"NO"<<endl;
            continue;
        }
        bfs();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Dilly__dally/article/details/81639360