Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12198   Accepted: 5147   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 ≤ i ≤ 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 A, B, 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个杯子装满(i=0,1)

2、DROP(i)      把第i个杯子倒空

3、POUR(i,j)    把i的水倒入到j中,直到j满或i倒完

我的想法:把a->b,b->c,。。。。共6种倒水方法一个一个列出来,而每种都是一样的讨论方法,

虽然很好做,但对于我这样的入门级水手来说还是写不出太好看的代码,所以。。。。

看看吧,看不懂再去看看网上别人的==||

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=;
bool vis[maxn][maxn];
int fa[maxn][maxn];
int op[maxn][maxn];
int sa,sb,sc;
struct node{
int a,b,step;
};
char ans[][]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
void print(int a ,int b){
if(op[a][b]==-)
return;
else{
print(fa[a][b]/,fa[a][b]%);
printf("%s\n",ans[op[a][b]]);
}
} bool bfs(){
node u,v;
u.a=,u.b=;
u.step=;
queue<node>q;
q.push(u);
memset(op,,sizeof(op));
memset(vis,false,sizeof(vis));
memset(fa,,sizeof(fa));
op[][]=-;
fa[][]=;
vis[][]=true;
while(!q.empty()){
u=q.front();
q.pop();
if(u.a==sc||u.b==sc){
printf("%d\n",u.step);
print(u.a,u.b);
return true;
}
v=u; if(u.a!=sa){
v.a=sa;
if(!vis[v.a][v.b]){
v.step++;
q.push(v);
op[v.a][v.b]=;
vis[v.a][v.b]=true;
fa[v.a][v.b]=u.a*+u.b;
}
}v=u;
if(u.b!=sb){v.b=sb;
if(!vis[v.a][v.b]){
v.step=u.step+;
q.push(v);
op[v.a][v.b]=;
vis[v.a][v.b]=;
fa[v.a][v.b]=u.a*+u.b;
}
}v=u;
if(u.a){v.a=;
if(!vis[v.a][v.b]){
v.step++;
q.push(v);
op[v.a][v.b]=;
vis[v.a][v.b]=true;
fa[v.a][v.b]=u.a*+u.b;
}
}v=u;
if(u.b){v.b=;
if(!vis[v.a][v.b]){
v.step++;
q.push(v);
op[v.a][v.b]=;
vis[v.a][v.b]=true;
fa[v.a][v.b]=u.a*+u.b;
}
}v=u;
if(u.a){
if(v.a>=sb-u.b&&u.b!=sb){v.a-=(sb-u.b);v.b=sb;}
else if(v.a<sb-u.b){v.a=;v.b+=u.a;}
if(!vis[v.a][v.b]){
v.step++;
q.push(v);
op[v.a][v.b]=;
vis[v.a][v.b]=true;
fa[v.a][v.b]=u.a*+u.b;
}
}v=u;
if(u.b)
{
if(v.b>=sa-u.a&&u.a!=sa){v.b-=(sa-u.a);v.a=sa;}
else if(v.b<sa-u.a){v.b=;v.a+=u.b;}
if(!vis[v.a][v.b]){
v.step++;
q.push(v);
op[v.a][v.b]=;
vis[v.a][v.b]=true;
fa[v.a][v.b]=u.a*+u.b;
}
} }
return false;
}
int main(){
while(scanf("%d%d%d",&sa,&sb,&sc)!=EOF){
bool flag=bfs(); if(!flag)
printf("impossible\n");
}
return ;
}

最新文章

  1. ABP文档 - Javascript Api
  2. eCharts 数据转换json
  3. 安装.NET FRAMEWORK 4.5安装进度条回滚之后发生严重错误 代码0x80070643
  4. VBA笔记(一)
  5. 去除ios系统a标签点击时的灰色背景
  6. web 安全的前期准备哦
  7. react-native start 运行流程
  8. poj1274(匈牙利算法)
  9. Js~对数组进行分组户数
  10. [转]Ubuntu 系统 Update-rc.d 命令
  11. [Yii2]Access to debugger is denied due to IP address restriction. The requesting IP address is
  12. javascript设计模式1
  13. js控件位置
  14. Asp.Net--回调技术
  15. [ An Ac a Day ^_^ ] hrbust 2291 Help C5 分形
  16. js自定义的简易滚动条
  17. js对象,类
  18. DELPHI XE8 远程调试
  19. Linux普通用户修改owner非本人文件为什么成功
  20. 基于PLC-C#串口通讯,温度检测和转速监控的c#/.Net实现。

热门文章

  1. 一些好的IOS blog 不断增加中。。。。
  2. 【BZOJ3172】[TJOI2013] 单词(AC自动机的小应用)
  3. 2018.5.29 Oracle连接到空闲例程
  4. Debug与Release版本的区别详解
  5. 关于小程序 input 组件内容显示不全(显示的长度不满 input 宽度)问题
  6. NodeJS--exports和module.exports
  7. LeetCode948-令牌放置
  8. win7旗舰版64位java的jdk环境变量的配置(2012-12-26-bd 写的日志迁移
  9. JZOJ 5849 d
  10. POJ 2299 Ultra-QuickSort 简单题解