HDU1495 非常可乐 —— BFS + 模拟
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1495
非常可乐
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 16814 Accepted Submission(s): 6805
4 1 3
0 0 0
3
题解:
1.由于每个容器不超过100, 所以枚举所有状态(1e6)也不会超时。所以直接用BFS。
2.判重:
2.1:其中S<=100, 1<=N<100, 1<=M<100,所以可以将当前三个容器的可乐量压缩成一个int类型,且最大不超过1009999(S放在前面,如果放在中间或者后面,就要加多一位了,这里有讲究),然后就可以开个vis[]数组直接检查状态status是否已经访问过了。
2.2:其实不用把三个容器压缩成一个int类型,可以直接开个三维数组vis[110][110][110],每一维对应一个容器,也是1e6级别的大小,而且更加方便灵活。
2.3:其实开二维数组就够了,因为总的可乐量是确定的,当其中两个容器的可乐量确定了,那剩下的容器的可乐量也就确定了。这一个简单的优化又把内存消耗降低了100倍。
关于判重,目前自己有三个习惯的方法:STL的set、vis多维判重 与 vis status(int类型的以为判重) 。
1. set判重,适用状态高度离散的判重(开数组不能满足其范围), 特点慢,能用数组判重就尽量不要用set判重。
2.vis多维判重, 适用于状态集中且维度较少的情况。
3.staus判重,适用于状态集中且维度较多的情况, 其本质就是把所有维度的状态都压缩成一个int类型, 所以status本质是一个哈希值。
2.1(status判重):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = 2e6+; //0为容器S, 1为容器N, 2为容器M
struct node
{
int status, con[], step; //con[i]为容器i当前盛的可乐体积
};
int vol[], vis[MAXN]; //vol[i]为容器i的容量 queue<node>que;
int bfs()
{
ms(vis,);
while(!que.empty()) que.pop(); node now, tmp;
now.con[] = vol[]; //初始状态只用容器S盛有可乐
now.con[] = now.con[] = ;
now.status = vol[]*; //初始的状态
now.step = ;
vis[now.status] = ;
que.push(now); while(!que.empty())
{
now = que.front();
que.pop(); // cout<< now.status <<endl;
if((now.con[]==vol[]/ && now.con[]==vol[]/)
|| (now.con[]==vol[]/ && now.con[]==vol[]/)
|| (now.con[]==vol[]/ && now.con[]==vol[]/))
return now.step; for(int i = ; i<; i++) //模拟倒水的过程, i为倒, j为被倒
for(int j = ; j<; j++)
{
if(i==j) continue;
tmp = now;
int pour = min(tmp.con[i], vol[j]-tmp.con[j]); //能倒多少水
tmp.con[j] += pour;
tmp.con[i] -= pour;
tmp.status = tmp.con[]*+tmp.con[]*+tmp.con[]; //更新状态
if(!vis[tmp.status])
{
vis[tmp.status] = ;
tmp.step++;
que.push(tmp);
}
}
}
return -;
} int main()
{
while(scanf("%d%d%d",&vol[], &vol[], &vol[]) && (vol[]||vol[]||vol[]))
{
if(vol[]%) //奇数肯定不能平分
{
printf("NO\n");
continue;
}
int ans = bfs();
if(ans==-)
printf("NO\n");
else
printf("%d\n", ans);
}
return ;
}
2.2(三维判重):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; struct node
{
int con[], step;
};
int vol[], vis[MAXN][MAXN][MAXN]; queue<node>que;
int bfs()
{
ms(vis,);
while(!que.empty()) que.pop(); node now, tmp;
now.con[] = vol[];
now.con[] = now.con[] = ;
now.step = ;
vis[now.con[]][now.con[]][now.con[]] = ;
que.push(now); while(!que.empty())
{
now = que.front();
que.pop(); if((now.con[]==vol[]/ && now.con[]==vol[]/)
|| (now.con[]==vol[]/ && now.con[]==vol[]/)
|| (now.con[]==vol[]/ && now.con[]==vol[]/))
return now.step; for(int i = ; i<; i++) //模拟倒水的过程
for(int j = ; j<; j++)
{
if(i==j) continue;
tmp = now;
int pour = min(tmp.con[i], vol[j]-tmp.con[j]);
tmp.con[j] += pour;
tmp.con[i] -= pour;
if(!vis[tmp.con[]][tmp.con[]][tmp.con[]])
{
vis[tmp.con[]][tmp.con[]][tmp.con[]] = ;
tmp.step++;
que.push(tmp);
}
}
}
return -;
} int main()
{
while(scanf("%d%d%d",&vol[], &vol[], &vol[]) && (vol[]||vol[]||vol[]))
{
if(vol[]%)
{
printf("NO\n");
continue;
}
int ans = bfs();
if(ans==-)
printf("NO\n");
else
printf("%d\n", ans);
}
return ;
}
2.3(二维判重,推荐使用):
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; struct node
{
int con[], step;
};
int vol[], vis[MAXN][MAXN]; queue<node>que;
int bfs()
{
ms(vis,);
while(!que.empty()) que.pop(); node now, tmp;
now.con[] = vol[];
now.con[] = now.con[] = ;
now.step = ;
vis[now.con[]][now.con[]] = ;
que.push(now); while(!que.empty())
{
now = que.front();
que.pop(); if((now.con[]==vol[]/ && now.con[]==vol[]/)
|| (now.con[]==vol[]/ && now.con[]==vol[]/)
|| (now.con[]==vol[]/ && now.con[]==vol[]/))
return now.step; for(int i = ; i<; i++) //模拟倒水的过程
for(int j = ; j<; j++)
{
if(i==j) continue;
tmp = now;
int pour = min(tmp.con[i], vol[j]-tmp.con[j]);
tmp.con[j] += pour;
tmp.con[i] -= pour;
if(!vis[tmp.con[]][tmp.con[]])
{
vis[tmp.con[]][tmp.con[]] = ;
tmp.step++;
que.push(tmp);
}
}
}
return -;
} int main()
{
while(scanf("%d%d%d",&vol[], &vol[], &vol[]) && (vol[]||vol[]||vol[]))
{
if(vol[]%)
{
printf("NO\n");
continue;
}
int ans = bfs();
if(ans==-)
printf("NO\n");
else
printf("%d\n", ans);
}
return ;
}
HDU1495 非常可乐 —— BFS + 模拟的更多相关文章
- hdu_1495_非常可乐(bfs模拟)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题意:不解释 题解:BFS模拟,不过要细心,把所有情况都列举出来,开一个数组记录状态,代码有点长 ...
- 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.现在呢,游戏开始咯,在一个指定的空的小格子里 ...
- HDU-1495 非常可乐(BFS)
广搜的灵活应用题: 非常可乐 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- 非常可乐(杭电hdu1495)bfs
非常可乐 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- HDU1495 非常可乐(BFS/数论)
大家一定觉的运动以后喝可乐是一件很惬意的事情,但是seeyou却不这么认为.因为每次当seeyou买了可乐以后,阿牛就要求和seeyou一起分享这一瓶可乐,而且一定要喝的和seeyou一样多.但see ...
- 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 ...
随机推荐
- HP-Unix安装Memcache问题
安装环境 HP-UX 125RITEC B.11.31 U ia64 0942432495 gcc (GCC) 4.3.1 libevent-1.4.14b-stable.tar.gz memcach ...
- Java线程的学习_线程池
系统启动一个新线程需要很高的成本,因为它涉及与操作系统交互.在这种情况下,使用线程池可以很好地提高性能,尤其是当程序中需要创建大量生存期很短暂的线程时. 线程池在系统启动时即创建大量空闲的线程,程序将 ...
- android的网络访问URL
Thread visitBaiduThread = new Thread(new VisitWebRunnable()); visitBaiduThread.start(); try ...
- spring--路由
@RestController: Spring4之后新加入的注解,原来返回json需要@ResponseBody和@Controller配合. 即@RestController是@ResponseBo ...
- 487. Max Consecutive Ones II
Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at mos ...
- WEB学习-基础知识:列表、表单、div和span、注释、字符实体、HTML废弃标签介绍
列表 无序列表 无序列表,用来表示一个列表的语义,并且每个项目和每个项目之间,是不分先后的. ul就是英语unordered list,“无序列表”的意思. li 就是英语list item , “列 ...
- android studio settings
安装 Android Studio下载地址 http://www.android-studio.org/ 1.配置JDK 2.安装 Android Studio (带SDK) 3.配置 一.Set ...
- git使用快速入门
git简介 git是一种版本控制器,更直白的说,团队开发的时候,管理代码使用的软件 git安装 Windows安装 到 https://www.git-for-windows.github.io/ 下 ...
- 浅谈云网融合与SD-WAN
一.引言 近年来,SD-WAN作为一项新技术在行业应用领域里快速发展,企业对SD-WAN的接受度日渐提升,各厂商也纷纷提出解决方案.随着全球云计算领域的活跃创新和我国云计算发展进入应用普及阶段,越来越 ...
- T1063 合并果子 codevs
http://codevs.cn/problem/1063/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 在一个 ...