POJ 3414 Pots(罐子)
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:
- 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.
给定两个灌子,容量分别为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 A, B, 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(罐子)的更多相关文章
- 广搜+输出路径 POJ 3414 Pots
POJ 3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13547 Accepted: 5718 ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- BFS POJ 3414 Pots
题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...
- POJ 3414 Pots
Pots Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- poj 3414 Pots【bfs+回溯路径 正向输出】
题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ 3414 Pots【bfs模拟倒水问题】
链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...
- poj 3414 Pots(广搜BFS+路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:id=3414">http://poj.org/probl ...
- poj 3414 Pots ( bfs )
题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i) fill the ...
- POJ 3414 Pots bfs打印方案
题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...
随机推荐
- Vue 的路由实现 Hash模式 和 History模式
Hash 模式: Hash 模式的工作原理是onhashchange事件,Window对象可以监听这个事件... 可以通过改变路径的哈希值,来实现历史记录的保存,发生变化的hash 都会被浏览器给保存 ...
- tcp/ip 3次握手和4次挥手
tcp/ip 3次握手和4次挥手
- 使用Groovy+Spock轻松写出更简洁的单测
当无法避免做一件事时,那就让它变得更简单. 概述 单测是规范的软件开发流程中的必不可少的环节之一.再伟大的程序员也难以避免自己不犯错,不写出有BUG的程序.单测就是用来检测BUG的.Java阵营中,J ...
- vue-cli项目npm run build后,index.html无法在浏览器打开
- tensorflow学习6
g_w1 = tf.get_variable('g_w1', [z_dim, 3136], dtype=tf.float32, initializer=tf.truncated_normal_init ...
- Android Auto开发初探
一.Android Auto 概述 二.Android Auto 使用方法 四.Android Auto应用开发 五.Android Auto开发总结 一.Android Auto 概述 最近物联网是 ...
- AtCoder Beginner Contest 088 (ABC)
A - Infinite Coins 题目链接:https://abc088.contest.atcoder.jp/tasks/abc088_a Time limit : 2sec / Memory ...
- python shutil模块简单介绍
python shutil模块简单介绍 简介 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作. shutil 模块方法: copy(src, ...
- The Little Prince-summary
The Little Prince-summary 这些年 ”寂寞”这个词使用频率越来越高 这些年 不管有钱没钱 有对象没对象的人 入夜时分总是心里空空 不知生活的意义是什么 我们不喜欢一座城市 对一 ...
- Eclipse在Debug模式下经常进入ThreadPoolExecutor解决办法
1.进入Window-->搜索:debug