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> # ...
随机推荐
- SoapUI link
1. Groovy language: http://docs.groovy-lang.org/latest/html/documentation/index.html http://doc ...
- python os.path.join()
>>> import os >>> path = '/Users/beazley/Data/data.csv' >>> # Get the las ...
- 任务调度工具 Apache Airflow 初识
参考文章: Apache Airflow (incubating) Documentation — Airflow ... 任务调度神器 airflow 之初体验 airflow 介绍 - 简书(原文 ...
- 20165215 2017-2018-2 《Java程序设计》第6周学习总结
20165215 2017-2018-2 <Java程序设计>第6周学习总结 教材学习内容总结 chapter8 Java把String类定义为final类,即String类不能有子类 用 ...
- ajax 检测用户名是否可用
下面是一个 ajax 检测用户名是否可用的例子. django 项目中. —— views.py 里—— from django.shortcuts import render,HttpRespon ...
- Django 应用 静态文件配置
Django 应用 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- AtCoder Beginner Contest 086 (ABCD)
A - Product 题目链接:https://abc086.contest.atcoder.jp/tasks/abc086_a Time limit : 2sec / Memory limit : ...
- hdu 1466 计算直线的交点数 递推
题目描述 平面上有n条直线,且无三线共点,问这些直线能有多少种不同交点数. 比如,如果n=2,则可能的交点数量为0(平行)或者1(不平行). 输入 输入数据包含多个测试实例,每个测试实例占一行,每行包 ...
- SQL表分区之二
前面说的给表做表分区,现在有个问题,比如上面我们说的是按照20w为一个分割线,那些现在我们想把这个调整下怎么办?难道要把之前的分区函数和分区方案删了,重新新建分区函数和分区方案嘛? 当然,此方式肯定是 ...
- webform 使用富文本编辑器
<div class="form-group"> <label class="col-xs-2 control-label text-right&quo ...