POJ 3414 Pots(罐子)

Time Limit: 1000MS    Memory Limit: 65536K

Description - 题目描述

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

  1. FILL(i)        fill the pot i (1 ≤ ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. 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.

给定两个灌子,容量分别为A与B升。可执行下列操作:
.FILL(i)        将罐子 i ( ≤ i ≤ ) 装满;
.DROP(i)      将罐子 i 倒空;
.POUR(i,j)    将罐子 i 倒向罐子 j; 此操作后罐子 j 可能被装满 (罐子 i 中可能还剩一些水), 或者罐子 i 为空 (所有东西都被倒入罐子 j).

CN

Input - 输入

  On the first and only line are the numbers AB, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

输入的第一行也是唯一一行,有三个数A,B和C。所有整数都在1到100间,且C≤max(A,B)。

CN

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’.

输出的第一行必须包含操作序列长度K。
随后K行必须每行描述一个操作。如果有多种最短序列,输出任意一种。
如果无法得出结果,输出的第一行也是唯一一行则为‘impossible’。

CN

Sample Input - 输入样例

3 5 4

Sample Output - 输出样例

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

题解

  因为需要找最短的操作,不能直接上DFS了。
  典型的BFS套路。注意步骤的回溯即可。
  (作为懒人直接丢vector……)

代码 C++

 #include <cstdio>
#include <cstring>
#include <queue>
struct OPR{
int a, b, len;
std::vector<int> op;
void popOP(int sum){
while (sum--) op.pop_back();
}
void pushOP(int i, int j, int k){
op.push_back(i); op.push_back(j);
if (i == ) op.push_back(k);
}
}opt; int len[];
std::queue<OPR> q;
void qPush(OPR now){
if (now.len < len[now.a * + now.b]){
len[now.a * + now.b] = now.len;
q.push(now);
}
} int main(){
int aMX, bMX, c, tmpA, tmpB, i;
memset(len, 0x7F, sizeof len); opt.len = len[];
scanf("%d%d%d", &aMX, &bMX, &c);
OPR now = { , , }; q.push(now);
while (!q.empty()){
now = q.front(); q.pop();
if (now.a == c || now.b == c){
if (now.len < opt.len) opt = now;
continue;
}
++now.len;
tmpA = now.a; tmpB = now.b; if (tmpA != aMX){
now.a = aMX;
now.pushOP(, , -); qPush(now);
now.a = tmpA; now.popOP();
}
if (now.b != bMX){
now.b = bMX;
now.pushOP(, , -); qPush(now);
now.b = tmpB; now.popOP();
}
if (tmpA){
now.a = ;
now.pushOP(, , -); qPush(now);
now.a = tmpA; now.popOP(); now.b += now.a;
if (now.b > bMX) now.a = now.b - bMX, now.b = bMX;
else now.a = ;
now.pushOP(, , ); qPush(now);
now.a = tmpA; now.b = tmpB; now.popOP();
}
if (now.b){
now.b = ;
now.pushOP(, , -); qPush(now);
now.b = tmpB; now.popOP(); now.a += now.b;
if (now.a > aMX) now.b = now.a - aMX, now.a = aMX;
else now.b = ;
now.pushOP(, , ); qPush(now);
now.a = tmpA; now.b = tmpB; now.popOP();
}
} if (opt.len == len[]) puts("impossible");
else{
printf("%d\n", opt.len);
for (i = ; i < opt.op.size();){
switch (opt.op[i]){
case : printf("FILL(%d)\n", opt.op[i + ]); i += ; break;
case : printf("DROP(%d)\n", opt.op[i + ]); i += ; break;
default:printf("POUR(%d,%d)\n", opt.op[i + ], opt.op[i + ]); i += ; break;
}
}
}
return ;
}

POJ 3414 Pots(罐子)的更多相关文章

  1. 广搜+输出路径 POJ 3414 Pots

    POJ 3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13547   Accepted: 5718   ...

  2. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

  3. BFS POJ 3414 Pots

    题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...

  4. POJ 3414 Pots

    Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status  ...

  5. poj 3414 Pots【bfs+回溯路径 正向输出】

    题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  6. POJ 3414 Pots【bfs模拟倒水问题】

    链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...

  7. poj 3414 Pots(广搜BFS+路径输出)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:id=3414">http://poj.org/probl ...

  8. poj 3414 Pots ( bfs )

    题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i)        fill the ...

  9. POJ 3414 Pots bfs打印方案

    题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...

随机推荐

  1. 解决caffe绘制训练过程的loss和accuracy曲线时候报错:paste: aux4.txt: 没有那个文件或目录 rm: 无法删除"aux4.txt": 没有那个文件或目录

    我用的是faster-rcnn,在绘制训练过程的loss和accuracy曲线时候,抛出如下错误,在网上查找无数大牛博客后无果,自己稍微看了下代码,发现,extract_seconds.py文件的 g ...

  2. ad 原件布局布线基本规则

    一.原件布局基本规则 1.按照电路模块进行布局,电路中的元件应该采用集中就近原则,同时数字电路和模拟电路分开: 2.定位孔.标准孔等周围1.27mm内不得贴元器件,安装孔周围3.5mm不得特装元件 3 ...

  3. html5实现移动端下拉刷新(原理和代码)

    这篇文章给大家介绍的内容是关于html5实现移动端下拉刷新(原理和代码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 移动端的下拉刷新是一个很常见的功能,也有许多开源库实现了这个功 ...

  4. Elasticsearch集群内的原理

        一个运行中的 Elasticsearch 实例称为一个 节点,而集群是由一个或者多个拥有相同 cluster.name 配置的节点组成, 它们共同承担数据和负载的压力.当有节点加入集群中或者从 ...

  5. The Little Prince-12/02

    The Little Prince-12/02 What moves me so deeply, about this little prince who is sleeping here, is h ...

  6. springmvc请求路径和请求参数的获取注解- @PathVariable和@RequestParam

    @PathVariable和@RequestParam @PathVariable是从路径里面去获取变量,也就是把路径当做变量. @RequestParam是从请求里面获取参数. 如:url:http ...

  7. 基于spring框架的jt项目分页查询知识点(二)

    知识点汇总 1. 日志记录方法 private Logger log= Logger.getLogger(SysLogServiceImpl.class.getName()); 记录SysLogSer ...

  8. 对gulp的理解和使用(一)

    说的gulp,到底是什么?用来做什么的? 以前并没有想过这个问题,拿到公司的项目脚手架就开始做事情了.现在专门来总结一下. gulp干什么的呢? gulp是node中的一种代码构建工具,还有就是项目自 ...

  9. The logback manual #01# Introduction

    依赖包如下pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...

  10. bzoj 3083

    bzoj 3083 树链剖分,换根 对于一颗树有以下操作 1.确定x为根,2.将u到v的简单路径上的数全改成c,3.询问当前以x为根的子树中的节点最小权值. 如果没有操作1:树链剖分很明显. 于是考虑 ...