POJ 3414 Pots(深搜并打印路径)

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)

解题思路:

1.两遍dfs,第一遍找到最小的倒水次数,第二遍打印路径
2.倒水问题:模拟倒水的6种情况

#include<iostream>//代码稍微有点长,但是思路很简单
#include<cstdlib>
#include<cstring>
#include<stack>
#define INF 0x7fffffff
using namespace std;
typedef struct node{int x;//操作x==1代表操作FILL,x==2代表操作DROPint y;//代表操作的参数如x==1,y==2则代表FILL(2)int z;
}Node;
Node temp;
stack<Node>s;//利用栈记录路径
int book[110][110];//标记数组
int flag=0;
int mins=INF;
int a,b,tol;
void dfs(int x,int y,int sum,int f,int fx,int fy) {//参数x,y记录第1个和第2个水杯的水量,sum代表操作次数,//f代表当前操作,fx,fy是操作的两个参数if(x==tol||y==tol) {//当符合情况时更新最小值if(sum<mins){mins=sum;}return;}int xx,yy,ff,ffx,ffy;if(x!=0) {//第一种情况,当第一个水杯有水时全部倒出DROP(1)xx=0;yy=y;ff=2;//记录是第几个操作ffx=1;//记录操作的第一个参数ffy=0;//记录操作的第二个参数(只有在POUR操作下才有)if(book[xx][yy]==0){//没出现过此种状态book[xx][yy]=1;dfs(xx,yy,sum+1,ff,ffx,ffy);book[xx][yy]=0;}}if(y!=0) {//第2种情况,当第2个水杯有水时全部倒出DROP(2)xx=x;yy=0;ff=2;ffx=2;ffy=0;if(book[xx][yy]==0){book[xx][yy]=1;dfs(xx,yy,sum+1,ff,ffx,ffy);book[xx][yy]=0;}}if(x!=a) {第3种情况,当第1个水杯没有装满时倒满水FILL(1)xx=a;yy=y;ff=1;ffx=1;ffy=0;if(book[xx][yy]==0){book[xx][yy]=1;dfs(xx,yy,sum+1,ff,ffx,ffy);book[xx][yy]=0;}}if(y!=b) {//第4种情况,当第2个水杯没有装满时倒满水FILL(2)xx=x;yy=b;ff=1;ffx=2;ffy=0;if(book[xx][yy]==0){book[xx][yy]=1;dfs(xx,yy,sum+1,ff,ffx,ffy);book[xx][yy]=0;}}if(x!=a&&y!=0) {//第5种情况,当第1个水杯不满,第2个水杯有水时,//将第二个水杯的水倒入第一个水杯POUR(2,1)xx=min(x+y,a);yy=y-(xx-x);ff=3;ffx=2;ffy=1;if(book[xx][yy]==0){book[xx][yy]=1;dfs(xx,yy,sum+1,ff,ffx,ffy);book[xx][yy]=0;}}if(x!=0&&y!=b) {第6种情况,当第2个水杯不满,第1个水杯有水时,//将第1个水杯的水倒入第2个水杯POUR(1,2)yy=min(x+y,b);xx=x-(yy-y);ff=3;ffx=1;ffy=2;if(book[xx][yy]==0){book[xx][yy]=1;dfs(xx,yy,sum+1,ff,ffx,ffy);book[xx][yy]=0;}}return;
}
void prints(int f,int fx,int fy){//打印一步的操作if(f==1){cout<<"FILL("<<fx<<")"<<endl;}else if(f==2){cout<<"DROP("<<fx<<")"<<endl;}else if(f==3){cout<<"POUR("<<fx<<","<<fy<<")"<<endl;}
}
void print(int x,int y,int sum,int f,int fx,int fy) {//寻找路径并记录路径if(x==tol||y==tol) {if(sum==mins){//当达到最小值时记录此路径flag=1;}return;}int xx,yy,ff,ffx,ffy;if(x!=0) {//和上边的dfs思路一样,只不过多了一个判断xx=0;yy=y;ff=2;ffx=1;ffy=0;if(book[xx][yy]==0){book[xx][yy]=1;print(xx,yy,sum+1,ff,ffx,ffy);book[xx][yy]=0;if(flag==1){//多个这个判断temp.x=ff;temp.y=ffx;temp.z=ffy;s.push(temp);return;}}}if(y!=0) {xx=x;yy=0;ff=2;ffx=2;ffy=0;if(book[xx][yy]==0){book[xx][yy]=1;print(xx,yy,sum+1,ff,ffx,ffy);book[xx][yy]=0;if(flag==1){temp.x=ff;temp.y=ffx;temp.z=ffy;s.push(temp);return;}}}if(x!=a) {xx=a;yy=y;ff=1;ffx=1;ffy=0;if(book[xx][yy]==0){book[xx][yy]=1;print(xx,yy,sum+1,ff,ffx,ffy);book[xx][yy]=0;if(flag==1){temp.x=ff;temp.y=ffx;temp.z=ffy;s.push(temp);return;}}}if(y!=b) {xx=x;yy=b;ff=1;ffx=2;ffy=0;if(book[xx][yy]==0){book[xx][yy]=1;print(xx,yy,sum+1,ff,ffx,ffy);book[xx][yy]=0;if(flag==1){temp.x=ff;temp.y=ffx;temp.z=ffy;s.push(temp);return;}}}if(x!=a&&y!=0) {xx=min(x+y,a);yy=y-(xx-x);ff=3;ffx=2;ffy=1;if(book[xx][yy]==0){book[xx][yy]=1;print(xx,yy,sum+1,ff,ffx,ffy);book[xx][yy]=0;if(flag==1){temp.x=ff;temp.y=ffx;temp.z=ffy;s.push(temp);return;}}}if(x!=0&&y!=b) {yy=min(x+y,b);xx=x-(yy-y);ff=3;ffx=1;ffy=2;if(book[xx][yy]==0){book[xx][yy]=1;print(xx,yy,sum+1,ff,ffx,ffy);book[xx][yy]=0;if(flag==1){temp.x=ff;temp.y=ffx;temp.z=ffy;s.push(temp);return;}}}return;
}
int main() {cin>>a>>b>>tol;memset(book,0,sizeof(book));book[0][0]=1;dfs(0,0,0,0,0,0);//寻找最少倒换几次if(mins!=INF){cout<<mins<<endl;memset(book,0,sizeof(book));book[0][0]=1;print(0,0,0,0,0,0);//寻找路径while(!s.empty()){//输出路径temp=s.top();s.pop();prints(temp.x,temp.y,temp.z);}}else{cout<<"impossible"<<endl;}return 0;
}

