Pots
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10071   Accepted: 4237   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 ≤ ≤ 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)

思路:一共同拥有6种操作:把A中水倒掉,把A加满,把B里的水倒入A中。B和A类似。

罐子最大容积为100,设一个常量N=100, 开一个二维数组记录状态变化的值。

1、从水龙头往A里加水t,记录-t,

2、从水龙头往B里加水t,记录-t-N,

3、从B里面往A加水t,记录t

4、从A里面往B加水t。记录N+t

5、把A里水倒掉,记录2*N+t,(A原有水t)

6、把B里水倒掉,记录3*N+t,(B原有水t)

#include<stdio.h>
#include<queue>
#include<map>
#include<string>
#include<string.h>
using namespace std;
#define N 105
const int inf=0x1f1f1f1f;
int a,b,c,flag;
int mark[N][N];
struct node
{
int x,y,t;
friend bool operator<(node a,node b)
{
return a.t>b.t;
}
};
void prif(int x,int y) //递归输出路径
{
if(x==0&&y==0)
return ;
if(mark[x][y]>3*N)
{
prif(x,mark[x][y]-3*N);
printf("DROP(2)\n");
}
else if(mark[x][y]>2*N)
{
prif(mark[x][y]-2*N,y);
printf("DROP(1)\n");
}
else if(mark[x][y]>N)
{
int tmp=mark[x][y]-N;
prif(x+tmp,y-tmp);
printf("POUR(1,2)\n");
}
else if(mark[x][y]>0)
{
int tmp=mark[x][y];
prif(x-tmp,y+tmp);
printf("POUR(2,1)\n");
}
else if(mark[x][y]>-N)
{
int tmp=-mark[x][y];
prif(x-tmp,y);
printf("FILL(1)\n");
}
else if(mark[x][y]<-N)
{
int tmp=N+mark[x][y];
prif(x,y+tmp);
printf("FILL(2)\n");
}
}
void bfs()
{
priority_queue<node>q;
node cur,next;
mark[0][0]=inf; //该状态仅仅能出现一次。赋值为inf防止干扰其它值
mark[a][0]=-a;
mark[0][b]=-b-N;
cur.t=1;
cur.x=a;
cur.y=0;
q.push(cur);
cur.x=0;
cur.y=b;
q.push(cur);
while(!q.empty())
{
cur=q.top();
q.pop();
next.t=cur.t+1;
if(cur.x==c||cur.y==c)
{
flag=1;
printf("%d\n",cur.t);
prif(cur.x,cur.y);
return ;
}
if(cur.x<a) //向A加水
{
int tmp=a-cur.x;
next.y=cur.y;
next.x=a; //来自水龙头的水
if(!mark[next.x][next.y])
{
mark[next.x][next.y]=-tmp;
q.push(next);
}
if(cur.y>0) //来自B的水
{
int tmp=min(cur.y,a-cur.x);
next.x=cur.x+tmp;
next.y=cur.y-tmp;
if(!mark[next.x][next.y])
{
mark[next.x][next.y]=tmp;
q.push(next);
}
}
}
if(cur.y<b) //向B加水
{
int tmp=b-cur.y;
next.x=cur.x;
next.y=b; //来自水龙头的水
if(!mark[next.x][next.y])
{
mark[next.x][next.y]=-tmp-N;
q.push(next);
}
if(cur.x>0) //来自A的水
{
int tmp=min(cur.x,b-cur.y);
next.y=cur.y+tmp;
next.x=cur.x-tmp;
if(!mark[next.x][next.y])
{
mark[next.x][next.y]=tmp+N;
q.push(next);
}
}
}
if(cur.x>0) //倒掉水
{
int tmp=cur.x;
next.x=0;
next.y=cur.y;
if(!mark[next.x][next.y])
{
mark[next.x][next.y]=2*N+tmp;
q.push(next);
}
}
if(cur.y>0)
{
int tmp=cur.y;
next.y=0;
next.x=cur.x;
if(!mark[next.x][next.y])
{
mark[next.x][next.y]=3*N+tmp;
q.push(next);
}
}
}
}
int main()
{
while(scanf("%d%d%d",&a,&b,&c)!=-1)
{
memset(mark,0,sizeof(mark));
flag=0;
bfs();
if(!flag)
printf("impossible\n");
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

poj 3414 Pots (bfs+线索)的更多相关文章

  1. POJ 3414 Pots bfs打印方案

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

  2. POJ 3414 Pots(BFS)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description You are g ...

  3. poj 3414 Pots(bfs+输出路径)

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

  4. POJ - 3414 Pots BFS(著名倒水问题升级版)

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

  5. POJ 3414 Pots (BFS/DFS)

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

  6. poj 3414 Pots bfs+模拟

    #include<iostream> #include<cstring> #define fillA 1 #define pourAB 2 #define dropA 3 #d ...

  7. POJ 3414 Pots ( BFS , 打印路径 )

    题意: 给你两个空瓶子,只有三种操作 一.把一个瓶子灌满 二.把一个瓶子清空 三.把一个瓶子里面的水灌到另一个瓶子里面去(倒满之后要是还存在水那就依然在那个瓶子里面,或者被灌的瓶子有可能没满) 思路: ...

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

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

  9. BFS POJ 3414 Pots

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

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

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

随机推荐

  1. 数组去重Array

    var aee3=[31,42,13,19,5,11,8,13,40,39,1,8,44,15,3]; Array.prototype.unqu2=function(){ this.sort(); v ...

  2. 怎么制作U盘系统/PE启动盘

    原地址:http://jingyan.baidu.com/article/3c48dd3453d05de10be358fb.html 不用光驱就能启动系统,做一个U盘启动系统就搞定,下面我给大家演示如 ...

  3. android之listView定位到指定行同一时候隐藏输入键盘

    帮别人该bug遇到的一个问题,记录下来. listView.setSelection(a); 这种方法能够让让你的listview定位到指定行 可是假设紧接着运行隐藏输入键盘的代码.则会有bug.这个 ...

  4. IOS线程操作(3)

    采用CGD更有效的比前两个(它被认为是如此,有兴趣的同学可以去试试). 这是推荐的方式来使用苹果的比较. GCD它是Grand Central Dispatch缩写,这是一组并行编程C介面. GCD是 ...

  5. 公布windows的&quot;Universal Apps&quot; Unity3D游戏

    转载请注明出处:http://blog.csdn.net/u010019717 更全的内容请看我的游戏蛮牛地址:http://www.unitymanual.com/space-uid-18602.h ...

  6. 从mina中学习超时程序编写

    从mina中学习超时程序编写 在很多情况下,程序需要使用计时器定,在指定的时间内检查连接过期.例如,要实现一个mqtt服务,为了保证QOS,在服务端发送消息后,需要等待客户端的ack,确保客户端接收到 ...

  7. FMOD在Android玩音响系统的抖动问题

    1. 基本介绍 在Android升级系统Android4.4之后,发现FMOD在Android音会出现抖动.导致声音不正常.边赫赫有名的"极品飞车"都有问题. 经查验,是FMOD的 ...

  8. JavaScript类数组对象参考

    JavaScript和DOM中有很多类数组对象,它们有以下特点 1.有length属性 2.可以使用[]通过下标访问 3.部分类数组对象使用[]访问成员时不只可以使用下标,还可以使用id或name 4 ...

  9. HDU 2828 DLX搜索

    Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  10. 二叉树3种递归和非递归遍历(Java)

    import java.util.Stack; //二叉树3种递归和非递归遍历(Java) public class Traverse { /******************一二进制树的定义*** ...