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 ...
随机推荐
- 路飞学城详细步骤 part2
一 显示课程列表 需求:当你点击课程,course.vue在 <router-view>渲染,并不需要你进行其他点击,所欲的课程列表直接在前端显示,数据是从数据库拿到的. 补充1:生命周期 ...
- 解决 IDEA 中src下xml等资源文件无法读取的问题
该问题的实质是,idea对classpath的规定. 在eclipse中,把资源文件放在src文件夹下,是可以找到的: 但是在idea中,直接把资源文件放在src文件夹下,如果不进行设置,是不能被找到 ...
- ftrace 提供的工具函数
内核头文件 include/linux/kernel.h 中描述了 ftrace 提供的工具函数的原型,这些函数包括 trace_printk.tracing_on/tracing_off 等.本文通 ...
- Virtualization基础
官方文档学习 https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/pdf/Virtualization_G ...
- 并发安全问题之HashMap
原文地址: http://my.oschina.net/xianggao/blog/393990#OSC_h2_1 目录[-] 并发问题的症状 多线程put后可能导致get死循环 多线程put的时候可 ...
- js判断手机的横竖屏调整样式
在移动端,我们经常遇到横竖屏的问题,所以我们改如何判断或针对横竖屏来写代码呢.首先需要在head中加入如下代码: <meta name="viewport" content= ...
- (11)UML设计视图
UML的词汇表包含三种构造块:事物.关系和图 事物:事物是对模型中最具有代表性的成分的抽象 关系:把事物结合在一起 图:图聚集了相关的事物 一.事物 UML中有4种事物 (1)结构事物 UML 模型中 ...
- Struts2的值栈和OGNL牛逼啊
Struts2的值栈和OGNL牛逼啊 一 值栈简介: 值栈是对应每个请求对象的一套内存数据的封装,Struts2会给每个请求创建一个新的值栈,值栈能够线程安全的为每个请求提供公共的数据存取服务. 二 ...
- db2 获取自增主键的方法
1.用SEQUENCES方式 建表语句 CREATE TABLE TEST1( PKEY INTEGER NOT NULL, NAME VARCHAR(100), SEX VARCHAR(100), ...
- How to fill the background with image in landscape in IOS? 如何使image水平铺满屏幕
UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:self.view.frame]; [backgroundIm ...