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

poj3414 Pots (BFS)的更多相关文章

  1. POJ-3414 Pots (BFS)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  2. poj3414 Pots(BFS)

    题目链接 http://poj.org/problem?id=3414 题意 有两个杯子,容量分别为A升,B升,可以向杯子里倒满水,将杯子里的水倒空,将一个杯子里的水倒到另一个杯子里,求怎样倒才能使其 ...

  3. 【POJ - 3414】Pots(bfs)

    Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i)        将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...

  4. Pots(BFS)

    Pots Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submiss ...

  5. poj 3414 Pots ( bfs )

    题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i)        fill the ...

  6. POJ 3414 Pots(罐子)

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

  7. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  8. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  9. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

随机推荐

  1. app再次进入数据不加载问题

    问题原因:触发点击事件在加载页面之前完成. 1.调整了一下页面加载顺序 2.增加了settime的时间

  2. WordPress企业建站心得

    回头聊聊我用WordPress做企业网站的事.说是企业网站,其实就是一个小的企业展示网站.事情要从我爸开了一家自行车店开始说起,自从他开了自行车店,不但开始学着玩起了微信(因为要做微信营销),又想到了 ...

  3. 【CF799B】T-shirt buying(一道很水的小根堆)

    点此看题面 大致题意: 有\(n\)件T恤衫,告诉你每件T恤衫的价格以及它正面和反面的颜色(\(1≤\)颜色的编号\(≤3\)),现在有m个顾客,已知每个人想要的衣服的颜色(一件T恤衫只要有一面的颜色 ...

  4. python_61_装饰器4

    import time def timer(func):#timer(test1) func=test1 def deco(): start_time=time.time() func()#run t ...

  5. 牛客小白月赛5 F 圆(circle) 【欧拉定理】

    题目连接: https://www.nowcoder.com/acm/contest/135/F 签到题来了,送你们一个Python秒的题. Apojacsleam来到了OI大陆,经过了连年征战,成为 ...

  6. Dart Socket 与Java Socket连接

    -------------------------------------------------------------  Dart    SocketClient----------------- ...

  7. VM内存溢出

    平常开发时,有的人会运行的同时,会改代码,可能会导致VM内存溢出 Eclipse需要设置如下步骤: 1.点击Run>Run Configurations.. 2.定位到Tomcat(自己本地配置 ...

  8. Java流(Stream)、文件(File)和IO

    Java.io包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io包中的流支持很多格式,比如:基本类型.对象.本地化字符集等等. 一个流可以理解为一个数据的序列 ...

  9. docker时区正常,但java获得的时间早了8小时解决方法

    我解决容器时区的方法是挂载宿主机的/etc/localtime 到容器的/etc/localtime,这时输入date命令容器时区显示正常,但是跑在容器中的java项目取到的时间却早了8小时. 查阅相 ...

  10. bootmem API总结

    bootmem_init()函数执行完成后,linux启动初期的bootmem分配器就初始化完成了,可以调用bootmem提供的API分配内存. 这些API在include/linux/bootmem ...