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) 给你两个壶,容量a,b,让你三种操作 1.接满 2.一个壶倒到另一个壶(这个壶倒空了或者那个壶满了 才停止) 3.把一个壶倒空 问你几步两个壶中能有一个壶容量是能c。
这个题显然bfs,就是node结构体里面在加上一个queue和string保存下之前的状态。没啥了,代码好长啊QAQ。
代码如下:
 #include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <queue>
#include <vector>
using namespace std;
struct node
{
int na,nb,step;
vector<int> num;
string ns;
};
bool vis [][];
int a,b,c;
bool iffind;
void init ()
{
memset(vis,false,sizeof vis);
iffind=false;
}
void bfs(node now)
{
queue<node>q;
q.push(now);
while (!q.empty())
{
node temp=q.front();
q.pop();
if (vis[temp.na][temp.nb])
continue ;
vis[temp.na][temp.nb]=true;
if (temp.na==c||temp.nb==c)//success
{
iffind=true;
printf("%d\n",temp.step);
//printf("=== %d %d\n",sizeof(temp.ns),temp.num.size());
for (int i=;i<temp.num.size();++i)
{
if (temp.ns[i]=='F')
{
printf("FILL");
if (temp.num[i]==)
printf("(1)\n");
else
printf("(2)\n");
}
else if (temp.ns[i]=='P')
{
printf("POUR");
if (temp.num[i]==)
printf("(1,2)\n");
else
printf("(2,1)\n");
}
else
{
printf("DROP");
if (temp.num[i]==)
printf("(1)\n");
else
printf("(2)\n");
}
}
return ;
}
if (temp.na!=a)//fill a
{
node now=temp;
now.step++;
now.na=a;
now.ns+="F";
now.num.push_back();
q.push(now);
}
if (temp.nb!=b)//fill b
{
node now=temp;
now.step++;
now.nb=b;
now.ns+="F";
now.num.push_back();
q.push(now);
}
if (temp.na>&&temp.nb<b)//pour a to b
{
node now=temp;
now.step++;
now.na-=min(temp.na,b-temp.nb);
now.nb+=min(temp.na,b-temp.nb);
now.ns+="P";
now.num.push_back();
q.push(now);
}
if (temp.nb>&&temp.na<a)//pour b to a
{
node now=temp;
now.step++;
now.nb-=min(temp.nb,a-temp.na);
now.na+=min(temp.nb,a-temp.na);
now.ns+="P";
now.num.push_back();
q.push(now);
}
if(temp.na!=)// drop a
{
node now=temp;
now.step++;
now.na=;
now.ns+='D';
now.num.push_back();
q.push(now);
}
if(temp.nb!=)//drop b
{
node now=temp;
now.step++;
now.nb=;
now.ns+='D';
now.num.push_back();
q.push(now);
}
}
return ;
}
int main()
{
while (~scanf("%d%d%d",&a,&b,&c))
{
init();
node x;
x.na=,x.nb=,x.ns="",x.step=,x.num.clear();
bfs(x);
if (iffind==false){
printf("impossible\n");
}
}
return ;
}
 

POJ 3414 Pots (dfs,这个代码好长啊QAQ)的更多相关文章

  1. BFS POJ 3414 Pots

    题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...

  2. POJ 3414 Pots(罐子)

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

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

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

  4. 广搜+输出路径 POJ 3414 Pots

    POJ 3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13547   Accepted: 5718   ...

  5. POJ 3414 Pots (BFS/DFS)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7783   Accepted: 3261   Special Ju ...

  6. POJ 3414 Pots

    Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status  ...

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

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

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

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

  9. POJ 3414 Pots【bfs模拟倒水问题】

    链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...

随机推荐

  1. JS中常见的几种报错类型

    1.SyntaxError(语法错误) 解析代码时发生的语法错误 var 1a; //Uncaught SyntaxError: Invalid or unexpected token 变量名错误 c ...

  2. JS谷歌浏览器断点调试

    1.找到对应的文件 按F12打开网页调试工具,默认打开的是Elements,显示的是网页标签元素.选择Source,在左侧找到对应的js代码文件(这里是在page标签上找到的) 1.1.如何找到web ...

  3. django搭建一个小型的服务器运维网站-重启服务器的进程

    目录 项目介绍和源码: 拿来即用的bootstrap模板: 服务器SSH服务配置与python中paramiko的使用: 用户登陆与session; 最简单的实践之修改服务器时间: 查看和修改服务器配 ...

  4. ES6 Generator使用

    // generator介绍: function* hello() { console.log("hello world") } hello();//没有执行 // 直接调用hel ...

  5. 【转】 Linux 命令解释(Linux基础二)

    前言 对服务器来讲,图形界面会占用更多的系统资源,而且会安装更多的服务.开放更多的端口,这对服务器的稳定性和安全性都有负面影响.其实,服务器是一个连显示器都没有的家伙,要图形界面干十么? 说到这里,有 ...

  6. xshell设置选中复制,右击粘贴功能

    . 设置选中复制: 工具--->选项--->键盘和鼠标--->(然后根据下图设置保存即可) 2. 设置ctrl + v 粘贴功能: 工具--->选项--->键盘和鼠标-- ...

  7. 对GridFS数据进行分片

    On this page files 集合 chunks 集合 在对 GridFS 存储进行分片时,需要注意以下的情况: files 集合 大多数情况下不需要对 files 集合进行分片,这个集合通常 ...

  8. 用threading 解决 gunicorn worker timeout

    产生worker timeout 的背景 while 1: ..... time.sleep(1) gunicorn运行起来,只等待了30s,就卡住了,没报任何异常或err,查了gunicorn 官方 ...

  9. MySQL 添加用户、删除用户与授权

    mysql -uroot -proot MySQL5.7 mysql.user表没有password字段改 authentication_string: 一. 创建用户: 命令:CREATE USER ...

  10. Java前端控制器模式~

    前端控制器设计模式用于提供集中式请求处理机制,以便所有请求将由单个处理程序处理.此处理程序可以执行请求的身份验证/授权/记录或跟踪,然后将请求传递到相应的处理程序. 以下是这种类型的设计模式的实体. ...