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. spiderUI窗口过小解决

    复制以下代码,直接替换此css样式即可: C:\Users\Administrator\AppData\Local\Programs\Python\Python37\Lib\site-packages ...

  2. python os.path.expanduser()

    # Expand the user's home directory

  3. ResourceBundle与Properties读取配置文件

    ResourceBundle与Properties的区别在于ResourceBundle通常是用于国际化的属性配置文件读取,Properties则是一般的属性配置文件读取. ResourceBundl ...

  4. [转载]SQL Server中的事务与锁

    了解事务和锁 事务:保持逻辑数据一致性与可恢复性,必不可少的利器. 锁:多用户访问同一数据库资源时,对访问的先后次序权限管理的一种机制,没有他事务或许将会一塌糊涂,不能保证数据的安全正确读写. 死锁: ...

  5. Lambda表达式select()和where()的区别

    可能很多同学和我一样对于select()和where()区别并不是太清晰,其实两者还是有本质区别的. 1.where()用法:必须加条件,且返回对象结果. static void Main(strin ...

  6. Different between MB SD Connect Compact 5 and MB SD C4 Star Diagnostic Tool

    MB SD C4 Star Diagnostic Tool is the professional MB Star Diagnostic Tools for benz cars and trucks. ...

  7. P1383 高级打字机

    P1383 高级打字机 主席树 一发主席树解决. 插入操作十分显然. 撤销操作复制前面的版本就行. 询问操作十分显然. #include<iostream> #include<cst ...

  8. Iris Classification on Tensorflow

    Iris Classification on Tensorflow Neural Network formula derivation \[ \begin{align} a & = x \cd ...

  9. 06: 字典、顺序表、列表、hash树 实现原理

    算法其他篇 目录: 1.1 python中字典对象实现原理 1.2 顺序表 1.3 python 列表(list) 1.1 python中字典对象实现原理返回顶部   注:字典类型是Python中最常 ...

  10. phpstorm中设置代码上传到github

    参考: https://blog.csdn.net/Knight_quan/article/details/54894691 https://www.300168.com/biancheng/show ...