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”。

BFS时有6种操作,分别是S->M,S->N,N->S,N->M,M->S,M->N。每次讲这6中情况放入队列,遇到结果输出即可。

若被分的可乐为奇数,那么无法平分,因为给出的可乐都是整数。

代码总览

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define nmax 105
#define inf 1000000
using namespace std;
bool visit[nmax][nmax][nmax];
typedef struct{
    int n;
    int m;
    int s;
    int times;
//    int type;
}mes;
int N,M,S,ans,Target;
void bfs()
{
    queue<mes> q;
    while(!q.empty()) q.pop();
    mes temp = {0,0,S,0},head;
    temp.n = temp.m = temp.times = 0;
    temp.s = S;
    memset(visit,0,sizeof(visit));
    visit[temp.n][temp.m][temp.s] = true;
    q.push(temp);
    while(!q.empty()){
        head = q.front();q.pop();
        if((head.m == Target && head.n == Target) || (head.m == Target && head.s == Target) || (head.n == Target && head.s == Target)){
            ans = head.times;
            return;
        }

        for(int i = 0;i<6;++i){
            temp = head;
            if(i == 0){// S->N
                if(head.s + head.n < N){
                    temp.n = head.s + head.n;
                    temp.s = 0;
                }else{
                    temp.n = N;
                    temp.s = head.s + head.n - N;
                }
            }else if(i == 1){// S->M
                if(head.s + head.m < M){
                    temp.m = head.s + head.m;
                    temp.s = 0;
                }else{
                    temp.m = M;
                    temp.s = head.s + head.m - M;
                }
            }else if(i == 2){// N->S
                if(head.s + head.n < S){
                    temp.n = 0;
                    temp.s = head.s + head.n;
                }else{
                    temp.n = head.s + head.n - S;
                    temp.s = S;
                }
            }else if( i == 3){// M->S
                if(head.s + head.m < S){
                    temp.m = 0;
                    temp.s = head.s + head.m;
                }else{
                    temp.m = head.s + head.m - S;
                    temp.s = S;
                }
            }else if(i == 4){// N->M
                if(head.m + head.n < M){
                    temp.n = 0;
                    temp.m = head.m + head.n;
                }else{
                    temp.m = M;
                    temp.n = head.m + head.n - M;
                }
            }else if(i == 5){// M->N
                if(head.m + head.n < N){
                    temp.n = head.m + head.n;
                    temp.m = 0;
                }else{
                    temp.n = N;
                    temp.m = head.m + head.n - N;
                }
            }
            if(!visit[temp.n][temp.m][temp.s]){
                visit[temp.n][temp.m][temp.s] = true;
//                temp.type = i;
                temp.times = head.times +1;
                //printf("%d %d %d %d %d\n",temp.n,temp.m,temp.s,temp.times,temp.type);
                q.push(temp);
            }
        }
    }
}
int main()
{
//    freopen("out.txt","w",stdout);
    while(scanf("%d %d %d",&S,&N,&M) != EOF){
        if(N == 0 && M == 0 && S == 0) break;
        if(S%2 !=0){
            printf("NO\n");
            continue;
        }
//        printf("N M S TIME\n");
//        printf("%d %d %d\n",N,M,S);
        ans = inf;
        Target = S/2;
        bfs();
        if(ans == inf) printf("NO\n");
        else printf("%d\n",ans);
    }
    return 0;
}
posted @ 2017-07-29 10:36  pengwill  阅读(142)  评论(0编辑  收藏  举报