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 ...
随机推荐
- Spring Boot SpringSecurity5 身份验证
对于没有访问权限的用户需要转到登录表单页面.要实现访问控制的方法多种多样,可以通过Aop.拦截器实现,也可以通过框架实现(如:Apache Shiro.Spring Security). pom.xm ...
- ado:SqlDataAdapter的两种不同写法,以及SqlCommand的两种不同写法
原文发布时间为:2008-08-01 -- 来源于本人的百度文章 [由搬家工具导入] SqlDataAdapter:(它是自动打开连接且自动关闭的,所以可以不必显示打开关闭连接) SqlConnect ...
- 【ztree】zTree取消树节点选中的背景色
点击树节点的时候是ztree给树加了个class: curSelectedNode 所以最简单的清除树节点的背景色的方法是移除其有背景色的class: $(".curSelectedN ...
- MongoDB_单机版环境搭建
linux上安装MogoDB http://www.runoob.com/mongodb/mongodb-linux-install.html 在https://www.mongodb.com/dow ...
- Powerdesigner 使用小技巧
1.table与table之间:改直角为直线; 2.Name 和code 不联动
- 一个关于 jquery 和 php 的 jsonp 例子(与后台PHP成功通信)
<script> $(document).ready(function(){ $.ajax({ url:'http://localhost/test/jsonp.php', dataTyp ...
- HTTP请求的缓存(Cache)机制
原文地址:http://small.aiweimeng.top/index.php/archives/58.html 先来一张图: ####下面简单的来描述一下HTTP Cache机制: 当资源资源第 ...
- 2019年北航OO第3单元(JML)总结
1 JML语言的理论基础及应用工具链 1.1 JML语言 Java建模语言(JML)是一种行为接口规范语言,可用于指定Java模块的行为.它结合了Eiffel的"契约设计(design by ...
- 让win7任务条上的文件夹打开是c,d,e,f而不是库
如果资源管理器是打开的,则右键点击资源管理器的图标,在跳出的菜单中,右键点击“Windows资源管理器”,选择“属性”. 在“快捷方式’选项卡,“目标”一栏,默认的是 %windir%\explore ...
- 团购类网站倒计时的js实现
一.如火如荼的团购网站 根据易观国际提供的统计数据,截至2010年6月,中国市场团购网站数量已经突破400家.国内团购潮从今年2月份开始出现,在4~6月出现高峰,尤其是今年5月,一些大的网站如爱帮网. ...