Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 22688   Accepted: 9626   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

题意:

有二个水壶,对水壶有三种操作:

1)FILL(i),将i水壶的水填满;

2)DROP(i),将水壶i中的水全部倒掉;

3)POUR(i,j)将水壶i中的水倒到水壶j中,若水壶 j 满了,则 i 剩下的就不倒了,问进行多少步操作,并且怎么操作,输出操作的步骤,两个水壶中的水可以达到C这个水量。如果不可能则输出impossible。初始时两个水壶是空的,没有水。

思路:

模拟一下,然后如果当前的状态已经出现过了就说明不可以这样子,必须要用其他操作,这个和poj3087的题目有点像,这里还需要储存一个路径,这个和poj3984有点像,poj3984之前的博客里面用了递归的方式输出路径,这一回用了栈,两种方法应该都可以做的,具体的看代码吧,注释已经很清楚了

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define _e exp(1.0)
#define ll long long
const int maxn=;
struct cup
{
int x,y; //a和b的当前水的状态
int step;
int flag; //标记操作,是操作几
cup *pre; //记录路径的玩意儿
};
queue<cup>que;
stack<int>R;
int a,b,e;
int vis[maxn][maxn]={}; //记录当前的状态是否到达过
int ans; void bfs(int x,int y)
{
cup c;
cup t[]; //目前瓶子里剩余的水量
c.x=;
c.y=;
c.flag=;
c.pre=NULL;
c.step=;
que.push(c);
vis[x][y]=;
int count=-;
while(!que.empty())
{
count++;
t[count]=que.front();
que.pop();
for(int i=;i<=;i++)
{
switch(i)
{
case : //fill a
c.x=a;
c.y=t[count].y;
c.flag=;
break;
case : //fill b
c.x=t[count].x;
c.y=b;
c.flag=;
break;
case : //drop a
c.x=;
c.y=t[count].y;
c.flag=;
break;
case : //drop b
c.x=t[count].x;
c.y=;
c.flag=;
break;
case : //pour a to b
if(t[count].x>b-t[count].y) //a可以装满b
{
c.x=t[count].x-(b-t[count].y);
c.y=b;
}
else //a不能装满b
{
c.x=;
c.y=t[count].y+t[count].x;
}
c.flag=;
break;
case : //pour b to a
if(t[count].y>a-t[count].x) //b可以装满a
{
c.y=t[count].y-(a-t[count].x);
c.x=a;
}
else //b不可以装满a
{
c.x=t[count].x+t[count].y;
c.y=;
}
c.flag=;
break;
}
if(vis[c.x][c.y])
continue;
vis[c.x][c.y]=;
c.step=t[count].step+;
c.pre=&t[count];
if(c.x==e || c.y==e)
{
ans=c.step;
while(c.pre)
{
R.push(c.flag);
c=*c.pre;
}
return;
}
que.push(c);
}
}
}
void print()
{
while(!R.empty())
{
int i=R.top();
R.pop();
switch(i)
{
case :cout<<"FILL(1)"<<endl;break;
case :cout<<"FILL(2)"<<endl;break;
case :cout<<"DROP(1)"<<endl;break;
case :cout<<"DROP(2)"<<endl;break;
case :cout<<"POUR(1,2)"<<endl;break;
case :cout<<"POUR(2,1)"<<endl;break;
}
}
}
int main()
{
cin>>a>>b>>e;
bfs(,);
if(ans==)
cout<<"impossible"<<endl;
else
{
cout<<ans<<endl;
print();
}
return ;
}

Pots POJ - 3414 (搜索+记录路径)的更多相关文章

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

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

  2. hdu 1664(数论+同余搜索+记录路径)

    Different Digits Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. Pots POJ 3414

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

  4. - 迷宫问题 POJ - 3984 bfs记录路径并输出最短路径

    定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...

  5. kuangbin专题 专题一 简单搜索 Pots POJ - 3414

    题目链接:https://vjudge.net/problem/POJ-3414 题意:给你两个杯子,分别容量为A(1),B(2)和一个C,C是需要经过下列操作,得到的一个升数.(1) FILL(i) ...

  6. 迷宫问题 (bfs广度优先搜索记录路径)

    问题描述: 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, ...

  7. POJ 3414 Pot (输出路径)【BFS】

    <题目链接> 题目大意: 有两个容量的空杯子,能够对这两个空杯子进行三种操作: 分别是fill(a),装满a杯子: drop(a),倒空a杯子: pour(a,b),将a杯子中的水倒入b杯 ...

  8. poj 1787 背包+记录路径

    http://poj.org/problem?id=1787 Charlie's Change Time Limit: 1000MS   Memory Limit: 30000K Total Subm ...

  9. poj 2395 bfs/记录路径

    http://poj.org/problem?id=2935 Basic Wall Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

随机推荐

  1. crawlspider的源码学习

    Spider基本上能做很多事情了,但是如果你想爬取全站的话,可能需要一个更强大的武器.CrawlSpider基于Spider,但是可以说是为全站爬取而生.CrawlSpiders是Spider的派生类 ...

  2. BZOJ 2462 [BeiJing2011]矩阵模板 矩阵哈希

    昨天卡了一天常数...然后发现吧$unsigned\space long\space long$改成$unsigned$就可以过了$qwq$ 先把每一行的前缀哈希求出,然后再竖着把每个前缀哈希值哈希起 ...

  3. Uva1608

    如果一个序列的所有子序列中均存在至少一个元素,这个元素在该子序列中只出现一次,则这个序列non-boring. 当一个序列[x,y]中没有元素只出现一次,那么该序列不符合要求,如果有的话,设为第i个元 ...

  4. java——变量、jvm内存划分

    基本数据变量类型:byte.short.int.long.float.double.boolean.char eg : int i = 1; 引用数据变量类型:数组.类.接口.枚举.注解 eg : S ...

  5. android 开发-系统设置界面的实现

    具体与Preference的用法类似,这里就不做过多解释,直接贴示例代码,需要在res下新建xml文件夹,在xml文件夹下添加xml文件. xml:(注意:root节点是:PreferenceScre ...

  6. Docker | 第四章:Dockerfile简单介绍及使用

    前言 前一章节,介绍了Docker常用的命令.在基本使用上,熟悉这些常用的命令基本上就够了.但在一些场景下,比如在部署SpringBoot应用时,通常我们都是打成Jar包,然后利用java命令进行运行 ...

  7. Redis中String类型的Value最大可以容纳数据长度

    项目中使用redis存储,key-value方式,在Redis中字符串类型的Value最多可以容纳的数据长度是512M 官方信息: A String value can be at max 512 M ...

  8. 设计模式--观察者模式(KVO)

    观察者模式(Observer):观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态上发生变化时,会通知所有观察者对象,使它们能够自动更新自己. 举个例子, ...

  9. C# 调用NPOI 修改Excel 完成实时更新公式结果

    C# 调用NPOI,修改EXCEL中的数据后并保存后,不会对公式进行更新操作.打开Excel表需要更新一下公式才生效 强制更新公式:C# 调用sheet.ForceFormulaRecalculati ...

  10. Spring Cloud config中,使用数据库存储配置信息

    主要内容 在springcloud config中,使用数据库存储配置信息. 系统默认采用git的方式,此处我们介绍使用jdbc的方式存储配置信息 准备数据库 数据库我们使用mysql. 新建库 p- ...