非常可乐

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

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

分析:因为有三个杯子a,b,c,所以对于下一步可以分6种情况:a向b,c中倒水,b向a,c中倒水,c向a,b中倒水,对这6种情况直接搜索即可

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std; const int MAX=101+10;
int sum,n,m;
bool mark[MAX][MAX];//表示杯子a,b有水i,j的状态是否到达过 struct Node{
int a,b,c,time;//代表杯子a,b,c里的水量和倒水的次数
Node(){}
Node(int A,int B,int C,int Time):a(A),b(B),c(C),time(Time){}
}start; int BFS(){
memset(mark,false,sizeof mark);
start=Node(2*sum,0,0,0);
mark[start.a][start.b]=true;
Node oq,next;
queue<Node>q;
q.push(start);
while(!q.empty()){
oq=q.front();
q.pop();
int x=2*sum-oq.a,y=n-oq.b,z=m-oq.c;
if(x){//向a里面可以倒水
if(oq.b)next=Node(oq.a+min(x,oq.b),oq.b-min(x,oq.b),oq.c,oq.time+1);//b向a倒水
if(!mark[next.a][next.b]){
mark[next.a][next.b]=true;
if(next.a == sum && next.b == sum || (next.a == sum && next.c == sum) || (next.b == sum && next.c == sum))return next.time;
q.push(next);
}
if(oq.c)next=Node(oq.a+min(x,oq.c),oq.b,oq.c-min(x,oq.c),oq.time+1);//c向a倒水
if(!mark[next.a][next.b]){
mark[next.a][next.b]=true;
if(next.a == sum && next.b == sum || (next.a == sum && next.c == sum) || (next.b == sum && next.c == sum))return next.time;
q.push(next);
}
}
if(y){//向b里面倒水
if(oq.a)next=Node(oq.a-min(oq.a,y),oq.b+min(oq.a,y),oq.c,oq.time+1);//a向b倒水
if(!mark[next.a][next.b]){
mark[next.a][next.b]=true;
if(next.a == sum && next.b == sum || (next.a == sum && next.c == sum) || (next.b == sum && next.c == sum))return next.time;
q.push(next);
}
if(oq.c)next=Node(oq.a,oq.b+min(y,oq.c),oq.c-min(y,oq.c),oq.time+1);//c向b倒水
if(!mark[next.a][next.b]){
mark[next.a][next.b]=true;
if(next.a == sum && next.b == sum || (next.a == sum && next.c == sum) || (next.b == sum && next.c == sum))return next.time;
q.push(next);
}
}
if(z){//向c倒水
if(oq.a)next=Node(oq.a-min(oq.a,z),oq.b,oq.c+min(oq.a,z),oq.time+1);//a向c倒水
if(!mark[next.a][next.b]){
mark[next.a][next.b]=true;
if(next.a == sum && next.b == sum || (next.a == sum && next.c == sum) || (next.b == sum && next.c == sum))return next.time;
q.push(next);
}
if(oq.b)next=Node(oq.a,oq.b-min(oq.b,z),oq.c+min(oq.b,z),oq.time+1);//b向c倒水
if(!mark[next.a][next.b]){
mark[next.a][next.b]=true;
if(next.a == sum && next.b == sum || (next.a == sum && next.c == sum) || (next.b == sum && next.c == sum))return next.time;
q.push(next);
}
}
}
return -1;
} int main(){
while(cin>>sum>>n>>m,sum+n+m){
if(sum%2){cout<<"NO"<<endl;continue;}
sum=sum/2;
int temp=BFS();
if(temp == -1)cout<<"NO"<<endl;
else cout<<temp<<endl;
}
return 0;
}

