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) 题目分析:打眼一看就是BFS,还是普通的BFS。 代码如下:
 # include<iostream>
# include<cstdio>
# include<string>
# include<queue>
# include<vector>
# include<cstring>
# include<algorithm>
using namespace std;
struct node
{
int a,b,t;
vector<string>op;
bool operator < (const node &a) const {
return t>a.t;
}
node & operator = (const node &p) {
a=p.a,b=p.b,t=p.t;
op.clear();
for(int i=;i<p.op.size();++i)
op.push_back(p.op[i]);
return *this;
}
};
int vis[][];
void bfs(int A,int B,int C)
{
priority_queue<node>q;
memset(vis,,sizeof(vis));
node sta;
sta.a=sta.b=sta.t=;
sta.op.clear();
vis[][]=;
q.push(sta);
while(!q.empty())
{
node u=q.top();
q.pop();
if(u.a==C||u.b==C){
printf("%d\n",u.t);
for(int i=;i<u.op.size();++i)
cout<<u.op[i]<<endl;
return ;
}
if(u.a<A){
node now=u;
now.a=A,now.b=u.b;
if(!vis[now.a][now.b]){
vis[now.a][now.b];
now.t=u.t+;
now.op.push_back("FILL(1)");
q.push(now);
}
}
if(u.b<B){
node now=u;
now.a=u.a,now.b=B;
if(!vis[now.a][now.b]){
vis[now.a][now.b];
now.t=u.t+;
now.op.push_back("FILL(2)");
q.push(now);
}
}
if(u.a>){
node now=u;
now.a=,now.b=u.b;
if(!vis[now.a][now.b]){
vis[now.a][now.b];
now.t=u.t+;
now.op.push_back("DROP(1)");
q.push(now);
}
}
if(u.b>){
node now=u;
now.a=u.a,now.b=;
if(!vis[now.a][now.b]){
vis[now.a][now.b];
now.t=u.t+;
now.op.push_back("DROP(2)");
q.push(now);
}
}
if(u.a<A&&u.b>){
node now=u;
now.a=min(A,u.a+u.b);
now.b=max(,u.b-A+u.a);
if(!vis[now.a][now.b]){
vis[now.a][now.b]=vis[now.b][now.a]=;
now.t=u.t+;
now.op.push_back("POUR(2,1)");
q.push(now);
}
}
if(u.a>&&u.b<B){
node now=u;
now.a=max(,u.a-B+u.b);
now.b=min(B,u.b+u.a);
if(!vis[now.a][now.b]){
vis[now.a][now.b]=vis[now.b][now.a]=;
now.t=u.t+;
now.op.push_back("POUR(1,2)");
q.push(now);
}
}
}
printf("impossible\n");
}
int main()
{
int A,B,C;
scanf("%d%d%d",&A,&B,&C);
bfs(A,B,C);
return ;
}

POJ-3414 Pots (BFS)的更多相关文章

  1. poj 3414 Pots ( bfs )

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

  2. POJ 3414 Pots(罐子)

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

  3. poj 3414 Pots (bfs+线索)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10071   Accepted: 4237   Special J ...

  4. POJ 3414 Pots(BFS+回溯)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11705   Accepted: 4956   Special J ...

  5. poj 3414 Pots 【BFS+记录路径 】

    //yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...

  6. poj 3414 Pots【bfs+回溯路径 正向输出】

    题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  7. poj 3414 Pots(广搜BFS+路径输出)

    转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:id=3414">http://poj.org/probl ...

  8. 【POJ - 3414】Pots(bfs)

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

  9. POJ 3414 Pots (dfs,这个代码好长啊QAQ)

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

  10. POJ 3414 pots (未解决)

    http://poj.org/problem?id=3414 #include <iostream> #include <cstdio> #include <queue& ...

随机推荐

  1. Linux中Postfix邮件安装配置(二)

    本套邮件系统的搭建,从如何发邮件到收邮件到认证到虚拟用户虚拟域以及反病毒和反垃圾邮件等都有详细的介绍.在搭建过程中必须的参数解释以及原理都有告诉,这样才能更好地理解邮件系统. 卸载自带postfix ...

  2. sqlserver 判断各种不存在

    判断数据库是否存在 if exists (select * from dbo.sysobjects where name = '数据库名') --drop database [数据库名] 判断表是否存 ...

  3. 推荐:Java性能优化系列集锦

    Java性能问题一直困扰着广大程序员,由于平台复杂性,要定位问题,找出其根源确实很难.随着10多年Java平台的改进以及新出现的多核多处理器,Java软件的性能和扩展性已经今非昔比了.现代JVM持续演 ...

  4. tensorflow reduction_indices理解

    在tensorflow的使用中,经常会使用tf.reduce_mean,tf.reduce_sum等函数,在函数中,有一个reduction_indices参数,表示函数的处理维度,直接上图,一目了然 ...

  5. mac下的一些操作

    mac 下修改Hosts文件 : http://www.cnblogs.com/zhangqs008/p/3773623.html mac下装Tomcat服务器: 在苹果系统安装Tomcat:首先下载 ...

  6. C# string字节数组转换

    string转byte[]:byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str ); byte[]转string:string ...

  7. DSDS,双模,双卡,双待,单待,双通,单通,概念及相互关系?【转】

    本文转载自:https://blog.csdn.net/dirk_it/article/details/7178058?utm_source=blogxgwz9 DSDS:双卡双待 DualSimDu ...

  8. 32位MD5加密补齐丢失的0

    /// <summary> /// 获取32位MD5加密字符串(已补完0) /// </summary> /// <param name="strWord&qu ...

  9. spring注解注入properties配置文件

    早期,如果需要通过spring读取properties文件中的配置信息,都需要在XML文件中配置文件读取方式. 基于XML的读取方式: <bean class="org.springf ...

  10. 【第二十六章】 hystrix-dashboard + turbine

    一.使用turbine的意义 引入多个hystrix stream: 1.使用hystrix-dashboard的可以添加多个stream的功能 图中添加的两个stream会在真正monitor的时候 ...