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 ...
随机推荐
- msp430入门学习44
msp430的其他十二 msp430入门学习
- msp430项目编程41
msp430综合项目---红外遥控直流电机调速系统41
- 最短路中部分点只能从中任意选取K个问题
题意:给N个点,还有另外m个点(其中只能选K个),求最短路. 思路:在SPFA的基础上,用一个数组来统计,在某点入队时(要拓展其他点了),若该点是m个点中的,则count[i]=原来的+1:若不是,则 ...
- IOC基本理解
什么是IOC? IOC全称为控制反转(Inversion Of Control),别名依赖注入(Dependency Injection). 控制反转即指我们获取依赖的方式发生了反转. 假设存在如下情 ...
- C51单片机头文件和启动文件
STARTUP.A51//启动文件. 清理RAM.设置堆栈等.即执行完start.a51后跳转到.c文件的main函数 <reg51.h> //特殊寄存器的字节地址和位地址,sfr定义字 ...
- luogu P1886 滑动窗口(单调队列
题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array i ...
- Codeforces 518 D Ilya and Escalator
Discription Ilya got tired of sports programming, left university and got a job in the subway. He wa ...
- db2 获取自增主键的方法
1.用SEQUENCES方式 建表语句 CREATE TABLE TEST1( PKEY INTEGER NOT NULL, NAME VARCHAR(100), SEX VARCHAR(100), ...
- WebGIS开发之用openlayers加载离线百度地图
因为项目需要,只有内网环境,没有外网环境,所以需要下载地图瓦片. 一.下载瓦片地图 这个可以自行在网上找一些地图瓦片下载器,下好的瓦片地图是分级的.大概如图这种类型. 二.在地图上显示标记 首先使用o ...
- php 中函数获取可变参数的方法, 这个语法有点像 golang 语言中的
原文呢:http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.strict Onl ...