hdu1495之经典搜索的更多相关文章

  1. hdu 1043(经典搜索)

    题意: 给你一个初始的图,然后每次输入一个图,要求移动x最小的步数达到和初始图一样,输出路径 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 5 6 7 8 5 6 7 8 5 6 7 ...

  2. 棍子Sticks(poj_1011)[经典搜索]

    [题意描述] George用相同的长度棍子,将他们随机切成最多64个单位的长度,现在,他想回到原来的状态,但他忘了他原来的多少根,以及他们原本是多长.请帮助他和设计一个程序,计算最小的可能的原始长度. ...

  3. HDU 2102 A计划 经典搜索

    A计划 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissio ...

  4. (中等) HDU 1043 Eight,经典搜索问题。

    Problem Description The 15-puzzle has been around for over 100 years; even if you don't know it by t ...

  5. uva 1601 poj 3523 Morning after holloween 万圣节后的早晨 (经典搜索,双向bfs+预处理优化+状态压缩位运算)

    这题数据大容易TLE 优化:预处理, 可以先枚举出5^3的状态然后判断合不合法,但是由于题目说了有很多墙壁,实际上没有那么多要转移的状态那么可以把底图抽出来,然后3个ghost在上面跑到时候就不必判断 ...

  6. (C++一本通)最少转弯问题 (经典搜索)

    题目描述 给出一张地图,这张地图被分为n×m(n,m<=100)个方块,任何一个方块不是平地就是高山.平地可以通过,高山则不能.现在你处在地图的(x1,y1)这块平地,问:你至少需要拐几个弯才能 ...

  7. POJ 1011:Sticks 经典搜索

    Sticks Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 128734   Accepted: 30173 Descrip ...

  8. Hdu 1016 Prime Ring Problem (素数环经典dfs)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. 【poj1011】 Sticks

    http://poj.org/problem?id=1011 (题目链接) 题意 给出一大堆小棍子的长度,需要把他们拼成几根长度相等的大棍子,求大棍子的最短长度. Solution 经典搜索题,剪枝剪 ...

随机推荐

  1. c语言,变长数组

    下面这个结构体,可以在malloc的时候指定数据data的长度,这样的形式就是变长数组:typedef struct{ int data_len; char data[0];//或char data[ ...

  2. 纯CSS设置Checkbox复选框控件的样式

    Checkbox复选框是一个可能每一个网站都在使用的HTML元素,但大多数人并不给它们设置样式,所以在绝大多数网站它们看起来是一样的.为什么不把你的网站中的Checkbox设置一个与众不同的样式,甚至 ...

  3. Swift - 闭包的介绍及用法(以数组排序为例)

    闭包(即一些小的匿名代码块),可以像函数一样使用.可以很方便的将闭包传给其他函数,告诉它们应当如何执行某一个任务. 1,使用sort方法和闭包进行数组排序 sort方法返回一个数组的有序版本.(sor ...

  4. asp.net2.0安全性(2)--用户个性化设置(2)--转载来自车老师

    上一篇我们用Profile.age等方式可以读取用户的年龄和其它的信息,但有的时候我们要查询显示所有用户的信息,但asp.net没有提供查询所有用户信息的功能,我们只能对现有的用户逐一查询其Profi ...

  5. 用Delphi进行word开发

    使用以CreateOleObjects方式调用Word 实际上还是Ole,但是这种方式能够真正做到完全控制Word文件,能够使用Word的所有属性,包括自己编写的VBA宏代码.------------ ...

  6. 程序员必须知道的几个Git代码托管平台(转)

    上一篇博客中2015继续任性——不会Git命令,照样玩转Git我们简单的介绍了在VS2013中使用Git,和GitHub客户端的使用.那么使用Git到底有什么好处呢?最为明显的是支持Git代码托管的平 ...

  7. 基于visual Studio2013解决面试题之0608找出两个只出现一次的数

     题目

  8. linux安装Eclipse c++环境

    yum install eclipse     yum install eclipse-cdt

  9. HDU4706:Children's Day

    Problem Description Today is Children's Day. Some children ask you to output a big letter 'N'. 'N' i ...

  10. strip 命令的使用方法

    用途 通过除去绑定程序和符号调试程序使用的信息,降低扩展公共对象文件格式(XCOFF)的对象文件的大小. 语法 strip [ -V ] [ -r [ -l ] | -x [ -l ] | -t | ...