poj 3414 Pots(广搜BFS+路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents
题目链接:http://poj.org/problem?id=3414
此题和poj1606一样 :http://blog.csdn.net/u012860063/article/details/37772275
题目大意:
有二个水壶,对水壶有三种操作:
1)FILL(i),将i水壶的水填满;
2)DROP(i),将水壶i中的水全部倒掉;
3)POUR(i,j)将水壶i中的水倒到水壶j中,若水壶 j 满了,则 i 剩下的就不倒了,问进行多少步操作,并且怎么操作,输出操作的步骤,两个水壶中的水可以达到C这个水量。如果不可能则输出impossible。初始时两个水壶是空的,没有水。
思路:
直接BFS就可以了。但是难点是记录路径。
我的方法是用一个 path[]结构体数组记录入队的节点,用pre记录其前一步的下标,然后输出的时候,从最后一个状态一直找到开始状态。
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <stack> 6 using namespace std; 7 #define N 10000 8 9 int a,b,c; 10 struct node 11 { 12 int l,r,step,flag;//l为左容器,r右容器,step步数,flag为操作 13 int pre;//记录前驱 14 }; 15 bool vis[250][250];//访问标记数组 16 stack<int> s; 17 void print() 18 { 19 while (!s.empty()) 20 { 21 int i=s.top(); 22 switch (i) 23 { 24 case 0: 25 printf("FILL(1)\n"); 26 break; 27 case 1: 28 printf("FILL(2)\n"); 29 break; 30 case 2: 31 printf("DROP(1)\n"); 32 break; 33 case 3: 34 printf("DROP(2)\n"); 35 break; 36 case 4: 37 printf("POUR(2,1)\n");//注意审题,是从左边倒到右边!!! 38 break; 39 case 5: 40 printf("POUR(1,2)\n"); 41 break; 42 } 43 s.pop();//记得出栈!!! 44 } 45 } 46 void bfs() 47 { 48 node n; 49 n.l=n.r=n.step=0; 50 n.flag=7; 51 n.pre=-1; 52 queue<node> q; 53 q.push(n); 54 node path[N]; 55 int ind=0;//注意要给初值 56 memset(vis,0,sizeof(vis)); 57 while (!q.empty()) 58 { 59 path[ind]=q.front(); 60 ind++; 61 n=q.front(); 62 vis[n.l][n.r]=1; 63 q.pop(); 64 int i; 65 node nn; 66 for (i=0;i<6;i++) 67 { 68 switch(i) 69 { 70 case 0: 71 nn.l=a; 72 nn.r=n.r; 73 nn.flag=0; 74 break; 75 case 1: 76 nn.l=n.l; 77 nn.r=b; 78 nn.flag=1; 79 break; 80 case 2: 81 nn.l=0; 82 nn.r=n.r; 83 nn.flag=2; 84 break; 85 case 3: 86 nn.l=n.l; 87 nn.r=0; 88 nn.flag=3; 89 break; 90 case 4: 91 nn.l=min(a,n.r+n.l); 92 nn.r=max(0,n.r-(a-n.l)); 93 nn.flag=4; 94 break; 95 case 5: 96 nn.l=max(0,n.l-(b-n.r)); 97 nn.r=min(b,n.r+n.l); 98 nn.flag=5; 99 break; 100 } 101 nn.step=n.step+1; 102 nn.pre=ind-1;//不是ind,因为ind之前已经自加过了!!! 103 if (nn.l==c||nn.r==c) 104 { 105 printf("%d\n",nn.step); 106 while (nn.pre!=-1) 107 { 108 s.push(nn.flag); 109 nn=path[nn.pre]; 110 } 111 print(); 112 return; 113 } 114 if (vis[nn.l][nn.r]==1) 115 { 116 continue; 117 } 118 vis[nn.l][nn.r]=1; 119 q.push(nn); 120 } 121 } 122 printf("impossible\n"); 123 } 124 125 int main() 126 { 127 while (scanf("%d %d %d",&a,&b,&c)!=EOF) 128 { 129 bfs(); 130 } 131 132 return 0; 133 }
转载于:https://www.cnblogs.com/hemeiwolong/p/9347810.html
poj 3414 Pots(广搜BFS+路径输出)相关推荐
- poj 1606 Jugs(广搜BFS+路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj.org/problem?id=1606 此题和poj ...
- POJ 3414 Pots(深搜并打印路径)
POJ 3414 Pots(深搜并打印路径) You are given two pots, having the volume of A and B liters respectively. The ...
- POJ 3414 Pots【BFS】+ Python
原题链接: 3414 -- Pots 参考资料:POJ 3414 - Pots | 眈眈探求 POJ 3414 Pots[BFS][图搜] - it610.com 一 特别注意: 1. 每一种操作对应 ...
- POJ 3414 Pots(罐子)
POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ...
- hdu-2612-Find a way(广搜,bfs)
Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year ...
- POJ 3126 Prime Path 简单广搜(BFS)
题意:一个四位数的质数,每次只能变换一个数字,而且变换后的数也要为质数.给出两个四位数的质数,输出第一个数变换为第二个数的最少步骤. 利用广搜就能很快解决问题了.还有一个要注意的地方,千位要大于0.例 ...
- 深搜DFS\广搜BFS 图初步入门
首先,不管是BFS还是DFS,由于时间和空间的局限性,它们只能解决数据量比较小的问题. 深搜,顾名思义,它从某个状态开始,不断的转移状态,直到无法转移,然后退回到上一步的状态,继续转移到其他状态,不断 ...
- POJ 3414 Pots
题目链接 Description You are given two pots, having the volume of A and B liters respectively. The follo ...
- 算法学习笔记(六) 二叉树和图遍历—深搜 DFS 与广搜 BFS
图的深搜与广搜 复习下二叉树.图的深搜与广搜. 从图的遍历说起.图的遍历方法有两种:深度优先遍历(Depth First Search), 广度优先遍历(Breadth First Search),其 ...
最新文章
- [51nod] 1301 集合异或和
- 上周热点回顾(7.9-7.15)
- leetcode面试题 08.03. 魔术索引(二分)
- java数学计算表达式_Java初学者:内建函数计算简单的数学表达式
- Safari 时间格式转换 NAN 问题
- 【华为云技术分享】一文讲清C语言核心要点
- [Java] 蓝桥杯ADV-147 算法提高 学霸的迷宫
- 太强了,神州7号发射flash全程模拟!
- CocosCreator之KUOKUO带你简单使用Spine骨骼动画
- 应用matlab快速实现实验对象随机分组,应用MATLAB快速实现实验对象随机分组
- 拳王公社:最新虚拟资源项目赚钱成交系统,1.2W字干货大揭秘!
- hibernate报错could not insert
- Vmware+Ubuntu18.04配置桥接模式,并解决虚机中网络慢的问题
- 利用ARCGIS和QGIS画等值线图
- 算法复杂度(时间频度,时间复杂度介绍计算,空间复杂度)
- lemming games 3 ! hdlbits
- 福师计算机网考,福师网院20春计算机应用基础考核答案
- 专业的地形分析处理软件SAGA(附安装包下载)
- [Kettle] CST时间格式转yyyy-MM-dd HH:mm:ss格式
- js获取当前时间与获取时间戳,时间戳转换时间和时间转换时间戳
热门文章
- Docker(三)关于docker 的应用场景
- 超实用的JavaScript技巧及最佳实践
- redhat下svn服务器搭建
- 巧用负载均衡 解决数据中心三大困惑
- Selenium私房菜系列8 -- 玩转Selenium Server
- python对编写神经网络作用_神经网络(BP)算法Python实现及应用
- idea 启动tomcat 工程_如何在IDEA中创建web项目并且部署到Tomcat中
- 一天测血压的最佳时间_高血压病患者,一天之内在什么时间点测血压最好?
- 亚太数学建模竞赛优秀论文_全国大学生数学建模竞赛介绍
- 梅森旋转产生随机数c语言实现,C++生成随机数的实现代码