Pots

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

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 ≤ ≤ 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 AB, 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)

注:本人英语很渣,题目大意大多来自百度~=0=

 

题目大意:

给出了两个瓶子的容量a,b, 以及一个目标水量n

对A、B可以有如下操作:

FILL(i)        把i倒满

DROP(i)      把i倒空

POUR(i,j)     把i倒入j

问经过哪几个操作后能使得任意一个瓶子的水量为C。

若不可能得到则输出impossible

输入就是A(a) B(b) C(n)

输出第一行是一共需要几次操作

下面是每行是一次操作

解题思路 :

BFS

每次操作有8种可能(额,我是按照自己题解写的序号 将就着看吧=0=)

a倒入b时:

1.倒满, a剩余

7.倒空,a为0

b倒入a时:

2.倒满, b剩余

8.倒空,b为0

当a不满时

3.FILL(a)

当b不满时

4.FILL(b)

当a != 0时

5.倒空a

当b != 0时

6.倒空b

由于输出需要步骤,所以我用结构体保存了前一个状态的oa, ob, oc(原因是这三个都需要才能找到上一个操作)

代码如下:

#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <queue>
#define N 110
#define INF 0xfffffff
using namespace std;
int a, b, n;
bool f[N][N][]; //f用来判断此状态是不是已经使用过
struct node
{
int a, b, step, oa, ob, c, oc;//a, b 状态的下标, c 状态操作 o+..是指前一个状态
}s, e, ans[N][N][]; //ans用来保存每一个状态的值
void Putc(int c)
{
if(c == || c == ) printf("POUR(1,2)\n");
if(c == ) printf("DROP(1)\n");
if(c == || c == ) printf("POUR(2,1)\n");
if(c == ) printf("DROP(2)\n");
if(c == ) printf("FILL(1)\n");
if(c == ) printf("FILL(2)\n");
}
void inPut(node s)
{
int d[N], dd = ;
printf("%d\n", s.step);
d[dd++] = s.c;
while(s.a || s.b) {
s = ans[s.oa][s.ob][s.oc];
d[dd++] = s.c; //将之前每一个操作储存起来 最后输出 因为如果直接输出顺序回事反的
}
for(int i = dd - ; i >= ; i--)
Putc(d[i]);
}
void Init() //初始化
{
memset(f, false, sizeof(f));
memset(ans, , sizeof(ans));
}
void BFS()
{
queue<node>q;
s = {, , };
q.push(s);
f[s.a][s.b][s.c] = ;
while(!q.empty()) {
s = q.front();
q.pop();
if(s.a == n || s.b == n) {
inPut(s);
return;
}
if(s.a != ) {
//a->b
if(s.a + s.b > b){//b被倒满a有剩余
e = {s.a - (b - s.b), b, s.step + , s.a, s.b, , s.c};
if(!f[e.a][e.b][e.c])
q.push(e), f[e.a][e.b][e.c] = , ans[e.a][e.b][e.c] = e;
}
else {//a倒b 无剩余
e = {, s.a + s.b, s.step + , s.a, s.b, , s.c};
if(!f[e.a][e.b][e.c])
q.push(e), f[e.a][e.b][e.c] = , ans[e.a][e.b][e.c] = e;
}
//倒空a
e = {, s.b, s.step + , s.a, s.b, , s.c};
if(!f[e.a][e.b][e.c])
q.push(e), f[e.a][e.b][e.c] = , ans[e.a][e.b][e.c] = e;
}
if(s.b != ) {
//b->a
if(s.a + s.b > a){//a被倒满b有剩余
e = {a, s.b - (a - s.a), s.step + , s.a, s.b, , s.c};
if(!f[e.a][e.b][e.c])
q.push(e), f[e.a][e.b][e.c] = , ans[e.a][e.b][e.c] = e;
}
else {//b倒a 无剩余
e = {s.a + s.b, , s.step + , s.a, s.b, , s.c};
if(!f[e.a][e.b][e.c])
q.push(e), f[e.a][e.b][e.c] = , ans[e.a][e.b][e.c] = e;
}
//~倒空b
e = {s.a, , s.step + , s.a, s.b, , s.c};
if(!f[e.a][e.b][e.c])
q.push(e), f[e.a][e.b][e.c] = , ans[e.a][e.b][e.c] = e;
}
//把a加满
if(s.a != a) {
e = {a, s.b, s.step + , s.a, s.b, , s.c};
if(!f[e.a][e.b][e.c])
q.push(e), f[e.a][e.b][e.c] = , ans[e.a][e.b][e.c] = e;
}
//把b加满
if(s.b != b){
e = {s.a, b, s.step + , s.a, s.b, , s.c};
if(!f[e.a][e.b][e.c])
q.push(e), f[e.a][e.b][e.c] = , ans[e.a][e.b][e.c] = e;
}
}
printf("impossible\n");
}
int main()
{
while (~scanf("%d %d %d", &a, &b, &n)) {
Init();
BFS();
}
}

POJ 3414 Pots的更多相关文章

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

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

  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. BFS POJ 3414 Pots

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

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

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

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

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

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

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

  8. poj 3414 Pots ( bfs )

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

  9. POJ 3414 Pots bfs打印方案

    题目: http://poj.org/problem?id=3414 很好玩的一个题.关键是又16ms 1A了,没有debug的日子才是好日子.. #include <stdio.h> # ...

随机推荐

  1. hive相关

    HIVE JOIN:http://blog.csdn.net/yfkiss/article/details/8073608 HIVE资料: 一条记录map阶段输出KV,shuffle sort,输出K ...

  2. python 学习笔记十五 django基础

    Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...

  3. Discuz!模板解析语法

    <!--{eval echo autostart("); }--> PHP中使用template()函数显示已存在模板 在Discuz!程序执行中可以通过 include tem ...

  4. iOS上传文件代码,自定义组装body

    以下代码为上传文件所用代码,简单方便,搞了好久,终于知道这么简单的方式来上传. 其它类库也就是把这几句代码封装的乱七八糟得,让你老久搞不懂原理.不就是在body上面加点字符串,body下面加点字符串, ...

  5. 如何使用xshell远程连接ubuntu

    在Ubuntu上安装ssh就可以使用xshell登录了,安装步骤: 1,sudo apt-get install openssh-server 2,然后启动ssh sudo /etc/init.d/s ...

  6. 动态规划(DP)

    一.基本概念 动态规划过程是:每次决策依赖于当前状态,又随即引起状态的转移.一个决策序列就是在变化的状态中产生出来的,所以,这种多阶段最优化决策解决问题的过程就称为动态规划. 二.基本思想与策略 基本 ...

  7. juqery模板 Templates

    现在已经有了许多JavaScript的解决方案模板,从这方面说,标准化的模板解决方案必然是大势所趋.在本节中,我们向你简要描述四个最流行最有趣的模板.现有的模板解决方案能解决什么?那些特色在jQuer ...

  8. SQL函数

    1,字符串截取拼接 CONCAT(),'****');SUBSTRING_INDEX(c.context,'}',1);SUBSTRING_INDEX(a.task_context,':',-1) a ...

  9. WinForm开发框架【细化权限至操作按钮】

    有不少园友经常问我程序有没有更新,真的很抱歉,最近因为工作原因一直很忙,导致程序有很长时间都没有更新了,首先在这里感谢关心俺的朋友们. 这几天好好看了一下原来的程序,还有很多地方需要改进,比如操作数据 ...

  10. 备份MySQL数据库

    备份MySQL数据库脚本: #!/bin/bash # description: MySQL buckup shell script # author: lmj # web site: http:// ...