[poj3414]Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16925   Accepted: 7168   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

题目大意:两只杯子,分别N容量M容量,问要怎样凑出K容量

                DROP(x):倒空x杯

                FILL(x):倒满x被

                POUR(x,y):将x杯中的水倒入y

试题分析:思路很明确的一道题,直接写就行,就算锻炼代码能力了  难得1AC

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
//#include<cmath> using namespace std;
const int INF = 9999999;
#define LL long long inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
int N,M,K;
struct data{
int a,b,fr,st;
int fg;
}Que[200001];
int l=1,r=1;
/*
1:Fill(1)
2:Fill(2)
3:POUR(1,2)
4:ROUP(2,1)
5:DROP(1)
6:DROP(2)
*/
bool vis[201][201];
bool alflag=false; void print(data k){
if(k.fr!=-1){
print(Que[k.fr]);
if(k.fg==1) puts("FILL(1)");
if(k.fg==2) puts("FILL(2)");
if(k.fg==3) puts("POUR(1,2)");
if(k.fg==4) puts("POUR(2,1)");
if(k.fg==5) puts("DROP(1)");
if(k.fg==6) puts("DROP(2)");
}
} void BFS(){
Que[l].a=0,Que[l].b=0,Que[l].fr=-1,Que[l].st=0;
vis[0][0]=true;
int x,y,step;
while(l<=r){
x=Que[l].a,y=Que[l].b,step=Que[l].st;
if(x!=N&&!vis[N][y]){
vis[N][y]=true;
Que[++r].fg=1;
Que[r].a=N;
Que[r].b=y;
Que[r].st=step+1;
Que[r].fr=l;
if(Que[r].a==K||Que[r].b==K){
printf("%d\n",step+1);
print(Que[r]);
alflag=true;
return ;
}
}
if(y!=M&&!vis[x][M]){
vis[x][M]=true;
Que[++r].fg=2;
Que[r].a=x;
Que[r].b=M;
Que[r].st=step+1;
Que[r].fr=l;
if(Que[r].a==K||Que[r].b==K){
printf("%d\n",step+1);
print(Que[r]);
alflag=true;
return ;
}
}
if(x&&y!=M){
int xx,yy;
if(x-(M-y)<=0) yy=y+x,xx=0;
else yy=M,xx=x-(M-y);
if(!vis[xx][yy]){
vis[xx][yy]=true;
Que[++r].fg=3;
Que[r].a=xx;
Que[r].b=yy;
Que[r].st=step+1;
Que[r].fr=l;
if(Que[r].a==K||Que[r].b==K){
printf("%d\n",step+1);
print(Que[r]);
alflag=true;
return ;
}
}
}
if(y&&x!=N){
int xx,yy;
if(y-(N-x)<=0) xx=y+x,yy=0;
else xx=N,yy=y-(N-x);
if(!vis[xx][yy]){
vis[xx][yy]=true;
Que[++r].fg=4;
Que[r].a=xx;
Que[r].b=yy;
Que[r].st=step+1;
Que[r].fr=l;
if(Que[r].a==K||Que[r].b==K){
printf("%d\n",step+1);
print(Que[r]);
alflag=true;
return ;
}
}
}
if(x&&!vis[0][y]){
vis[0][y]=true;
Que[++r].fg=5;
Que[r].a=0;
Que[r].b=y;
Que[r].st=step+1;
Que[r].fr=l;
if(Que[r].a==K||Que[r].b==K){
printf("%d\n",step+1);
print(Que[r]);
alflag=true;
return ;
}
}
if(y&&!vis[x][0]){
vis[x][0]=true;
Que[++r].fg=6;
Que[r].a=x;
Que[r].b=0;
Que[r].st=step+1;
Que[r].fr=l;
if(Que[r].a==K||Que[r].b==K){
printf("%d\n",step+1);
print(Que[r]);
alflag=true;
return ;
}
}
l++;
}
} int main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
N=read(),M=read(),K=read();
BFS();
if(!alflag){
printf("impossible");
return 0;
}
return 0;
}

最新文章

  1. java分享第十七天-02(封装操作excel类)
  2. mysql实现分组和组内序号
  3. C语言的基本输入与输出函数(全解)
  4. PHP面向对象编程之深入理解方法重载与方法覆盖(多态)
  5. Linux/Ubuntu常用快捷键
  6. C++之编码问题(Unicode,ASCII,本地默认)
  7. OOP 概述
  8. 关于viewWithTag的一点说明
  9. List列表 OrderBy
  10. maven 教程一 入门
  11. Maven工具的介绍,配置及使用
  12. 为table元素添加操作日志
  13. github及git使用
  14. Spring03-AOP
  15. 如何使用 Deepfakes 换脸
  16. WPF工具开发: 第三库选择
  17. api文档生成器apidoc的安装和使用
  18. XPath高级用法(冰山一角)
  19. FZU Problem 2030 括号问题
  20. WebStorm配置Node.js IDE

热门文章

  1. bzoj 1084 DP
  2. 特征工程(Feature Engineering)
  3. SQL Workbench/J
  4. ORA-02291:parent key not found
  5. opengl基础学习专题 (一 )编程环境搭建
  6. Kettle提高输入输出数据总结
  7. vue 文件引入
  8. mysql 5.1.7.17 zip安装 和 隔段时间服务不见了处理
  9. python 结束练习
  10. J2EE MySQL Date数据保持一致解决方案