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. gene Ontology (基因本体论)

    gene ontology为了查找某个研究领域的相关信息,生物学家往往要花费大量的时间,更糟糕的是,不同的生物学数据库可能会使用不同的术语,好比是一些方言一样,这让信息查找更加麻烦,尤其是使得机器查找 ...

  2. django中orm的批量操作

    ORM批量操作 数据模型定义 from django.db import models class Product(models.Model): name = models.CharField(max ...

  3. python 内置函数enumerate()

    enumerate() 函数用于将一个可遍历的数据对象(如列表.元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中.在python 3中返回一个生成器,代码如下: a ...

  4. linux常用命令:touch 命令

    linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. 1.命令格式: touch [选项]... 文件... 2.命令参数: -a    ...

  5. window下安装cross-env解决NODE_ENV ts-node 不是内部或外部命令,也不是可运行的程序 或批处理文件 问题

    window下安装cross-env解决NODE_ENV ts-node 不是内部或外部命令,也不是可运行的程序 或批处理文件 问题 在git bash上启动无法进行调试,采用cross-env后可以 ...

  6. JDBC-day02

    JDBC:数据库连接  java  database connectivity ###properties 属性对象,用于读取*.properties属性配置文件中的数据 -为什么使用:之前写法是把数 ...

  7. Symfony2 学习笔记之系统路由

    mfony2 学习笔记之系统路由   漂亮的URL绝对是一个严肃的web应用程序必须做到的,这种方式使index.php?article_id=57这类的丑陋URL被隐藏,由更受欢迎的像 /read/ ...

  8. Docker学习笔记之保存和共享镜像

    0x00 概述 让 Docker 引以为傲的是它能够实现相比于其他虚拟化软件更快的环境迁移和部署,在这件事情上,轻量级的容器和镜像结构的设计无疑发挥了巨大的作用.通过将容器打包成镜像,再利用体积远小于 ...

  9. Spring Boot(六):如何使用mybatis

    Spring Boot(六):如何使用mybatis orm框架的本质是简化编程中操作数据库的编码,发展到现在基本上就剩两家了,一个是宣称可以不用写一句SQL的hibernate,一个是可以灵活调试动 ...

  10. String类的知识点(不断更新)

    知识点1.String类位于java.lang包中,具有丰富的方法计算字符串的长度.比较字符串.连接字符串.提取字符串2.数组的length是属性,字符串的length()是方法3.import ja ...