poj 3414 Pots(BSF + 记录路径)
来源:互联网 发布:手机dns软件 编辑:程序博客网 时间:2024/06/06 02:02
Pots
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15972 Accepted: 6730 Special Judge
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
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
题意:
有两个水杯,可以倒水,有三种操作
1. fill(i)加满i号杯子
2. drop(i)倒掉i号杯子里的水
3. pour(i,j)把i号杯子里的水倒向j号杯子
问最后能不能配出c体积的水,如果能配出,则配出c体积水需要最少多少步,并输入步骤
- 分析:
该题目是个BFS搜索题目,搜索过程不难,难在如何记录路径,刚刚学习了一下。用一个数组,记录整棵遍历的树,每个结点记录父结点,找到符合要求的点,则顺着树枝找到根节点,并把途中所有的结点放入到栈中,最后输出栈。其中用了一个type遍历,记录了该结点是由什么操作变过来的,这样便可以输出路径了。
- 代码如下:
#include <stdio.h>#include <string.h>#include <queue>#include <stack>using namespace std;typedef struct{ int a,b; int i,pre; int type; int t;}node;int a,b,c;int n;int vis[105][105];queue <node>q;stack <node>s;node ans[10005];int num=0;void init(){ while(!q.empty()) q.pop();}node bfs(){ int i,j; vis[0][0]=1; node temp; temp.a = 0; temp.b = 0; temp.i = 0; temp.t = 0; temp.pre = -1; q.push(temp); while(!q.empty()) { temp = q.front(); q.pop(); //printf("<%d %d %d>\n",temp.a,temp.b,temp.t); if(temp.a == c || temp.b == c) return temp; node peace; peace.pre = temp.i; peace.t = temp.t+1; if(temp.a!=a) { //a加满 type = 1 peace.a = a; peace.b = temp.b; if(!vis[peace.a][peace.b]) { vis[peace.a][peace.b]=1; peace.type = 1; peace.i = ++num; ans[peace.i] = peace; q.push(peace); } //b倒向a type = 2 peace.a = (temp.a + temp.b) > a ? a : (temp.a + temp.b); peace.b = temp.b - (peace.a - temp.a); if(!vis[peace.a][peace.b]) { vis[peace.a][peace.b]=1; peace.type = 2; peace.i = ++num; ans[peace.i] = peace; q.push(peace); } } if(temp.b!=b) { //b加满 type = 3 peace.a = temp.a; peace.b = b; if(!vis[peace.a][peace.b]) { vis[peace.a][peace.b]=1; peace.type = 3; peace.i = ++num; ans[peace.i] = peace; q.push(peace); } //a倒向b type = 4 peace.b = (temp.a + temp.b) > b ? b : (temp.a + temp.b); peace.a = temp.a - (peace.b - temp.b); if(!vis[peace.a][peace.b]) { vis[peace.a][peace.b]=1; peace.type = 4; peace.i = ++num; ans[peace.i] = peace; q.push(peace); } } if(temp.a!=0) { //a清空 type = 5 peace.a = 0; peace.b = temp.b; if(!vis[peace.a][peace.b]) { vis[peace.a][peace.b]=1; peace.type = 5; peace.i = ++num; ans[peace.i] = peace; q.push(peace); } } if(temp.b!=0) { //清空b type = 6 peace.a = temp.a; peace.b = 0; if(!vis[peace.a][peace.b]) { vis[peace.a][peace.b]=1; peace.type = 6; peace.i = ++num; ans[peace.i] = peace; q.push(peace); } } } temp.t = -1; return temp;}int main(){ while(scanf("%d %d %d",&a,&b,&c)!=EOF) { node temp; memset(vis,0,sizeof(vis)); init(); temp = bfs(); if(temp.t==-1) { printf("impossible\n"); } else { printf("%d\n",temp.t); while(temp.i!=0) { s.push(temp); temp = ans[temp.pre]; } while(!s.empty()) { temp = s.top(); s.pop(); if(temp.type == 1) printf("FILL(1)\n"); else if(temp.type == 2) printf("POUR(2,1)\n"); else if(temp.type == 3) printf("FILL(2)\n"); else if(temp.type == 4) printf("POUR(1,2)\n"); else if(temp.type == 5) printf("DROP(1)\n"); else printf("DROP(2)\n"); } } } return 0;}
- poj 3414 Pots(BSF + 记录路径)
- POJ 3414 Pots【BFS+记录路径】
- poj 3414 Pots (bfs+路径记录)
- POJ 3414 Pots(BFS记录路径)
- POJ 3414 Pots (BFS + 记录路径)
- poj-3414-Pots【BFS+记录路径】
- poj 3414 Pots (BFS + 记录路径)
- POJ 3414 Pots(模拟bfs+路径记录)
- POJ 3414 Pots(bfs+路径记录)
- 【POJ 3414】 Pots (bfs + 模拟 + 路径记录)
- poj 3414 pots -- 最爱的 BFS (记录路径)
- POJ 3414 Pots 记录路径的广搜
- POJ 3414 Pots(隐式图的遍历+记录路径)
- POJ 3414 Pots (BFS记录最短路径)
- POJ 3414 pots 详解(记录路径+非常可乐+最快路径模板)
- POJ 3414 Pots ( BFS , 打印路径 )
- POJ 3414--Pots(BFS+回溯路径)
- POJ -3414 Pots(BFS + 打印路径)
- 技能库--线上jvm内存对象分析(137)
- 百度ueditor富文本编辑器插入html代码问题
- Code Forces 587 A. Duff and Weight Lifting
- 【CodeForces 479C】 Exams(排序)
- Magic Record 使用记录
- poj 3414 Pots(BSF + 记录路径)
- .NET 同步与异步 之 警惕闭包(十)
- TabLayout跟ViewPager进行关联
- Android Webview无法播放美拍网或者优酷网视频,其他视频没问题解决办法。(网页可能暂时无法打开或已永久移至新地址)
- 关联矩阵
- 51nod 1116 K进制下的大数
- 洛谷1341 无序字母对 欧拉路
- VMware与锐捷的矛盾解决办法
- SAPUI5 (25)