POJ 3414 Pots(深搜并打印路径)相关推荐

  1. POJ 3414 Pots【BFS】+ Python

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

  2. POJ 3414 Pots(罐子)

    POJ 3414 Pots(罐子) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 You are given two po ...

  3. poj 1950 Dessert 深搜

    题意: 给n,问有多少和由1,2...n,和'+','-','.'(表示连接,4.5表示45)组成,值为0的表达式. 分析: 深搜,因为要枚举到"连接"的情况,所以传送pre表示上 ...

  4. I - 滑雪 POJ - 1088(深搜,记忆化搜索)

    Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡. ...

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

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

  6. POJ 3414 Pots【广搜】

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

  7. POJ 3414 Pots

    题目链接 Description You are given two pots, having the volume of A and B liters respectively. The follo ...

  8. poj 3414 Pots BFS

    参考文章:思路公式 代码思路 原来的代码有点瑕疵,这是最新的AC代码 #pragma warning(disable:4996) #include<iostream> #include&l ...

  9. [数据结构] 迷宫问题(栈和队列,深搜和广搜)

    代码: #include <iostream> #include <string.h> #include <stack> #include <queue> ...

最新文章

  1. 底部菜单_css实现移动端底部导航菜单隆起效果
  2. Python可视化应用实战-如何制作酷炫的图表?
  3. MATLAB基本操作(一):MATLAB中变量的文件存储
  4. python深入与提高_关于提高Python计算性能的说明摘要,深入,提升,python,笔记,小结,不,定时,更新...
  5. python实现基于八方向判断的断裂连接
  6. highcharts总结
  7. 传输协议上的字节解析问题
  8. Java – 2012年回顾和未来预测
  9. sklearn adaboost_集成学习-从零推导和实现adaboost与3D可视化
  10. 上位机和下位机基础概念
  11. 华为认证考试方式有哪些?华为认证考试怎么考?
  12. 8-1 职场价值塑造-摆脱低价值瓶颈,展示高价值收获新机会
  13. Word插入Excel的时候报错:用于创建此对象的程序是Excel
  14. Machine Learning读书会,面试算法讲座,创业活动,算法班(15年6月)
  15. 解决CCS闪退问题(亲测有效)
  16. 【论文笔记】MGU-Net
  17. Ubuntu18.04+Nvidia RTX 3060+Pytorch配置GPU环境
  18. HDU 5594(ZYB's Prime-网络流)
  19. 头文件中能否进行函数的定义
  20. 2018年上半年阅读书单

热门文章

  1. 2020年信息系统项目管理师考试通过反思
  2. 软件的力量,数字经济时代“海一样的力量”
  3. ubuntu20.04安装haproxy-2.5-dev3
  4. Stream流(工厂的流水线)
  5. 计算机一级b证书图片p,只需两步,分分钟搞定证件照(内附福利)
  6. unbuntu安装google浏览器和谷歌浏览器驱动
  7. 发篇面经,感谢北邮人对我的帮助~(摘自北邮人论坛)
  8. 【Docker 完整版教程笔记】
  9. 大数据时代推进服装行业“新零售”的发展
  10. 后台基于elment-dialog展示打卡定位腾讯地图