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. Coursera 算法二 week 3 Baseball Elimination

    这周的作业不需要自己写算法,只需要调用库函数就行,但是有些难以理解,因此用了不少时间. import edu.princeton.cs.algs4.FlowEdge; import edu.princ ...

  2. JS.match方法 正则表达式

    match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配. 该方法类似 indexOf() 和 lastIndexOf(),但是它返回指定的值,而不是字符串的位置. <sc ...

  3. 【BZOJ3668】[NOI2014] 起床困难综合症(位运算思想)

    点此看题面 大致题意: 给定一些位运算操作,让你在\(0\sim m\)范围内选一个初始值,使其在经过这些运算后得到的结果最大. 前置技能:关于位运算 作为一道位运算的题,如果你不知道什么是位运算,那 ...

  4. 【BZOJ1216】[HNOI2003] 操作系统(堆+模拟)

    点此看题面 大致题意: 有\(n\)个任务,每个任务有4个属性:编号.到达时间.执行时间和优先级.每个单位时间,会执行一个优先级最高(若有多个优先级最高的,就先执行到达时间较早的)的任务,请你按完成的 ...

  5. 2018.1.4 UML 第三章 用例图

    第三章 用例图 (1)参与者 是指系统以外的需要使用系统或与系统交互的外部实体,吧阔人.设备.外部系统等. (2)参与者之间的关系 泛化关系的含义是参与者的共同行为提取出来表示成通用行为,并描述成超类 ...

  6. CentOS系统下安装Redis

    1. 安装C语言环境 yum install gcc-c++ 2.下载Redis安装包 http://download.redis.io/releases/redis-3.2.9.tar.gz 3.解 ...

  7. Matlab将多幅图片保存为mat

    %% 储存某目录所有的图片 pt = 'd:\imgs\'; ext = '*.jpg'; dis = dir([pt ext]); nms = {dis.name}; for k = 1:lengt ...

  8. c++ 中十进制 八进制 十六进制 二进制转换 最简方法

    #include<iostream> using namespace std; int main() { int i; cin>>dec>>i; //cin> ...

  9. SpingBoot之多Profile文件

    1.我们在主配置文件编写的时候,文件名可以是 application-{profile}.properties/yml默认使用application.properties的配置: 在我们的项目开发.测 ...

  10. Java 获取Web项目相对webapp地址

    例如, import java.io.File; import java.io.FileInputStream; import javax.servlet.http.HttpServletReques ...