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. C#将可编译为本地机器码

    微软宣布了.net native的开发者预览版,详见这里. 这是一个大家期待了很多年的特性.每年在技术论坛上都有无数的人问,C#能否编译成本地机器码. 有了这个特性之后,更多开发商会开始选择C#来开发 ...

  2. codeforce 382 div2 E —— 树状dp

    题意:给一棵n个结点的无根树染色,求使每个结点距离为k的范围内至少有一个被染色的结点的总染色方法数目 分析:首先我们定义: 对于结点v, 如果存在一个黑色结点u距离v不超过k,则结点v被“控制” 首先 ...

  3. linux 在 1 MB 之下的 ISA 内存

    一个最著名的 I/O 内存区是在个人计算机上的 ISA 范围. 这是在 640 KB(0xA0000)和 1 MB(0x100000)之间的内存范围. 因此, 它正好出现于常规内存 RAM 中间. 这 ...

  4. Fetch 记录

    encodeURI()不会对本身属于URI的特殊字符进行编码,例如冒号.正斜杠.问号和井字号:而encodeURIComponent()则会对它发现的任何非标准字符进行编码. Fetch 请求body ...

  5. XSS攻击及防范

    1.什么是XSS攻击 跨站脚本攻击(Cross Site Scripting),攻击者往Web页面里插入恶意Script代码,当用户浏览该页之时,嵌入其中Web里面的Script代码会被执行,从而达到 ...

  6. Servlet request 面试题

    使用request获得请求行:String getmethod():获得请求的资源:String getcontextpath():----web应用名称request是一个域对象request完成请 ...

  7. python列表(list)

    #str #类,字符串 #name = "raitorei" #创建一个对象 #list #类,列表 ##############list类中提供的方法(灰魔法)######### ...

  8. 【一起学源码-微服务】Nexflix Eureka 源码八:EurekaClient注册表抓取 精妙设计分析!

    前言 前情回顾 上一讲 我们通过单元测试 来梳理了EurekaClient是如何注册到server端,以及server端接收到请求是如何处理的,这里最重要的关注点是注册表的一个数据结构:Concurr ...

  9. Python3使用Pyintaller-打包成exe

    Pyinstaller打包exe执行文件 安装Pyinstaller 使用pip安装Pyinstaller 用管理员模式运行cmd,输入命令: pip install pyinstaller 此方法会 ...

  10. 原生JS数组方法实现(一)————push()、unshift()、pop()和shift()

    push 向数组末尾添加一个或多个元素,并返回数组新的长度 ```javascript function push(){ for(let i=0;i<arguments.length;i++){ ...