码迷,mamicode.com
首页 > 其他好文 > 详细

HDOJ 1495 非常可乐 【BFS】

时间:2015-03-18 23:24:56      阅读:340      评论:0      收藏:0      [点我收藏+]

标签:bfs   priority_queue   

非常可乐

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5954    Accepted Submission(s): 2428


Problem Description
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是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

分析:可以将本题看成一个隐式图,枚举所有的情况直到能够平分,或者枚举完。

代码:

#include <cstdio>
#include <queue>  //用到了优先队列
#include <cstring>
#include <algorithm>
const int M = 101;
using namespace std;

bool vis[101][101][101]; //标记状态
struct node{
    int v[3];
    int step;
    bool operator < (const node &a) const{  
        return step > a.step;
    }
};
int hal, v[3];  //hal是一半,v数组是每个杯子的容积

bool match(node a){
    int ans = 0;
    for(int i = 0; i < 3; ++ i)
        if(a.v[i] == hal) ++ans;
    if(ans == 2) return 1;  //
    return 0;
}

int bfs(){
    memset(vis, 0, sizeof(vis));
    node st;
    st.v[0] = v[0]; st.v[1] = st.v[2] = 0;
    st.step = 0;
    priority_queue<node >q;
    q.push(st);
    vis[v[0]][0][0] = 1;
    if(match(st)) return st.step;
    while(!q.empty()){
        node temp = q.top();
        q.pop();
        for(int i = 0; i < 3; ++ i){  
            if(temp.v[i]){  //如果一个杯子有水,就让他往其他两个杯子倒水
                for(int j = 1; j < 3; ++ j){
                    node cur = temp;
                    int w = (i+j)%3;
                    if(cur.v[w] < v[w]){  //倒的时候也分是不是满了,如果不满那么还需要判断能不能倒满还是不能倒满
                        if(cur.v[i] <= (v[w]-cur.v[w])){
                            cur.v[w] += cur.v[i]; cur.v[i] = 0;
                            if(match(cur)) return cur.step+1;
                        }
                        else {
                            cur.v[i] -= (v[w]-cur.v[w]);
                            cur.v[w] = v[w];
                            if(match(cur)) return cur.step+1;
                        }
                        if(!vis[cur.v[0]][cur.v[1]][cur.v[2]]){
                                vis[cur.v[0]][cur.v[1]][cur.v[2]] = 1;
                                cur.step++;
                                q.push(cur);
                        }
                    }
                }
            }
        }
    } 
    return -1;
}

int main(){
    while(scanf("%d%d%d", &v[0], &v[1], &v[2]), v[0]||v[1]||v[2]){
        if(v[0]&1){
            printf("NO\n");
            continue;
        }
        else {
            hal = v[0]/2;
            int ans = bfs();
            if(ans >= 0) printf("%d\n", ans);
            else printf("NO\n");
        }
    }
    return 0;
}


HDOJ 1495 非常可乐 【BFS】

标签:bfs   priority_queue   

原文地址:http://blog.csdn.net/shengweisong/article/details/44425231

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!