HDU-1495-非常可乐【BFS】

1495-非常可乐

        Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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

题目链接:HDU-1495

题目思路:BFS,每种状态6种可能性,S->N(S中水倒给N),S->M,N->M,N->S,M->S,M->N。

以下是代码:

//
//  M.cpp
//  搜索
//
//  Created by pro on 16/3/24.
//  Copyright (c) 2016年 pro. All rights reserved.
//

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <queue>
using namespace std;
struct node
{
    int s,n,m;
    int cnt;
};
node b[200];
int s,m,n;
int vis[105][105][105];
queue <node> que;

int bfs()
{
    node zero;
    zero.s = s;
    zero.m = 0;
    zero.n = 0;
    zero.cnt = 0;
    que.push(zero);
    vis[s][0][0] = 1;
    while (!que.empty())
    {
        node front = que.front();
        que.pop();
        int need = 0;
        if ((front.s == s / 2 && front.m == s / 2) || (front.n == s / 2 && front.m == s / 2) || (front.s == s / 2 && front.n == s / 2) ) return front.cnt;
        if (front.n < n && front.s > 0)  //s倒n的情况
        {
            node tmp;
            tmp.m = front.m;
            tmp.cnt = front.cnt+1;
            need = n - front.n;

            if (need < front.s)  //s把n倒满
            {
                tmp.n = n;
                tmp.s = front.s - need;
            }
            else  //s全部倒进n
            {
                tmp.n = front.s + front.n;
                tmp.s = 0;
            }
            if (!vis[tmp.s][tmp.m][tmp.n])
            que.push(tmp);
            vis[tmp.s][tmp.m][tmp.n] = 1;
        }
        if (front.m < m && front.s > 0)  //s倒m的情况
        {
            node tmp;
            tmp.n = front.n;
            tmp.cnt = front.cnt+1;
            need = m - front.m;
          //  cout <<need  << " " << m<< endl;
            if (need < front.s)  //s把m倒满
            {
                tmp.m = m;
                tmp.s = front.s - need;
            }
            else  //s全部倒进m
            {
                tmp.m = front.s + front.m;
                tmp.s = 0;
            }
            if (!vis[tmp.s][tmp.m][tmp.n])
            que.push(tmp);
            vis[tmp.s][tmp.m][tmp.n] = 1;
        }

        if (front.s < s && front.m > 0)  //m倒s的情况
        {
            node tmp;
            tmp.n = front.n;
            tmp.cnt = front.cnt+1;
            need = s - front.s;
            if (need < front.m)  //m把s倒满
            {
                tmp.s = s;
                tmp.m = front.m - need;
            }
            else  //m全部倒进s
            {
                tmp.s = front.m + front.s;
                tmp.m = 0;
            }
            if (!vis[tmp.s][tmp.m][tmp.n])
            que.push(tmp);
            vis[tmp.s][tmp.m][tmp.n] = 1;
        }
        if (front.n < n && front.m > 0)  //m倒n的情况
        {
            node tmp;
            tmp.s = front.s;
            tmp.cnt = front.cnt+1;
            need = n - front.n;
            if (need < front.m)  //m把n倒满
            {
                tmp.n = n;
                tmp.m = front.m - need;
            }
            else  //m全部倒进n
            {
                tmp.n = front.m + front.n;
                tmp.m = 0;
            }
            if (!vis[tmp.s][tmp.m][tmp.n])
            que.push(tmp);
            vis[tmp.s][tmp.m][tmp.n] = 1;
        }
        if (front.m < m && front.n > 0)  //n倒m的情况
        {
            node tmp;
            tmp.s = front.s;
            tmp.cnt = front.cnt+1;
            need = m - front.m;
            if (need < front.n)  //n把m倒满
            {
                tmp.m = m;
                tmp.n = front.n - need;
            }
            else  //n全部倒进m
            {
                tmp.m = front.n + front.m;
                tmp.n = 0;
            }
            if (!vis[tmp.s][tmp.m][tmp.n])
            que.push(tmp);
            vis[tmp.s][tmp.m][tmp.n] = 1;
        }
        if (front.s < s && front.n > 0)  //n倒s的情况
        {
            node tmp;
            tmp.m = front.m;
            tmp.cnt = front.cnt+1;
            need = s - front.s;
            if (need < front.n)  //n把s倒满
            {
                tmp.s = s;
                tmp.n = front.n - need;
            }
            else  //n全部倒进s
            {
                tmp.s = front.n + front.s;
                tmp.n = 0;
            }
            if (!vis[tmp.s][tmp.m][tmp.n])
            que.push(tmp);
            vis[tmp.s][tmp.m][tmp.n] = 1;
        }

    }
    return -1;
}
int main()
{
    while(cin >> s >> m >> n)
    {
        memset(vis,0,sizeof(vis));
        if (s == 0 && m == 0 && n == 0) break;
        while(!que.empty()) que.pop();
        if (s % 2) cout << "NO" << endl;
        else
        {
            int ans = bfs();
            if (ans == -1) cout << "NO\n";
            else cout << ans << endl;
        }
    }
    return 0;
}

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