HDOJ 1495 非常可乐

29 篇文章 0 订阅

题意:用三个瓶子,a瓶装满水,b,c是空瓶,Va=Vb+Vc,可以互相倒水,判断是否能将水平分。

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

思路:隐式图,有6种倒水的情况,广搜即可。

注意点:代码量比较大,其他没什么问题。


以下为AC代码:

Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor
119268342014-10-21 00:33:12Accepted149515MS384K9251 BG++luminous11
#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 <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

bool vis[106][106];
int ra, rb, rc, res;

int swap ( int &a, int &b )
{
    int tmp = a;
    a = b;
    b = tmp;
}

struct node
{
    int a;
    int b;
    int c;
    int t;
    node( int x, int y, int z, int k )
    {
        a = x;
        b = y;
        c = z;
        t = k;
    }
};

int BFS ( int a, int b, int c )
{
    int ta, tb, tc, tt;
    struct node pt( a, b, c, 0 );
    queue<node> q;
    q.push( pt );
    while ( ! q.empty() )
    {
        pt = q.front();
        q.pop();
        if ( pt.a == res )
        {
            return pt.t;
        }
        struct node tmp ( 0, 0, 0, 0 );
        for ( int i = 0; i < 6; i ++ )
        {
            if ( i == 0 && pt.a )//1->2
            {
                if ( pt.b == rb )
                {
                    continue;
                }
                else
                {
                    if ( ( rb - pt.b ) > pt.a )
                    {
                        tmp.a = 0;
                        tmp.b = pt.b + pt.a;
                        tmp.c = pt.c;
                        tmp.t = pt.t + 1;
                        if ( ! vis[tmp.a][tmp.b] )
                        {
                            q.push( tmp );
                            vis[tmp.a][tmp.b] = true;
                        }
                    }
                    else
                    {
                        tmp.a = pt.a - ( rb - pt.b );
                        tmp.b = rb;
                        tmp.c = pt.c;
                        tmp.t = pt.t + 1;
                        if ( tmp.a == res )
                        {
                            return tmp.t;
                        }
                        if ( ! vis[tmp.a][tmp.b] )
                        {
                            vis[tmp.a][tmp.b] = true;
                            q.push( tmp );
                        }
                    }
                }

            }

            if ( i == 1 && pt.a )//1->3
            {
                if ( pt.c == rc )
                {
                    continue;
                }
                else
                {
                    if ( pt.a > ( rc - pt.c ) )
                    {
                        tmp.a = pt.a - ( rc - pt.c );
                        tmp.b = pt.b;
                        tmp.c = rc;
                        tmp.t = pt.t + 1;
                        if ( tmp.a == res )
                        {
                            return tmp.t;
                        }
                        if ( ! vis[tmp.a][tmp.b] )
                        {
                            q.push ( tmp );
                            vis[tmp.a][tmp.b] = true;
                        }
                    }
                    else
                    {
                        tmp.a = 0;
                        tmp.b = pt.b;
                        tmp.c = pt.c + pt.a;
                        tmp.t = pt.t + 1;
                        if ( ! vis[tmp.a][tmp.b] )
                        {
                            vis[tmp.a][tmp.b] = true;
                            q.push ( tmp );
                        }
                    }
                }
            }
            if ( i == 2 && pt.b )//2->3
            {
                if ( pt.c == rc )
                {
                    continue;
                }
                else
                {
                    if ( pt.b > ( rc - pt.c ) )
                    {
                        tmp.a = pt.a;
                        tmp.b = pt.b - ( rc - pt.c );
                        tmp.c = rc;
                        tmp.t = pt.t + 1;
                        if ( ! vis[tmp.a][tmp.b] )
                        {
                            q.push( tmp );
                            vis[tmp.a][tmp.b] = true;
                        }
                    }
                    else
                    {
                        tmp.a = pt.a;
                        tmp.b = 0;
                        tmp.c = pt.b + pt.c;
                        tmp.t = pt.t + 1;
                        if ( ! vis[tmp.a][tmp.b] )
                        {
                            q.push( tmp );
                            vis[tmp.a][tmp.b] = true;
                        }
                    }
                }
            }
            if ( i == 3 && pt.b )//2->1
            {
                if ( ra == pt.a )
                {
                    continue;
                }
                else
                {
                    if ( pt.b > ( ra - pt.a ) )
                    {
                        tmp.a = ra;
                        tmp.b = pt.b - ( ra - pt.a );
                        tmp.c = pt.c;
                        tmp.t = pt.t + 1;
                        if ( ! vis[tmp.a][tmp.b] )
                        {
                            q.push ( tmp );
                            vis[tmp.a][tmp.b] = true;
                        }
                    }
                    else
                    {
                        tmp.a = pt.a + pt.b;
                        tmp.b = 0;
                        tmp.c = pt.c;
                        tmp.t = pt.t + 1;
                        if ( tmp.a == res )
                        {
                            return tmp.t;
                        }
                        if ( ! vis[tmp.a][tmp.b] )
                        {
                            q.push ( tmp );
                            vis[tmp.a][tmp.b] = true;
                        }
                    }
                }
            }
            if ( i == 4 && pt.c )//3->1
            {
                if ( pt.a == ra )
                {
                    continue;
                }
                else
                {
                    if ( pt.c > ( ra - pt.a ) )
                    {
                        tmp.a = ra;
                        tmp.b = pt.b;
                        tmp.c = pt.c - ( ra - pt.a );
                        tmp.t = pt.t + 1;
                        if ( ! vis[tmp.a][tmp.b] )
                        {
                            vis[tmp.a][tmp.b] = true;
                            q.push ( tmp );
                        }
                    }
                    else
                    {
                        tmp.a = pt.a + pt.c;
                        tmp.b = pt.b;
                        tmp.c = 0;
                        tmp.t = pt.t + 1;
                        if ( tmp.a == res )
                        {
                            return tmp.t;
                        }
                        if ( ! vis[tmp.a][tmp.b] )
                        {
                            vis[tmp.a][tmp.b] = true;
                            q.push ( tmp );
                        }
                    }
                }
            }
            if ( i == 5 && pt.c )//3->2
            {
                if ( pt.b == rb )
                {
                    continue;
                }
                else
                {
                    if ( pt.c > ( rb - pt.b ) )
                    {
                        tmp.a = pt.a;
                        tmp.b = rb;
                        tmp.c = pt.c - ( rb - pt.b );
                        tmp.t = pt.t + 1;
                        if ( ! vis[tmp.a][tmp.b] )
                        {
                            vis[tmp.a][tmp.b] = true;
                            q.push ( tmp );
                        }
                    }
                    else
                    {
                        tmp.a = pt.a ;
                        tmp.b = pt.b + pt.c;
                        tmp.c = 0;
                        tmp.t = pt.t + 1;
                        if ( ! vis[tmp.a][tmp.b] )
                        {
                            vis[tmp.a][tmp.b] = true;
                            q.push ( tmp );
                        }
                    }
                }
            }
        }
    }
    return 0;
}


int main()
{
    while ( cin >> ra >> rb >> rc )
    {
        if ( ! ra && ! rb && ! rc )
        {
            return 0;
        }
        if ( rb < rc )
        {
            swap ( rb, rc );
        }
        if ( ra & 1 )
        {
            cout << "NO" << endl;
            continue;
        }
        res = ra / 2;
        memset ( vis, false, sizeof ( vis ) );
        vis[ra][0] = true;
        int sum = BFS ( ra, 0, 0 );
        if ( sum == 0 )
        {
            cout << "NO" << endl;
        }
        else
        {
            cout << sum << endl;
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值