HDU 1495 非常可乐

29 篇文章 0 订阅

题意:给出三个水壶,体积分别为A、B、C,(A=B+C),在这三个水壶之间互相倒水,是否能将这壶水平分成两份

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495

思路:对每个体积记录并广搜

注意点:无


以下为AC代码:

Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor
130011132015-02-28 17:17:58Accepted1495124MS2324K3625 BG++luminous
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;
//const double pi = acos(-1);
//const double eps = 1e-10;
//const int dir[[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
int a, b, c;
int ans;
int sum;
bool vis[105][105][105];
struct node{
    int a, b, c, cnt;
    node(){}
    node( int _a, int _b, int _c, int _cnt ) : a(_a), b(_b), c(_c), cnt(_cnt) {}
};
inline bool judge ( const node & tmp )
{
    int cnt = 0;
    if ( tmp.a == sum )
        cnt ++;
    if ( tmp.b == sum )
        cnt ++;
    if ( tmp.c == sum )
        cnt ++;
    if ( cnt >= 2 ){
        ans = tmp.cnt;
        return true;
    }
    return false;
}
inline bool init()
{
    clr ( vis, 0 );
    cin >> a >> b >> c;
    if ( a || b || c )
        return true;
    else
        return false;
}
bool solve()
{
    if ( a & 1 )return false;
    sum = a / 2;
    queue<node> q;
    q.push ( node( a, 0, 0, 0 ) );
    vis[a][0][0] = 1;
    while ( ! q.empty() ){
        node tmp = q.front();
        q.pop();
        if ( judge( tmp ) ){
            return true;
        }
        if ( tmp.a && tmp.b < b ){
            int t = min ( tmp.a, b - tmp.b );
            if ( vis[tmp.a-t][tmp.b+t][tmp.c] == 0 ){
                vis[tmp.a-t][tmp.b+t][tmp.c] = 1;
                q.push ( node( tmp.a-t, tmp.b+t, tmp.c, tmp.cnt+1 ) );
            }
        }
        if ( tmp.a && tmp.c < c ){
            int t = min ( tmp.a, c - tmp.c );
            if ( vis[tmp.a-t][tmp.b][tmp.c+t] == 0 ){
                vis[tmp.a-t][tmp.b][tmp.c+t] = 1;
                q.push ( node ( tmp.a-t, tmp.b, tmp.c+t, tmp.cnt+1 ) );
            }
        }
        if ( tmp.b && tmp.a < a ){
            int t = min ( tmp.b, a - tmp.a );
            if ( vis[tmp.a+t][tmp.b-t][tmp.c] == 0 ){
                vis[tmp.a+t][tmp.b-t][tmp.c] = 1;
                q.push ( node( tmp.a+t, tmp.b-t, tmp.c, tmp.cnt+1 ) );
            }
        }
        if ( tmp.b && tmp.c < c ){
            int t = min ( tmp.b, c - tmp.c );
            if ( vis[tmp.a][tmp.b-t][tmp.c+t] == 0 ){
                vis[tmp.a][tmp.b-t][tmp.c+t] = 1;
                q.push ( node ( tmp.a, tmp.b-t, tmp.c+t, tmp.cnt+1 ) );
            }
        }
        if ( tmp.c && tmp.a < a ){
            int t = min ( tmp.c, a - tmp.a );
            if ( vis[tmp.a+t][tmp.b][tmp.c-t] == 0 ){
                vis[tmp.a+t][tmp.b][tmp.c-t] = 1;
                q.push ( node ( tmp.a+t, tmp.b, tmp.c-t, tmp.cnt+1 ) );
            }
        }
        if ( tmp.c && tmp.b < b ){
            int t = min ( tmp.c, b - tmp.b );
            if ( vis[tmp.a][tmp.b+t][tmp.c-t] == 0 ){
                vis[tmp.a][tmp.b+t][tmp.c-t] = 1;
                q.push ( node ( tmp.a, tmp.b+t, tmp.c-t, tmp.cnt+1 ) );
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio( false );
    while ( init() ){
        if ( solve() ){
            cout << ans << endl;
        }
        else{
            cout << "NO" << endl;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值