hdu_1495_非常可乐(bfs模拟)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1495
题意:不解释
题解:BFS模拟,不过要细心,把所有情况都列举出来,开一个数组记录状态,代码有点长,比较重复,尝试优化了一下,不过WA了
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
struct dt{int s,m,n,t;};
int s,m,n;
bool v[][][];
int bfs(){
memset(v,,sizeof(v));
v[s][][]=;
dt tmp,o;tmp.s=s,tmp.m=,tmp.n=,tmp.t=;
queue<dt>Q;Q.push(tmp);
while(!Q.empty()){
o=Q.front();Q.pop();
if((o.s==o.m&&!o.n)||(o.s==o.n&&!o.m)||(o.n==o.m&&!o.s))return o.t;
if(o.s){
if(o.m<m){
if(o.s+o.m>=m){
tmp.m=m,tmp.s=o.s+o.m-m,tmp.n=o.n,tmp.t=o.t+;
if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
}else{
tmp.m=o.m+o.s,tmp.s=,tmp.n=o.n,tmp.t=o.t+;
if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
}
}
if(o.n<n){
if(o.s+o.n>=n){
tmp.n=n,tmp.s=o.s+o.n-n,tmp.m=o.m,tmp.t=o.t+;
if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
}else{
tmp.n=o.n+o.s,tmp.s=,tmp.m=o.m,tmp.t=o.t+;
if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
}
}
}
if(o.m){
if(o.s<s){
if(o.s+o.m>=s){
tmp.s=s,tmp.m=o.m+o.s-s,tmp.n=o.n,tmp.t=o.t+;
if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
}else{
tmp.s=o.s+o.m,tmp.m=,tmp.n=o.n,tmp.t=o.t+;
if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
}
}
if(o.n<n){
if(o.m+o.n>=n){
tmp.n=n,tmp.m=o.m+o.n-n,tmp.s=o.s,tmp.t=o.t+;
if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
}else{
tmp.n=o.n+o.m,tmp.m=,tmp.s=o.s,tmp.t=o.t+;
if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
}
}
}
if(o.n){
if(o.m<m){
if(o.n+o.m>=m){
tmp.m=m,tmp.n=o.n+o.m-m,tmp.s=o.s,tmp.t=o.t+;
if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
}else{
tmp.m=o.m+o.n,tmp.n=,tmp.s=o.s,tmp.t=o.t+;
if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
}
}
if(o.s<s){
if(o.n+o.s>=s){
tmp.s=s,tmp.n=o.n+o.s-s,tmp.m=o.m,tmp.t=o.t+;
if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
}else{
tmp.s=o.s+o.n,tmp.n=,tmp.m=o.m,tmp.t=o.t+;
if(!v[tmp.s][tmp.m][tmp.n]){Q.push(tmp);v[tmp.s][tmp.m][tmp.n]=;}
}
}
}
}
return -;
}
int main(){
while(~scanf("%d%d%d",&s,&m,&n),s+n+m){
if(s%){puts("NO");continue;}
int ans=bfs();
if(ans==-)puts("NO");
else printf("%d\n",ans);
}
return ;
}
hdu_1495_非常可乐(bfs模拟)的更多相关文章
- HDU1495 非常可乐 —— BFS + 模拟
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 非常可乐 Time Limit: 2000/1000 MS (Java/Others) M ...
- BFS+模拟 ZOJ 3865 Superbot
题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...
- HDU 1495 非常可乐 BFS搜索
题意:有个为三个杯子(杯子没有刻度),体积为s,n,m,s=m+n, 刚开始只有体积为s的杯子装满可乐,可以互相倒,问你最少的次数使可乐均分,如果没有结果,输出-1; 分析:直接互相倒就完了,BFS模 ...
- Hdu 5336 XYZ and Drops (bfs 模拟)
题目链接: Hdu 5336 XYZ and Drops 题目描述: 有一个n*m的格子矩阵,在一些小格子里面可能会有一些水珠,每个小水珠都有一个size.现在呢,游戏开始咯,在一个指定的空的小格子里 ...
- POJ 3414 Pots【bfs模拟倒水问题】
链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...
- HDU 1495 非常可乐 BFS 搜索
http://acm.hdu.edu.cn/showproblem.php?pid=1495 题目就不说了, 说说思路! 倒可乐 无非有6种情况: 1. S 向 M 倒 2. S 向 N 倒 3. N ...
- BFS 模拟队列(水题)
BFS 这道题 觉得比较适合BFS新手入门写,也许大家都以为最入门 的BFS题是在二维图上搜索,但是这道题是线性搜索,更加简单 POJ 3278 Catch That Cow Time Limit: ...
- Vladik and Favorite Game CodeForces - 811D (思维+BFS+模拟+交互题)
D. Vladik and Favorite Game time limit per test 2 seconds memory limit per test 256 megabytes input ...
- HDU-1459.非常可乐(BFS )
这道题TLE了很多次,原来一直以为将数字化为最简可以让运算更快,但是去了简化之后才发现,真正耗时的就是化简....还和队友学到了用状态少直接数组模拟刚就能过... 本题大意:给出可乐的体积v1,给出两 ...
随机推荐
- Redis6-sorted set 的介绍
Sort Set排序集合类型(1)介绍和set一样sorted set也是string类型元素的集合,不同的是每个元素都会关联一个权.通过权值可以有序的获取集合中的元素该Sort Set类型适合场合: ...
- Oracle中 union 和 union all 的区别
如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字. union(或称为联合)的作用是将多个结果合并在一起显示出来. union和uni ...
- 解决WordPress邮件无法发送问题
1 安装插件 Wp Mail Bank 2 开启第三方SMTP服务 以163为例:设置 - POP3/SMTP/IMAP 开启,会要求设置授权码 3 配置插件:Wp Mail Bank - sett ...
- Linux系统安装rar压缩软件
将 hebaodans.com 目录打包为 hebaodans.rar # rar a hebaodans.rar ./hebaodans.com/ 解压 hebaodans.rar 到当前目录 # ...
- jQuery判断滚动条是上滚还是下滚,且是否到达底部或顶部
jQuery判断滚动条是上滚还是下滚,且是否到达底部或顶部:http://www.haorooms.com/post/jquery_scroll_upanddown //滚动条滚动加载更多内容 //判 ...
- 2016年团体程序设计天梯赛-决赛 L1-3. 出租(20)
下面是新浪微博上曾经很火的一张图: 一时间网上一片求救声,急问这个怎么破.其实这段代码很简单,index数组就是arr数组的下标,index[0]=2 对应 arr[2]=1,index[1]=0 对 ...
- ubuntu环境下jdk安装及jenkins安装
本文内容参考http://jingyan.baidu.com/article/c33e3f48a3365dea15cbb5c9.html 1 jdk下载 安装 http://www.oracle.co ...
- php基础(七)文件
PHP 文件处理 PHP 操作文件 当您操作文件时必须非常小心.如果您操作失误,可能会造成非常严重的破坏.常见的错误是: 编辑错误的文件 被垃圾数据填满硬盘 意外删除文件内容 PHP readfile ...
- mac下安装composer
打开命令后 cd /usr/local/bin 然后执行 curl -sS https://getcomposer.org/installer | php 接下来 sudo mv composer.p ...
- openwrt 的 inittab
文件位于 /etc/inittab, 内容如下: root@hbg:/# cat /etc/inittab ::sysinit:/etc/init.d/rcS S boot::shutdown:/et ...