You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;
        DROP(i)      empty the pot i to the drain;
        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)

题意 :

3 种操作,第一种将桶内装满,第二种将桶倒干净,第三种将一个桶内的倒入另一个桶。

思路 :

BFS做,每次将当前的状态打入队列,最后要输出路径,在结构体中存上一个点的位置。

代码 :

int a, b, c;
struct node
{
int x, y;
int pt;
int temp;
node *pre;
}arr[400]; int vis[105][105];
int ans;
stack<int>s;
void bfs(){
queue<node>que; // 每次将当前的状态打入队列,对于每个状态有 6 种处理方法
memset(vis, 0, sizeof(vis)); node v;
v.x = 0, v.y = 0;
v.pt = 0;
v.temp = 0;
v.pre = NULL;
while(!que.empty()) que.pop();
que.push(v);
int cnt = -1;
vis[0][0] = 1; while(!que.empty()){
v = que.front();
que.pop();
cnt++;
arr[cnt] = v; for(int i = 1; i <= 6; i++){
switch(i){
case 1:
v.x = a;
v.y = arr[cnt].y;
v.pt = 1;
break;
case 2:
v.x = arr[cnt].x;
v.y = b;
v.pt = 2;
break;
case 3:
v.x = 0;
v.y = arr[cnt].y;
v.pt = 3;
break;
case 4:
v.x = arr[cnt].x;
v.y = 0;
v.pt = 4;
break;
case 5:
if (arr[cnt].x > (b - arr[cnt].y)){
v.x = arr[cnt].x - (b - arr[cnt].y);
v.y = b;
}
else {
v.x = 0;
v.y = arr[cnt].x + arr[cnt].y;
}
v.pt = 5;
break;
case 6:
if (arr[cnt].y > (a - arr[cnt].x)){
v.x = a;
v.y = arr[cnt].y - (a - arr[cnt].x);
}
else {
v.x = arr[cnt].x + arr[cnt].y;
v.y = 0;
}
v.pt = 6;
}
if (vis[v.x][v.y]) continue;
vis[v.x][v.y] = 1;
v.temp = arr[cnt].temp + 1;
v.pre = &arr[cnt];
if (v.x == c || v.y == c){ while(!s.empty()) s.pop();
ans = v.temp;
while(v.pre){
s.push(v.pt);
v = *v.pre;
}
return;
}
que.push(v);
}
}
} void print(){
while(!s.empty()){
int f = s.top();
s.pop();
switch(f){
case 1: printf("FILL(1)\n");break;
case 2: printf("FILL(2)\n");break;
case 3: printf("DROP(1)\n");break;
case 4: printf("DROP(2)\n");break;
case 5: printf("POUR(1,2)\n");break;
case 6: printf("POUR(2,1)\n");break;
}
}
} int main() { while(~scanf("%d%d%d", &a, &b, &c)){
ans = 0;
bfs(); if (ans == 0) printf("impossible\n");
else {
printf("%d\n", ans);
print();
}
}
return 0;
}

bfs + 路径输出的更多相关文章

  1. [POJ] 1606 Jugs(BFS+路径输出)

    题目地址:http://poj.org/problem?id=1606 广度优先搜索的经典问题,倒水问题.算法不需要多说,直接BFS,路径输出采用递归.最后注意是Special Judge #incl ...

  2. POJ-3984.迷宫问题(BFS + 路径输出)

    昨天中午做的这道题,结果蛙了一整天,就因为一行代码困住了,今天算是见识到自己有多菜了.流泪.jpg 本题大意:给一个5 * 5的迷宫,1表示墙壁,0表示通路,从左上角走到右下角并输出路径. 本题思路: ...

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

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

  4. Pots(POJ - 3414)【BFS 寻找最短路+路径输出】

    Pots(POJ - 3414) 题目链接 算法 BFS 1.这道题问的是给你两个体积分别为A和B的容器,你对它们有三种操作,一种是装满其中一个瓶子,另一种是把其中一个瓶子的水都倒掉,还有一种就是把其 ...

  5. Poj3984 迷宫问题 (BFS + 路径还原)

    Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, ...

  6. POJ-3894 迷宫问题 (BFS+路径还原)

    定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...

  7. 洛谷 P2764 最小路径覆盖问题【最大流+拆点+路径输出】

    题目链接:https://www.luogu.org/problemnew/show/P2764 题目描述 «问题描述: 给定有向图G=(V,E).设P 是G 的一个简单路(顶点不相交)的集合.如果V ...

  8. java实现将指定文件夹里所有文件路径输出到指定文件作为参数化文件给lr脚本使用

    java实现将指定文件夹里所有文件路径输出到指定文件作为参数化文件给lr脚本使用 import java.io.BufferedReader; import java.io.BufferedWrite ...

  9. Floyd最短路(带路径输出)

    摘要(以下内容来自百度) Floyd算法又称为插点法,是一种利用动态规划的思想寻找给定的加权图中多源点之间最短路径的算法,与Dijkstra算法类似. 该算法名称以创始人之一.1978年图灵奖获得者. ...

随机推荐

  1. java 标准流

    标准输入流:  System.in   默认表示的是键盘录入    标准输出流:  System.out  默认表示的是屏幕输出 Eg: package june6D; import java.io. ...

  2. js执行代码顺序

    之前一直停留在主线程先执行,异步后执行的浅理解中,后来随着不断的学习,才渐渐意识到这里面还是有点复杂的,不过我不打算写很多.一幅图来说明这个复杂的关系 processTick可理解为node中的延时器 ...

  3. Intellij IDEA 如何使用Jrebel热部署

    My Jrebel 已经不再提供免费使用了,或使用破解版Jrebel 或可以移步至 HotSwapAgent,或使用破解的Jrebel. 一个java web项目,在写的过程中我们需要不断调试,如果没 ...

  4. Nginx的三种应用场景介绍

    配置虚拟主机 就是在一台服务器启动多个网站. 如何区分不同的网站: 1.域名不同 2.端口不同 1.1. 通过端口区分不同虚拟机 Nginx的配置文件: /usr/local/nginx/conf/n ...

  5. 快排java代码

    定一个基准位,递归左右两边排序. public void fun(){ int arr[] = {2,3,4,5,6,7,822,3,4,5,8,6,5,4,2,1}; //System.out.pr ...

  6. Delta Lake基础操作和原理

    目录 Delta Lake 特性 maven依赖 使用aws s3文件系统快速启动 基础表操作 merge操作 delta lake更改现有数据的具体过程 delta表schema 事务日志 delt ...

  7. MySQL 命令行(转)

    1.登录mysql 本地:mysql -u root -p, 回车后输入密码; 也可以p后不加空格,直接加密码.回车就登录了 远程:mysql -hxx.xx.xx.xx -u -pxxx 2.查看数 ...

  8. freemarker<三>

    前两篇博客介绍了freemaker是什么以及简单的语法规则,下面我们通过实现一个demo来看在实际应用中如何使用freemaker,本篇博客主要介绍freemaker与spring的整合. 需要的Ja ...

  9. 【Linux】查看系统资源及相关信息

    查看系统信息: uname -a # 查看Linux内核版本信息 cat /proc/version # 查看内核版本 cat /etc/issue # 查看系统版本 lsb_release -a # ...

  10. torch or numpy

    黄色:重点 粉色:不懂 Torch 自称为神经网络界的 Numpy, 因为他能将 torch 产生的 tensor 放在 GPU 中加速运算 (前提是你有合适的 GPU), 就像 Numpy 会把 a ...