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,给出两 ...
随机推荐
- 2017-1-15-libubox analysis
2017-1-15-libubox analysis libubox utils.h utils.h 提供了一些简单的实用工具函数.比如大小端转换,位图操作,编译器属性的封装,连续内存申请函数call ...
- ARM裸板调试思路总结、笔记
1. 点灯 2. 串口打印 3. JTAG调试器3.1 命令行调试 3.2 源码级别的调试前提a. 程序必须已经重定位好,位于它的链接地址a.1 如果程序的链接地址是SDRAM, 使用openocd初 ...
- Maxwell顺态求解器电磁力分析
文源:技术邻 问题描述:求解一段通有正弦交流电的直导线在某一稳态磁场中的受力情况,并简单验证仿真结果. 模型介绍: 如上几何模型中10mm边长立方体代表永磁体,材料属性为材料库中的NdFe35,修改磁 ...
- .net webapi项目中支持session
webapi中默认是不支持session的开启的 需要在Global.asax文件中,添加如下代码 public override void Init() { this.PostAuthenticat ...
- ECMAScript6标准新增加的内容
首选呐,你得了解一下javascript和ECMAScript的关系: 编程语言JavaScript是ECMAScript的实现和扩展,由ECMA(一个类似W3C的标准组织)参与进行标准化.ECMAS ...
- PRG(Post/Redirect/Get)
转自:PRG(Post/Redirect/Get) 摘要: Post/Redirect/Get 简称PRG,是一种用来防止表单重复提交数据的一种Web设计模式 Post/Redirect/Get 简称 ...
- 微信支付错误两个问题的解决:curl出错,错误码:60
如下是运行微信支付测试代码时出错代码: Warning: curl_setopt() expects parameter 2 to be long, string given in D:\wwwroo ...
- (转)提高代码质量---one
1. 摘要 这是烂代码系列的第二篇,在文章中我会跟大家讨论一下如何尽可能高效和客观的评价代码的优劣. 在发布了关于烂代码的那些事(上)之后,发现这篇文章竟然意外的很受欢迎,很多人也描(tu)述(cao ...
- 【LeetCode】463. Island Perimeter
You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...
- hdu_5927_Auxiliary Set(xjb搞)
题目链接:hdu_5927_Auxiliary Set 题意: 给一棵n个节点的树,最开始全部都是重点,现在有q个询问,每次给你一些轻点,并叫你输出整棵树的重点数量, 轻点可能会变为重点,如果这个轻点 ...