题目
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)

题意
给出两个罐子的容积A,B,及容积C,题目中每一个操作都代表每一步(1:向A倒满水,2:向B倒满水,3:把A的水倒空,4:把B的水倒空,5:A向B倒水[可能倒完,也可能没倒完],6:B向A倒水[可能倒完,也可能没倒完]),编写一个程序找出最短路径使A或B中的水的体积为C,如果不存在则输出impossible。

利用回溯输出
void print(int a,int b){
if(lu[a][b].a= =0 && lu[a][b].b= =0) // 到达初始状态
return ;
print(lu[a][b].sa,lu[a][b].sb);
switch(lu[a][b].way){
case 1: cout<<“FILL(1)”<<endl;break;
case 2: cout<<“FILL(2)”<<endl;break;
case 3: cout<<“DROP(1)”<<endl;break;
case 4: cout<<“DROP(2)”<<endl;break;
case 5: cout<<“POUR(1,2)”<<endl;break;
case 6: cout<<“POUR(2,1)”<<endl;break;
}
}
通过函数的递归回溯,因为当找到了最短路径后,存储的是终点,因为所走的每一步都记录了上一步的路径,可以通过自身的调用来从起点输出。

具体实现

#include <iostream>
#include <string.h>
#include <stdio.h>
#include <queue>
using namespace std;
int f[105][105];   //用来标记当前状态是否已经有过
int A,B,C;
struct node {int a,b;       //两个容器中的水int sa,sb;     //上个状态int way;       //操作1到6int step;      //当前步数
}lu[105][105];
queue<node>q;void print(int a,int b){if(lu[a][b].a==0 && lu[a][b].b==0)  // 到达初始状态return ;print(lu[a][b].sa,lu[a][b].sb);switch(lu[a][b].way){case 1: cout<<"FILL(1)"<<endl;break;case 2: cout<<"FILL(2)"<<endl;break;case 3: cout<<"DROP(1)"<<endl;break;case 4: cout<<"DROP(2)"<<endl;break;case 5: cout<<"POUR(1,2)"<<endl;break;case 6: cout<<"POUR(2,1)"<<endl;break;}
}void push(node d){    //入队操作if(f[d.a][d.b]==0){q.push(d);lu[d.a][d.b]=d;f[d.a][d.b]=1;}
}void bfs(){q.push(lu[0][0]);while(!q.empty()){node u=q.front();q.pop();if(u.a==C || u.b==C ){printf("%d\n",u.step);print(u.a,u.b);return ;}node v;v.sa=u.a;v.sb=u.b;v.step=u.step+1;if(u.a!=A){   //把1加满v.a=A;v.b=u.b;v.way=1;push(v);}if(u.b!=B){   //把2加满v.b=B;v.a=u.a;v.way=2;push(v);}if(u.a!=0){   //倒光3v.a=0;v.b=u.b;v.way=3;push(v);}if(u.b!=0){  //倒光4v.a=u.a;v.b=0;v.way=4;push(v);}if(u.a!=0 && u.b!=B){  //把1倒给2if(u.a+u.b>=B){v.b=B;v.a=u.a+u.b-B;}else{v.a=0;v.b=u.a+u.b;}v.way=5;push(v);}if(u.a!=A && u.b!=0){   //把2倒给1if(u.a+u.b>=A){v.a=A;v.b=u.a+u.b-A;}else{v.a=u.a+u.b;v.b=0;}v.way=6;push(v);}}printf("impossible\n");
}int main() {memset(f,0, sizeof(f));lu[0][0].a=lu[0][0].b=0;lu[0][0].sa=lu[0][0].sb=-1;lu[0][0].step=0;scanf("%d %d %d",&A,&B,&C);bfs();return 0;
}

pots题解(BFS简单应用)相关推荐

  1. BFS简单搜索--POJ 2243

    这题就是简单的BFS搜索,刚刚转到C++,还有很多库函数不熟悉,理解到BFS是一种奇妙的迭代法,其用的主要是队列的性质. 1 /*BFS简单搜索*/ 2 #include<iostream> ...

  2. POJ 3414 Pots【BFS】+ Python

    原题链接: 3414 -- Pots 参考资料:POJ 3414 - Pots | 眈眈探求 POJ 3414 Pots[BFS][图搜] - it610.com 一 特别注意: 1. 每一种操作对应 ...

  3. [kuangbin带你飞]专题一 做题顺序与题解 【简单搜索】

    随便说点: 博主正在刷kuangbin专题的题目,初学者,没接触过什么算法,刷题的初衷是备战蓝桥杯,后来发现了算法资料大多是针对acm的,挑选kuangbin专题入门也是如此,毕竟这样分类看起来比较有 ...

  4. Dungeon Master题解bfs

    Dungeon Master 题面翻译 题目描述 输入格式 输出格式 样例 #1 样例输入 #1 样例输出 #1 题解思路及TIME EXCEEDED问题 题解 超时题解(个人觉得没问题) 题面翻译 ...

  5. Pots(bfs)(存储路径)

    Pots 题意:输入的三个数A,B,C,A,B表示两个缸的容量,可以经过以下操作,得到任意一个缸里的水是C即可,最少的操作. 操作: FILL(1):给第一个缸添满水. FILL(2):给第二个缸添满 ...

  6. Pots 用bfs模拟2杯子体积分别为A,B通过一系列操作把其中一杯子变为C升水

    Pots POJ - 3414 给你两个罐子, 分别有A升和B升的体积 这里开始的时候A和B的杯子都是没有水的 .可以执行以下操作: 1,FILL(i)从水龙头填充锅i (1 ≤ i ≤ 2): 2, ...

  7. Pots (bfs)

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

  8. DFS BFS简单理解

    文章目录 BFS DFS介绍 实现思路 DFS BFS怎么应用 DFS BFS对比 又水了一篇博客呜呜,第一次尝试写DFS和BFS,做题也迷迷糊糊,看着大佬文章简单写了写总结,后续会补上DFS和BFS ...

  9. ACM4.3对抗赛DEF题题解(原来如此简单,超级详细入门必备)

    D:heilong的烦恼 题目传送门 题意:根据运算符决定运算,最后输出运算结果 思路:循环+数组存储 因为代码实在是太长了,我才你一定不怎么会看所以我先将核心进位代码写在这里 加法进位: c[i]= ...

最新文章

  1. TensorFlow实现one-hot编码【TensorFlow2入门手册】
  2. inx的c语言表达式,Spninx 解决的问题
  3. putty, puttycm区别
  4. ejb运行程序_在哪里可以运行EJB?
  5. mongodb mysql json数据_使用MongoDB与MySQL有很多JSON字段?
  6. 工作流实战_20_flowable 任务签收 反签收
  7. Django Form ModelForm modelfromset
  8. 秋招下半场依然没offer,怎么办?
  9. Atitit. null错误的设计 使用Optional来处理null
  10. CMMI5访谈学习笔记(项目经理角色)(转)
  11. 如何重置HDX卡的固件(firmware)
  12. python 角度变弧度_弧度制和角度值怎么转换?
  13. C# DLL HRESULT:0x8007000B
  14. 2021年 - 年终总结
  15. 什么叫创建oracle实例,请问建立数据库实例是什么意思
  16. 区块链新规,不可忽视的几个细节
  17. src // 的意思/src相对协议/src为//开头的图片怎么加载
  18. socket用法linux,linux下socket用法
  19. 深度强化学习CS285 lec13-lec15 (下)
  20. uboot和bootloader的区别

热门文章

  1. 四阶段P188~P(PJavaScript基础语法-dom-bom-js-es6新语法-jQuery- 数据可视化echarts 黑 马pink老师前端入门基础视频教程(持续更新)
  2. html5 翻牌效果,css3实现图片翻牌特效
  3. hdu2602——Bone Collector
  4. c语言与指针——(二)指针变量的定义与赋值
  5. Java相关书籍推荐
  6. 集和--迭代器Iterator
  7. 学习笔记——斜率优化dp
  8. java8 wordcount_Java8并行流写WordCount,并不简单
  9. 【Python】编程求1-1/2+1/3-1/4+。。。+1/n,n从键盘输入
  10. 珍爱生命急救与自救学习笔记