hdu1495之经典搜索
非常可乐
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3688 Accepted Submission(s): 1533
4 1 3
0 0 0
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之经典搜索的更多相关文章
- 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 ...
- 棍子Sticks(poj_1011)[经典搜索]
[题意描述] George用相同的长度棍子,将他们随机切成最多64个单位的长度,现在,他想回到原来的状态,但他忘了他原来的多少根,以及他们原本是多长.请帮助他和设计一个程序,计算最小的可能的原始长度. ...
- HDU 2102 A计划 经典搜索
A计划 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissio ...
- (中等) HDU 1043 Eight,经典搜索问题。
Problem Description The 15-puzzle has been around for over 100 years; even if you don't know it by t ...
- uva 1601 poj 3523 Morning after holloween 万圣节后的早晨 (经典搜索,双向bfs+预处理优化+状态压缩位运算)
这题数据大容易TLE 优化:预处理, 可以先枚举出5^3的状态然后判断合不合法,但是由于题目说了有很多墙壁,实际上没有那么多要转移的状态那么可以把底图抽出来,然后3个ghost在上面跑到时候就不必判断 ...
- (C++一本通)最少转弯问题 (经典搜索)
题目描述 给出一张地图,这张地图被分为n×m(n,m<=100)个方块,任何一个方块不是平地就是高山.平地可以通过,高山则不能.现在你处在地图的(x1,y1)这块平地,问:你至少需要拐几个弯才能 ...
- POJ 1011:Sticks 经典搜索
Sticks Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 128734 Accepted: 30173 Descrip ...
- Hdu 1016 Prime Ring Problem (素数环经典dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 【poj1011】 Sticks
http://poj.org/problem?id=1011 (题目链接) 题意 给出一大堆小棍子的长度,需要把他们拼成几根长度相等的大棍子,求大棍子的最短长度. Solution 经典搜索题,剪枝剪 ...
随机推荐
- Flash, Flex, Air, Flashplayer之间的相互关系是什么?
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:曾嵘链接:http://www.zhihu.com/question/20001256/answer/15565376来源:知 ...
- hdu3999The order of a Tree (二叉平衡树(AVL))
Problem Description As we know,the shape of a binary search tree is greatly related to the order of ...
- BZOJ 1305 CQOI2009 dance跳舞 二分答案+最大流
题目大意:给定n个男生和n个女生,一些互相喜欢而一些不.举行几次舞会,每次舞会要配成n对.不能有同样的组合出现.每一个人仅仅能与不喜欢的人跳k次舞,求最多举行几次舞会 将一个人拆成两个点.点1向点2连 ...
- CSDN高校俱乐部2013年秋季北京地区第一站“编程语言的应用及其发展”—北京联合大学
2013年12月11日晚17:00.CSDN高校俱乐2013年秋季北京地区第一站“编程语言的应用及其发展”在北京联合大学进行. 首先,CSDN总部人员介绍CSDN俱乐部的改版以及线上编程挑战赛.CSD ...
- Tesseract Ocr引擎
Tesseract Ocr引擎 1.Tesseract介绍 tesseract 是一个google支持的开源ocr项目,其项目地址:https://github.com/tesseract-ocr/t ...
- 重操JS旧业第七弹:面向对象与对象创建
JS是一种完全面向对象的程序设计语言,在面向对象处理方面,具有多种多样的实现方式,加之对象成员的动态性使得这门语言更加灵活:而js对象成员动态性也是创建和扩展对象的有力方式. 1 对象成员动态性 属性 ...
- Kendo UI开发教程(26): 单页面应用(四) Layout
Layout继承自View,可以用来包含其它的View或是Layout.下面例子使用Layout来显示一个View 1 <div id="app"></div&g ...
- JS - 6款鼠标悬停效果
下载地址:http://www.lanrentuku.com/js/tupian-1093.html
- 一次完整的HTTP请求所经历的7个步骤(前三步是浏览器工作,后四步是服务器工作)
HTTP通信机制是在一次完整的HTTP通信过程中,Web浏览器与Web服务器之间将完成下列7个步骤: 1. 建立TCP连接在HTTP工作开始之前,Web浏览器首先要通过网络与Web服务器建立连接,该连 ...
- Spark大型项目实战:电商用户行为分析大数据平台
本项目主要讲解了一套应用于互联网电商企业中,使用Java.Spark等技术开发的大数据统计分析平台,对电商网站的各种用户行为(访问行为.页面跳转行为.购物行为.广告点击行为等)进行复杂的分析.用统计分 ...