POJ3414 Pots —— BFS + 模拟
题目链接:http://poj.org/problem?id=3414
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 18550 | Accepted: 7843 | Special Judge |
Description
You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:
- FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
- DROP(i) empty the pot i to the drain;
- POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its
contents have been moved to the pot j).
Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.
Input
On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).
Output
The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the
desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.
Sample Input
3 5 4
Sample Output
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
Source
题解:
类似的题目,也是有关几个容器倒来倒去,然后求一个目标量:http://blog.csdn.net/dolfamingo/article/details/77804030
此题不过是多了个路径输出,队列把STL换成数组就可以了,不细述。
代码如下:
#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
{
//con[i]为容器i当前的量;op存操作的信息;pre为上一步所在队列的下标(为了输出路径)
int con[], op[], step, pre;
};
int vis[MAXN][MAXN], vol[]; //vol[0]、vol[1]为两个容器的容量, vol[3]为目标量 node que[]; //由于要输出路径,就要用数组来作队列了
int front, rear;
int bfs()
{
ms(vis,);
front = rear = ; node now, tmp;
now.con[] = now.con[] = ;
now.step = ;
vis[][] = ;
que[rear++] = now; while(front!=rear)
{
now = que[front++];
if(now.con[]==vol[] || now.con[]==vol[])
return front-; for(int i = ; i<; i++)
{
tmp = now; //操作1:FILL
tmp.con[i] = vol[i];
if(!vis[tmp.con[]][tmp.con[]])
{
vis[tmp.con[]][tmp.con[]] = ;
tmp.op[] = ; tmp.op[] = i;
tmp.step = now.step+;
tmp.pre = front-;
que[rear++] = tmp;
} tmp = now; //操作2:DROP
tmp.con[i] = ;
if(!vis[tmp.con[]][tmp.con[]])
{
vis[tmp.con[]][tmp.con[]] = ;
tmp.op[] = ; tmp.op[] = i;
tmp.step = now.step+;
tmp.pre = front-;
que[rear++] = tmp;
} tmp = now; //操作三POUR
int j = !i; //另外一个容器
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.op[] = ; tmp.op[] = i; tmp.op[] = j;
tmp.step = now.step+;
tmp.pre = front-;
que[rear++] = tmp;
}
}
}
return -;
} void Print(int i) //输出路径
{
if(que[i].pre!=)
Print(que[i].pre); if(que[i].op[]==)
printf("FILL(%d)\n", que[i].op[]+);
else if(que[i].op[]==)
printf("DROP(%d)\n", que[i].op[]+);
else
printf("POUR(%d,%d)\n", que[i].op[]+, que[i].op[]+);
} int main()
{
while(scanf("%d%d%d",&vol[], &vol[], &vol[])!=EOF)
{
int i = bfs();
if(i==-)
puts("impossible");
else
{
printf("%d\n",que[i].step);
Print(i);
}
}
}
POJ3414 Pots —— BFS + 模拟的更多相关文章
- POJ3414—Pots(bfs加回溯)
http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memor ...
- poj 3414 Pots bfs+模拟
#include<iostream> #include<cstring> #define fillA 1 #define pourAB 2 #define dropA 3 #d ...
- POJ-3414.Pots.(BFS + 路径打印)
这道题做了很长时间,一开始上课的时候手写代码,所以想到了很多细节,但是创客手打代码的时候由于疏忽又未将pair赋初值,导致一直输出错误,以后自己写代码可以专心一点,可能会在宿舍图书馆或者Myhome, ...
- POJ3414 Pots BFS搜素
题意:通过题目给出的三种操作,让任意一个杯子中的水到达一定量 分析:两个杯子最大容量是100,所以开个100*100的数组记录状态,最多1w个状态,所以复杂度很低,然后记录一下路径就好 注:代码写残了 ...
- POJ 3414 Pots (BFS/DFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7783 Accepted: 3261 Special Ju ...
- BFS+模拟 ZOJ 3865 Superbot
题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...
- hdu_1495_非常可乐(bfs模拟)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1495 题意:不解释 题解: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 ...
随机推荐
- PEP8 Python编码规范(转)
一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车.3 类 ...
- Docker-PostgresSQL
Postgresql Docker安装运行 mac环境: 1.拉取官方镜像,并创建容器 zhoumatoMacBook-Pro:~ zhou$ docker search postgresql NA ...
- Windows Phone 8.1 开发实例 网络编程 天气预报
首先感谢林政老师的博客,给了我很大的指导. 准备工作 我的开发环境: - Visual Studio 2013(With Update 4) - Windows Phone 8.1 - Windows ...
- hadoop+yarn+hbase+storm+kafka+spark+zookeeper)高可用集群详细配置
配置 hadoop+yarn+hbase+storm+kafka+spark+zookeeper 高可用集群,同时安装相关组建:JDK,MySQL,Hive,Flume 文章目录 环境介绍 节点介绍 ...
- 4.JAVA语言基础部分—枚举与泛型
枚举 //定义枚举 enum MyEnum{ ITEM_A, ITEM_B } public static void main(String[] args) { //values()获取所枚举项的集合 ...
- django : related_name and related_query_name
This post is about two Django ForeignKey parameters related_name related_query_name See an example b ...
- ASP.NET Core 如何记录每次响应的Response信息 - sky 胡萝卜星星 - CSDN博客
原文:ASP.NET Core 如何记录每次响应的Response信息 - sky 胡萝卜星星 - CSDN博客 上一篇文章中我们已经成功的记录了Request部分的信息,现在我们来看下如何记录Res ...
- python遍历两个列表,若长度不等,用None填充
zip经常会遇到截断问题,如:a = [1,2,3], b = [4,5,6,7],则zip(a,b) = [(1, 4), (2, 5), (3, 6)] 可考虑使用map: map(lambda ...
- VC++_错误 无法打开包括文件“glglut.h” No such file or directory 怎么办
在网上看到类似的问题,查找资料找到了解决方案,现整理如下,有些更改,好让自己多些印象,附原文网址:http://blog.csdn.net/bigloomy/article/details/62265 ...
- 进程监控模块配置与使用 ------ACE(开源项目)
下面我先从此工程的作用讲起: 此工程适用于程序异常退出,然后自动重启该程序.对于,系统重启不了该进程,那此程序将返回-1,也无法进行下一步工作. 下面,先从配置开始讲起: 参考资料:http://hi ...