博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu1495(bfs)
阅读量:7059 次
发布时间:2019-06-28

本文共 2587 字,大约阅读时间需要 8 分钟。

 

题目链接:

题意:有三个杯子,开始时第一个杯子装满水(体积为a),倒来倒去,得到其中2个杯里的水的体积都为a/2,求最小次数,不存在就输出NO。

分析:因为被子没有刻度,所以倒入时要倒满或倒完才能保证知道容积,即有6种情况来分别遍历。

#include 
#include
#include
#include
#include
#include
#include
#include
#define INF 100000000#define maxn 111111#define ll __int64#define lson 1,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;int vis[110][110][110];int a,b,c,flag;struct node{ int x,y,z; int step;};int judge(node k){ if((k.x==k.y&&k.z==0)||(k.x==k.z&&k.y==0)||(k.y==k.z&&k.x==0)) return 1; return 0;}void bfs(){ queue
que; node now,next; int num; now.x=a;now.y=0; now.z=0;now.step=0; vis[a][0][0]=1; que.push(now); while(!que.empty()) { now=que.front();que.pop(); if(judge(now)) { printf("%d\n",now.step); flag=1; return; } if(now.x>0)//a倒入其他 { if(b>now.y)//a倒入b { num=b-now.y; next.z=now.z; next.step=now.step+1; if(now.x>num)//a倒不完 { next.x=now.x-num; next.y=b; } else//倒完 { next.y=now.x+now.y; next.x=0; } if(!vis[next.x][next.y][next.z]) { vis[next.x][next.y][next.z]=1; que.push(next); } } if(c>now.z)//a倒入c { num=c-now.z; next.y=now.y; next.step=now.step+1; if(now.x>num)//倒不完 { next.x=now.x-num; next.z=c; } else//倒完 { next.z=now.x+now.z; next.x=0; } if(!vis[next.x][next.y][next.z]) { vis[next.x][next.y][next.z]=1; que.push(next); } } } if(now.y>0)//b倒入其他 { if(a>now.x) { num=a-now.x; next.z=now.z; next.step=now.step+1; if(now.y>num) { next.y=now.y-num; next.x=a; } else { next.x=now.x+now.y; next.y=0; } if(!vis[next.x][next.y][next.z]) { vis[next.x][next.y][next.z]=1; que.push(next); } } if(c>now.z) { num=c-now.z; next.x=now.x; next.step=now.step+1; if(now.y>num) { next.y=now.y-num; next.z=c; } else { next.z=now.y+now.z; next.y=0; } if(!vis[next.x][next.y][next.z]) { vis[next.x][next.y][next.z]=1; que.push(next); } } } if(now.z>0)//c倒入其他 { if(b>now.y) { num=b-now.y; next.x=now.x; next.step=now.step+1; if(now.z>num) { next.z=now.z-num; next.y=b; } else { next.y=now.z+now.y; next.z=0; } if(!vis[next.x][next.y][next.z]) { vis[next.x][next.y][next.z]=1; que.push(next); } } if(a>now.x) { num=a-now.x; next.y=now.y; next.step=now.step+1; if(now.z>num) { next.z=now.z-num; next.x=a; } else { next.x=now.x+now.z; next.z=0; } if(!vis[next.x][next.y][next.z]) { vis[next.x][next.y][next.z]=1; que.push(next); } } } }}int main(){ while(scanf("%d%d%d",&a,&b,&c)>0) { if(a+b+c==0)break; if(a%2)//剪枝 { puts("NO"); continue; } memset(vis,0,sizeof(vis)); flag=0; bfs(); if(!flag)puts("NO"); }}
View Code

 

转载于:https://www.cnblogs.com/lienus/p/4168345.html

你可能感兴趣的文章
Snap up RS3gold 3500M 60% off rs3 for sale &learn
查看>>
oracle函数
查看>>
json与String的转化
查看>>
linux上解压版安装jdk,tomcat
查看>>
科略教育—企业为什么始终处于竞争状态?
查看>>
iphone开发
查看>>
解决:在微信中访问app下载链接提示“已停止访问该网页”
查看>>
使用阿里云ECS自建RDS MySQL从库
查看>>
Linux下sed命令
查看>>
胃病犯了怎么办
查看>>
三星2610打印机故障INTERNAL ERROR - Incomplete Session by time out
查看>>
马哥2016全新Linux+Python高端运维班第五周作业
查看>>
thinkphp 跨模块调用配置文件信息
查看>>
nohup命令在后台自动执行程序
查看>>
MAVEN项目后 jar包无法发布到eclipse的web服务器
查看>>
选择结构与循环结构
查看>>
Linux系统生成随机密码的10种方法
查看>>
puppet初体验
查看>>
oracle安装前环境检查
查看>>
ansible出错
查看>>