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 经典搜索题,剪枝剪 ...
随机推荐
- shell语法简单介绍
一.基本的语法 1.1.shell文件开头 shell文件必须以以下的行開始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的參数是用来运行该文件的程序.在这个样例中我们 ...
- paip.php 配置ZEND DEBUGGER 断点调试for cli..
paip.php 配置ZENDDEBUGGER 断点调试for cli.. 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http:/ ...
- 语音信号处理之(三)矢量量化(Vector Quantization)
语音信号处理之(三)矢量量化(Vector Quantization) zouxy09@qq.com http://blog.csdn.net/zouxy09 这学期有<语音信号处理>这门 ...
- 迟来SQLHelper
机房收费系统个人重构版敲完登陆系统之后往后敲了几个窗口,对于那些数据库连接SqlConnenction.SqlConnamd等常常敲反复的代码,之前也看过其它人的博客,这个东西不用还真不行. SqlH ...
- 恭喜我开通了CSDN博客
准备在这里写点东西,记录我的学习过程....
- 自己总结的ruby on rails 查询方法
闲来无事,结合以前的代码,总结了ruby on rails的查询方法,方便自己以后查看,也方便后来人,如下,欢迎批评指正 1::simpleDB modules = find(:all, :condi ...
- TCP/IP协议的编写《转载》
基于HHARM9-EDU的TCP/IP(UDP)协议的实现 原文网址:http://blog.csdn.net/lhj0503/article/details/3323788 摘 要:嵌入式技术的发展 ...
- js / ajax 成功提交后怎么跳转到另外一个页面?
把success那段改成 success : function (r) { if ( r.status == 'error' ){ alert(msg[r.msgno]); } else if (r. ...
- 使用mex进行混合编程的一些注意事项
1.mxGetPr的使用: Use mxGetPr on arrays of type double only. Use mxIsDouble to validate the mxArray type ...
- 【玩转cocos2d-x之四十】怎样在Cocos2d-x 3.0中使用opengl shader?
有小伙伴提出了这个问题.事实上GLProgramCocos2d-x引擎自带了.全然能够直接拿来用. 先上图吧. 使用opengl前后的对照: watermark/2/text/aHR0cDovL2Js ...