Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17456   Accepted: 7407   Special Judge

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.

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

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

Sample Input

3 5 4

Sample Output

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

Source

Northeastern Europe 2002, Western Subregion
思路:和非常可乐很像,需要输出过程。
代码:
 #include "cstdio"
#include "stdlib.h"
#include "iostream"
#include "algorithm"
#include "string"
#include "cstring"
#include "queue"
#include "cmath"
#include "vector"
#include "map"
#include "set"
#define mj
#define db double
#define ll long long
using namespace std;
const int N=1e2+;
int cas=;
int a,b,c;
int ok=;
int v[N][N];
char s[][]={"","FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
struct P
{
int a,b,t;
char s[];//记录操作
}; void bfs()
{
queue<P>q;
P p,u;
p.a=,p.b=,p.t=,p.s[]='';
v[p.a][p.b]=;
q.push(p);
while(!q.empty()){
u=q.front();
q.pop();
if(u.a==c || u.b==c){
ok=;
printf("%d\n",u.t);
for(int i=;i<u.t;i++){
printf("%s\n",s[u.s[i]-'']);//输出操作
}
return ;
}
if(u.a<a){
p.a=a;
p.b=u.b;
p.t=u.t+;
strcpy(p.s,u.s);//复制之前的操作
p.s[u.t]=+'';
if(!v[p.a][p.b]){
v[p.a][p.b]=;
q.push(p);
}
}
if(u.b<b){
p.b=b;
p.a=u.a;
p.t=u.t+;
strcpy(p.s,u.s);
p.s[u.t]=+'';
if(!v[p.a][p.b]){
v[p.a][p.b]=;
q.push(p);
}
}
if(u.a!=){
p.a=;
p.b=u.b;
p.t=u.t+;
strcpy(p.s,u.s);
p.s[u.t]=+'';
if(!v[p.a][p.b]){
v[p.a][p.b]=;
q.push(p);
}
}
if(u.b!=){
p.b=;
p.a=u.a;
p.t=u.t+;
strcpy(p.s,u.s);
p.s[u.t]=+'';
if(!v[p.a][p.b]){
v[p.a][p.b]=;
q.push(p);
}
}
if(u.a!= && u.b<b){
if(b-u.b>=u.a){
p.b=u.a+u.b;
p.a=;
}
else{
p.a=u.a+u.b-b;
p.b=b;
}
p.t=u.t+;
strcpy(p.s,u.s);
p.s[u.t]=+'';
if(!v[p.a][p.b]){
v[p.a][p.b]=;
q.push(p);
}
}
if(u.b!= && u.a<a){
if(a-u.a>=u.b){
p.a=u.a+u.b;
p.b=;
}
else{
p.b=u.a+u.b-a;
p.a=a;
}
p.t=u.t+;
strcpy(p.s,u.s);
p.s[u.t]=+'';
if(!v[p.a][p.b]){
v[p.a][p.b]=;
q.push(p);
}
}
}
}
int main()
{
scanf("%d %d %d",&a,&b,&c);
memset(v,, sizeof(v));
bfs();
if(!ok) printf("impossible\n");
return ;
}
 

POJ 3414 BFS 输出过程的更多相关文章

  1. POJ - 3414 bfs [kuangbin带你飞]专题一

    状态搜索,每种状态下面共有六种选择,将搜索到的状态保存即可. d[i][j]表示状态A杯中水i升,B杯中水j升,总状态数量不会超过A杯的容量 * B杯的容量. AC代码 #include<cst ...

  2. 【BFS】POJ 3414

    直达 -> POJ 3414 Pots 相似题联动–>HDU 1495 非常可乐 题意:两个壶倒水,三种操作,两个桶其中一个满足等于C的最少操作,输出路径.注意a,b互倒的时候能不能倒满, ...

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

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

  4. BFS POJ 3414 Pots

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

  5. Pots(POJ - 3414)【BFS 寻找最短路+路径输出】

    Pots(POJ - 3414) 题目链接 算法 BFS 1.这道题问的是给你两个体积分别为A和B的容器,你对它们有三种操作,一种是装满其中一个瓶子,另一种是把其中一个瓶子的水都倒掉,还有一种就是把其 ...

  6. POJ 3414 Pots

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

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

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

  8. POJ 3414 Pots(罐子)

    POJ 3414 Pots(罐子) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 You are given two po ...

  9. Pots POJ 3414

    /* *POJ 3414 *简单模板bfs *编程应该为了方便理解,尽量提供接口 */ #include<cstdio> #include<algorithm> #includ ...

随机推荐

  1. RS485相关学习

    TIA-485-A (Revision of EIA-485) Standard ANSI/TIA/EIA-485-A-1998Approved: March 3, 1998Reaffirmed: M ...

  2. Web框架的应用

    从今天开始,我们将要学习有关Web框架的一些内容,在学习之前先来学习一下http协议,即基于http是如何通信的. http 概要:http是基于tcp/ip通信协议来传输数据的. 优点: 1.简单快 ...

  3. jquery进阶(1)

    今天我们接着来学习jQuery中的内容,包括css的操作.尺寸的操作.文档的操作.动画(有待补充),事件处理操作. 一.CSS 在css中可以设置css的基本属性 - .css("color ...

  4. 从零开始的全栈工程师——js篇2.16

    js操作css样式 div.style.width=“200px” 在div标签内我们添加了一个style属性 并设定了width值 这种写法会给标签带来了大量的style属性 跟实际项目是不符的 我 ...

  5. EasyUI Combobox 的 onChange,onSelect,onClick 事件

    EasyUI 中 Combobox 选项发生改变时会触发 onChange,onSelect,onClick,3 个事件.最近要做一个级联的 Combo 菜单,类似于选择地址时让用户填写省,市,区的菜 ...

  6. mysql decimal类型与decimal长度用法详解

    三者的区别介绍 float:浮点型,含字节数为4,32bit,数值范围为-3.4E38~3.4E38(7个有效位) double:双精度实型,含字节数为8,64bit数值范围-1.7E308~1.7E ...

  7. 【Java/Android性能优 7】Android公共库——图片缓存 网络缓存 下拉及底部更多ListView 公共类

    本文转自:http://www.trinea.cn/android/android-common-lib/ 介绍总结的一些android公共库,包含缓存(图片缓存.预取缓存.网络缓存).公共View( ...

  8. ArcSDE空间数据库中SDE用户使用探讨 (转载)

    ArcSDE作为空间数据库解决方案,应用非常广泛,本短文将尝试描述SDE的工作机制,简要说明空间数据库中SDE用户的使用方法.ArcSDE如何工作ArcSDE属于中间件技术,其本身并不能够存储空间数据 ...

  9. zip man man.config

    zip man man.config zip -r zip1 man.config man.zip gzip a tar -cvf test.tar /home/* tar -tf test.tar ...

  10. firefox 提示 ssl_error_unsupported_version 的解决方法

    访问一些HTTPS网站时尤其是国内网站 中文提示: 无法安全地连接 Firefox 无法保证您在 sx.ac.10086.cn 上的数据安全性,因为它使用 SSLv3,一个目前安全性欠佳的安全协议.专 ...