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. visualStudio 无法登陆

    如果遇到 visualStudio 无法登陆,可以看下我的方法,可能有用 尝试关闭代理 打开设置.网络.代理,关了它,试试 如果遇到下面的问题: 我们无法刷新此账户的凭据 No home tenant ...

  2. 关于对height:100%的研究

    参考此链接: https://segmentfault.com/a/1190000012707337

  3. jQuery 工具类函数-URL操作函数

    调用名为$. param的工具函数,能使对象或数组按照key/value格式进行序列化编码,该编码后的值常用于向服务端发送URL请求,调用格式为: $. param (obj); 参数obj表示需要进 ...

  4. 使用 koa-router 路由拆分

    根据功能不同,将路由拆分到不同的模块 目录结构: app.js const Koa = require('koa'); const Router = require('koa-router'); co ...

  5. WNMP nginx+php5+mysql测试环境安装(Windows7)(二)

    3. 安装Zend Optimizer Zend Optimizer对那些在被最终执行之前由Run-Time Complier产生的代码进行优化,提高PHP应用程序的执行速度.一般情况下,执行使用Ze ...

  6. SPOJ - PHRASES Relevant Phrases of Annihilation (后缀数组)

    You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages ...

  7. How to parse version range

    Now we are making a solution that has to get the package reference. But the version of package refer ...

  8. WPF实现软键盘

    wpf 实现一个软键盘, 先发个图: 工作有需要实现一个软键盘,本来想用windows自带的软键盘凑合凑合得了,又觉得那个软键盘太大了,所以自己实现了一个. 说一下实现的思路,其实没什么思路 界面就是 ...

  9. unsupported jsonb version number 123

    PostgreSQL  jsonb 入库时遇到   unsupported jsonb version number 123 变通方法 insert into  htclanedata (laneda ...

  10. centos7搭建hadoop2.10伪分布模式

    1.准备一台Vmware虚拟机,添加hdfs用户及用户组,配置网络见 https://www.cnblogs.com/qixing/p/11396835.html 在root用户下 添加hdfs用户, ...