HDU 1495 非常可乐!!!(隐式图状态搜索+bfs+map状态查重)

非常可乐

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


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
 

Author
seeyou
 

Source
 

Recommend
LL   |   We have carefully selected several similar problems for you:   1175  1253  1072  1372  1180 
总结:
这道题A的时候真是爽到爆炸啊,大致思路很简单,就是把每个状态看作一个节点去搜索,状态之间的转移是通过倒水操作来完成的,这道题主要用到了hash和map来判断状态是否重复,因为有三个水杯,每个水杯容量都是小于109的,所以我们把他们当作109进制的个位十位百位的权制,然后通过重载<来使得不同的状态分配到不同的位置上, 哦,对了,最后的最后写完的时候还是wa了,因为我的map没有清空啊啊!!以后使用map之前一定要记得清空啊!!

//
//  main.cpp
//  非常可乐
//
//  Created by 张嘉韬 on 16/8/23.
//  Copyright © 2016年 张嘉韬. All rights reserved.
//

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int maxn=1000000;
struct node{
    int a;
    int b;
    int c;
    int step;
    long long hash;
    bool operator <(const node &b) const
    {
        if(hash!=b.hash) return hash<b.hash;
        if(a!=b.a) return a<b.a;
        return c<b.c;
    }
}nodes[maxn];
int s,m,n,tempa,tempb,tempc;
map<node,int> vis;
void op0()
{
    int need=m-tempb;
    tempa-=need;
    tempb=m;
}
void op1()
{
    int need=n-tempc;
    tempa-=need;
    tempc=n;
}
void op2()
{
    tempa+=tempb;
    tempb=0;
}
void op3()
{
    int need=n-tempc;
    if(need>=tempb)
    {
        tempc+=tempb;
        tempb=0;
    }
    else
    {
        tempc=n;
        tempb-=need;
    }
}
void op4()
{
    tempa+=tempc;
    tempc=0;
}
void op5()
{
    int need=m-tempb;
    if(need>=tempc)
    {
        tempb+=tempc;
        tempc=0;
    }
    else
    {
        tempb=m;
        tempc-=need;
    }
}
int dfs(int a,int b,int c)
{
    nodes[1].a=a;
    nodes[1].b=0;
    nodes[1].c=0;
    nodes[1].step=0;
    nodes[1].hash=101*101*101*nodes[1].a+101*101*nodes[1].b+101*nodes[1].c;
    vis[nodes[1]]=1;
    int counter=1;
    queue <int> q;
    q.push(1);
    while(!q.empty())
    {
        int temp=q.front();
        q.pop();
//        cout<<nodes[temp].a<<"*"<<nodes[temp].b<<"*"<<nodes[temp].c<<endl;
        for(int i=0;i<6;i++)
        {
            tempa=nodes[temp].a;
            tempb=nodes[temp].b;
            tempc=nodes[temp].c;
            int temps=nodes[temp].step;
    
            if(i==0) op0();
            else if(i==1) op1();
            else if(i==2) op2();
            else if(i==3) op3();
            else if(i==4) op4();
            else if(i==5) op5();
            
            
            counter++;
            nodes[counter].a=tempa;
            nodes[counter].b=tempb;
            nodes[counter].c=tempc;
            nodes[counter].step=temps+1;
            nodes[counter].hash=101*101*101*tempa+101*101*tempb+101*tempc;
//            cout<<"("<<i<<")";
//            cout<<tempa<<" "<<tempb<<" "<<tempc<<" "<<endl;
            if(vis.count(nodes[counter])==1)
            {
                //cout<<"(b)"<<endl;
                counter--;
                continue;
            }
            if((tempc==s/2&&tempc==tempb)||(tempa==s/2&&tempa==tempb)||(tempa==s/2&&tempa==tempc))
            {

//                cout<<tempa<<" "<<tempb<<" "<<tempc<<" "<<endl;
                return temps+1;
            }
            vis[nodes[counter]]=1;
            q.push(counter);
        }
    }
    return 0;
}
int main(int argc, const char * argv[]) {
    //freopen("/Users/zhangjiatao/Documents/暑期训练/input.txt","r",stdin);
    while(scanf("%d%d%d",&s,&m,&n))
    {
        vis.clear();
        if(s==0&&m==0&&n==0) break;
        if(s%2==1) {printf("NO\n"); continue;}
        int ans=dfs(s,0,0);
        if(ans) printf("%d\n",ans);
        else printf("NO\n");
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值