Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8266   Accepted: 3507   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) 题意:给定三个数A B C,前两个数代表容量为A和B的容器,有三种操作,FILL,DROP,POUR;求最少经过几次这样的操作可以得到容量为C的水; 思路:因为每个容器都有FILL,DROP,POUR三种操作,将两个容器的状态用队列来维护,两个容器共有六种操作的可能;
另一个关键是打印路径,可以用一个结构体数组保存前驱,通过栈再将路径打印出来;
 #include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
//各种状态
#define FILL_A 1;
#define FILL_B 2;
#define DROP_A 3;
#define DROP_B 4;
#define POURA_B 5;
#define POURB_A 6;
using namespace std; struct node
{
int x,y;
int step;
};
queue<struct node>que; struct Path
{
int x,y;
int way;
}path[][];//保存前驱; int vis[][];
int a,b,c; //打印路径
void Print_Path(int x, int y)
{
stack<struct Path>st;
while(!st.empty())
st.pop(); while(!(x == && y == ))
{
st.push(path[x][y]);
int sx = path[x][y].x;
int sy = path[x][y].y;
x = sx;
y = sy;
} while(!st.empty())
{
switch(st.top().way)
{
case :printf("FILL(1)\n");break;
case :printf("FILL(2)\n");break;
case :printf("DROP(1)\n");break;
case :printf("DROP(2)\n");break;
case :printf("POUR(1,2)\n");break;
case :printf("POUR(2,1)\n");break;
}
st.pop();
}
} void bfs()
{
while(!que.empty())
que.pop();
que.push((struct node){,,});//初始状态进队列
vis[][] = ;
while(!que.empty())
{
struct node u = que.front();
que.pop();
if(u.x == c || u.y == c)
{
printf("%d\n",u.step);
Print_Path(u.x,u.y);
return;
}
//FILL_A
if(u.x < a && !vis[a][u.y])
{
vis[a][u.y] = ;
que.push((struct node){a,u.y,u.step+});
path[a][u.y] = (struct Path){u.x,u.y,};
}
//FILL_B
if(u.y < b && !vis[u.x][b])
{
vis[u.x][b] = ;
que.push((struct node){u.x,b,u.step+});
path[u.x][b] = (struct Path){u.x,u.y,};
}
//DROP_A
if(u.x > && !vis[][u.y])
{
vis[][u.y] = ;
que.push((struct node){,u.y,u.step+});
path[][u.y] = (struct Path){u.x,u.y,};
}
//DROP_B
if(u.y > && !vis[u.x][])
{
vis[u.x][] = ;
que.push((struct node){u.x,,u.step+});
path[u.x][] = (struct Path){u.x,u.y,};
}
//POURA_B
if(u.x < a && u.y > )
{
int tmp = min(a-u.x,u.y);
if(!vis[u.x+tmp][u.y-tmp])
{
vis[u.x+tmp][u.y-tmp] = ;
que.push((struct node){u.x+tmp,u.y-tmp,u.step+});
path[u.x+tmp][u.y-tmp] = (struct Path){u.x,u.y,};
}
}
//POURB_A
if(u.x > && u.y < b)
{
int tmp = min(u.x,b-u.y);
if(!vis[u.x-tmp][u.y+tmp])
{
vis[u.x-tmp][u.y+tmp] = ;
que.push((struct node){u.x-tmp,u.y+tmp,u.step+});
path[u.x-tmp][u.y+tmp] = (struct Path){u.x,u.y,};
}
}
}
printf("impossible\n");
}
int main()
{
scanf("%d %d %d",&a,&b,&c);
memset(vis,,sizeof(vis));
bfs();
return ;
}

Pots(bfs)的更多相关文章

  1. POJ 3414 Pots(BFS)

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

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

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

  3. POJ3414—Pots(bfs加回溯)

    http://poj.org/problem?id=3414                                       Pots Time Limit: 1000MS   Memor ...

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

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

  5. POJ3414 Pots —— BFS + 模拟

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

  6. POJ 3414 Pots (BFS/DFS)

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

  7. poj 3414 Pots bfs+模拟

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

  8. POJ 3414 Pots bfs打印方案

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

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

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

随机推荐

  1. (转)兼容主流浏览器的CSS透明代码

    透明往往能产生不错的网页视觉效果下面是兼容主流浏览器的CSS透明代码.transparent_class { filter:alpha(opacity=50); -moz-opacity:0.5; - ...

  2. Android Studio创建工程时一直卡在下载Gradle

    一直提示这个进度条,查了不少资料,有的说FQ,有的说下载gradle后运行下bin里面的批处理,再在环境变量里Path中加入路径,我都试了,都不和... 按理说FQ了可以下载了吧,但是半天没下载完,一 ...

  3. oracle 报错 :ORA-04052、 ORA-00604、 ORA-03106、 ORA-02063

    最近发现一个很奇怪的问题: 创建了一个DB_LINK连接另一个Oracle数据库. select * from tablename@dblinkname; 单句执行没问题,但是把这句SQL写到存储过程 ...

  4. 在C语言中使用scanf语句时遇到的问题总结

    在使用visual studio2013编写c语言代码时,遇到了这样的几个小问题,进行如下的总结. 1, 关于使用scanf语句报错的解决方案1 #include <stdio.h> in ...

  5. C# Base64编码/解码

    一.编码规则      Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编码.它将需要编码的数据拆分成字节数组.以3个字节为一组.按顺序排列24 位数据,再把这24位数据分成4 ...

  6. hdoj 1251 字典树

    代码: #include <stdio.h>#define  MAX    26 typedef struct TrieNode{     int nCount;      struct ...

  7. cocos2d-x 之 CCArray 源码分析

    cocos2d-x 自己实现了一个数组CCArray ,下面我们来分析一下CCArray的源码 CCArray继承CCObject,所以,CCArray也具有引用计数功能和内存自动管理功能. 数组的源 ...

  8. SGU 149. Computer Network

    时间限制:0.25s 空间限制:4M: 题意: 给出一颗n(n<=10000)个节点的树,和n-1条边的长度.求出这棵树每个节点到最远节点的距离: Solution: 对于一个节点,我们可以用D ...

  9. MVC埰坑日记 文件权限

    public static void DownLoadFile(string FileFullPath) { if (!string.IsNullOrEmpty(FileFullPath) & ...

  10. 根据打开页面加载不同Js

    根据打开页面加载不同Js //根据打开页面加载不同JS $(document).ready(function(){ var href = document.URL; /*获取当前页面的URL*/ if ...