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 ;
}

最新文章

  1. 非root Android设备上Tcpdump的实现
  2. sqlserver开窗函数
  3. 字体 font typograph
  4. spring含参数 环绕通知demo
  5. Android开发之通过反射获取到挂断电话的API
  6. shell中对于命令的搜寻顺序
  7. Struts2大约Action系统培训6大约action的接受三个参数的方法
  8. 深入研究React setState的工作机制
  9. spring-data-jpa 中,如果使用了one-to-many , many-to-one的注释,会在Jackson进行json字符串化的时候出现错误
  10. Android+TensorFlow+CNN+MNIST 手写数字识别实现
  11. git 提交的步骤
  12. thinkphp验证器
  13. DMA及cache一致性的学习心得 --dma_alloc_writecombine【转】
  14. oracle 语句 笔记
  15. oracle 11g AUTO_SAMPLE_SIZE动态采用工作机制
  16. 洛谷P2326 AKN’s PPAP
  17. siblings()
  18. CentOS7.4 删除virbr0 virbr0-nic虚拟网卡
  19. &lt;Android Framework 之路&gt; N版本 Framework Camera的一些改动
  20. Java中 方法的多态 简析图

热门文章

  1. Luogu P2973 [USACO10HOL]赶小猪Driving Out the Piggi 后效性DP
  2. Helvetic Coding Contest 2016 online mirror D1
  3. ELK(ElasticSearch, Logstash, Kibana) v5.3.2 分布式日志收集分析最佳解决方案 基于CentOS 7 ( 一 )
  4. form中onsubmit的使用
  5. 用mvc模式,整理前两次的代码并增加登陆注册
  6. DB2去重的几种方法
  7. Golang自带的http包的路由规则问题
  8. ASP Session的功能的缺陷以及解决方案
  9. UICollectionView笔记1
  10. Spring AOP初步总结(三)