poj3414(bfs)
来源:互联网 发布:arm单片机介绍 编辑:程序博客网 时间:2024/06/05 20:26
http://poj.org/problem?id=3414
Description
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
6FILL(2)POUR(2,1)DROP(1)POUR(2,1)FILL(2)POUR(2,1)
9216886
friends
POJ
3414
Accepted
32
0.9
2033
C++
2017-05-28 08:11:32
9216885
friends
POJ
3414
Memory Limit Exceeded
2036
C++
2017-05-28 08:10:48
9216883
friends
POJ
3414
Time Limit Exceeded
1936
C++
2017-05-28 08:00:48
9216881
friends
POJ
3414
Wrong Answer
2118
C++
2017-05-28 07:55:25
9216880
friends
POJ
3414
Wrong Answer
2120
C++
2017-05-28 07:54:53
9216879
friends
POJ
3414
Compile Error
2141
C++
2017-05-28 07:54:28
常见的智力题?!c-wa-t-m-a wa题日常啊
题意:给定两个水杯的容量,及目标容量,如果能够达到目标容量打印步骤,否则输出impossible。题解:从0,0出发枚举六种倒水情况,如果水量情况未出现,就放入队列并标记
一路做下来还算顺利。但关键是将各个状态关联起来,那么可以通过保存父亲节点来实现(①保存父亲节点的地址 ②保存父亲节点的下标)
一开始没开数组,也没用头尾指针,while里面放的是队列判空,用的局部变量当的父亲节点,秀智商啊。。。
标记水量当时make_pair+set,其实一个二维的vis就够了。
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <map>#include <queue>#include <set>using namespace std;int a,b,c;struct node{int watera;int waterb;int way;int step;//保存父亲节点,也可以通过保存父亲节点的数组下标 int一个pre node* pre; };node next[10005];//打印链表 void print(node* n){if(n->pre!=NULL){print(n->pre);if(n->way==1||n->way==2){printf("FILL(%d)\n",n->way);}else if(n->way==3||n->way==4){printf("DROP(%d)\n",n->way-2);}else if(n->way==5){printf("POUR(1,2)\n");}else if(n->way==6){printf("POUR(2,1)\n");}}}int bfs(){//根节点初始化 memset(next,0,sizeof(next));next[0].watera=0;next[0].waterb=0;next[0].step=0;next[0].pre=NULL;queue<node>q;q.push(next[0]);set<pair<int,int> >s;set<pair<int,int> >::iterator it; //标记已经出现过水量的情况 s.insert(make_pair(0,0));//首尾指针 hand是父亲节点,子节点尾插在enint hand=0,en=1;while(hand<en){next[hand]=q.front();q.pop();if(next[hand].watera==c||next[hand].waterb==c){printf("%d\n",next[hand].step);print(&next[hand]);return 1;}//枚举六种情况 for(int i=1;i<=6;i++){next[en]=next[hand];//FILL(1)if(i==1){next[en].watera=a;}//FILL(2)if(i==2){next[en].waterb=b;}//DROP(1)if(i==3){next[en].watera=0;}//DROP(2)if(i==4){next[en].waterb=0;}//POUR(1,2)if(i==5){ if(next[en].watera+next[en].waterb>b){next[en].watera-=(b-next[en].waterb);next[en].waterb=b;}else{next[en].waterb+=next[en].watera;next[en].watera=0;}}//POUR(2,1)if(i==6){ if(next[en].watera+next[en].waterb>a){next[en].waterb-=(a-next[en].watera);next[en].watera=a;}else{next[en].watera+=next[en].waterb;next[en].waterb=0;}}it=s.find(make_pair(next[en].watera,next[en].waterb));//如果当前的水量情况没有出现过,就尾插当前结点 if(it==s.end()){next[en].step++;next[en].pre=&next[hand];next[en].way=i; //标记当前结点的水量情况 s.insert(make_pair(next[en].watera,next[en].waterb));q.push(next[en++]);}}//父亲节点的六种情况枚举完了,头指针后移 hand++;}return 0;}int main(){scanf("%d%d%d",&a,&b,&c); int num=bfs();if(num==0){puts("impossible");}return 0;}
- POJ3414 BFS
- poj3414(BFS)
- poj3414 bfs()
- poj3414(bfs)
- POJ3414解题报告 BFS
- POJ3414 Pots(BFS)
- poj3414 Pots(BFS)
- BFS—POJ3414-Pots
- POJ3414 Pots BFS
- POJ3414(BFS+[手写队列])
- POJ3414 Pots(BFS)
- poj3414 Pots BFS
- 【POJ3414】Pots(BFS)
- poj3414 Pots(bfs)
- poj3414--Pots(bfs,记录路径)
- poj3414(bfs广度优先搜索)
- POJ3414 模拟倒水问题 BFS记录路径
- poj3414(用bfs模拟并回溯)
- 数据结构与算法
- dpdk ring 性能测试
- 交警罚没系统数据一致性问题
- Shell杂记
- 滴滴新锐计划实习生面试经历
- poj3414(bfs)
- [POJ3090]Visible Lattice Points
- 百度华为实习生面试经历
- 几种脱机环境下安装依赖的方法
- BZOJ3437 小P的牧场 斜率优化
- java基础篇(四)——C中struct和java中class
- hashcode方法
- java内存泄漏
- struts2第三讲笔记,登录界面实例,以.action结尾