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. jdbc连接池c3p0/dbcp强制连接超过设置时间后失效

    通常来说,各种技术实现的优化参数或者选项或者歪门邪道之所以能被想出来,通常是因为开发者或者实现的贡献者曾经遇到过导致此结果的问题,所以才出了对应的策略选项. 在有些情况下,比如存在客户端或者服务端连接 ...

  2. 编译错误 error C2451: “std::_Unforced”类型的条件表达式是非法的

    part 1 编译器 vs2015 VC++. 完整的错误信息粘贴如下: d:\program files (x86)\microsoft visual studio 14.0\vc\include\ ...

  3. this逃逸

    首先,什么是this逃逸? this逃逸是指类构造函数在返回实例之前,线程便持有该对象的引用. 常发生于在构造函数中启动线程或注册监听器. eg: public class ThisEscape { ...

  4. Python3.5 MySQL 数据库连接

    Python3.5  MySQL 数据库连接 在本文中介绍 Python3 使用PyMySQL连接数据库,并实现简单的增删改查 为什么使用PyMySQL? PyMySQL是在Pyhton3.x版本中用 ...

  5. jQuery ajax 添加头部参数跨域

    1.添加HTTP文件头 $.ajax({ url: "http://www.baidu.com", //contentType: "text/html; charset= ...

  6. springmvc-自定义消息转换器

    最近的项目没有用到这个,先把自己自学跑通的例子先帖出来,供自己以后参考吧! 如有不对地方望指出! 一.自定义类实现AbstractHttpMessageConverter package com.dz ...

  7. div滚动条时div内容显示一半

    本文为博主原创,未经允许不得转载 今天在做页面浏览器适配时,将页面中的一个div进行放大时,出现了滚动条,但滚动条对应div中的 内容只能显示一半. 仔细对应属性样式时,div具有overflow:h ...

  8. 操作构造字符串化宏#define STRINGIZE(x) #x

    c++test工程单元测试中报错 “STRINGIZE” 未定义错误 解决方案:在头文件定义宏STRINGIZE #符号把一个符号直接转换为字符串,例如:#define STRINGIZE(x) #x ...

  9. React Native 组件之TouchableHightLight

    TouchableHighlight 这个组件一般是用于封装视图 给视图添加事件“触摸按下”(类似于click事件) <TouchableHighlight onPress={this._onP ...

  10. DATEDIFF 和 DATEADD

    /* DATEDIFF函数计算两个日期之间的小时.天.周.月.年等时间间隔总数 语法 DATEDIFF(interval, date1, date2[, firstdayofweek[, firstw ...