(POJ3414)Pots BFS, 记录路径

2024-02-05 02:32
文章标签 路径 记录 bfs pots poj3414

本文主要是介绍(POJ3414)Pots BFS, 记录路径,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Pots
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)

Source

Northeastern Europe 2002, Western Subregion

题意:
有两个锅1和2,容量分别为A,B,开始时都是空的。你可以做以下操作:
装满1或2
倒空1或2
将1倒进2(可能有剩余),将2倒进1
问最少多少步,可以使1或2中的容量为C

分析:
是一道BFS的题,不过要记录路径,所有这样定义状态:
struct node
{
int id,pre;//编号,上一个状态的编号
int a,b,step;
int op;//由上一状态到达这一状态操作的种类
}nodes[10010];
然后找到结果后输出即可。

没有1AC,将drop1写错了,当时样例过了。。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;int A,B,C;
struct node
{int id,pre;int a,b,step;int op;
}nodes[10010];bool v[110][110];
int ans[10010];void output(int x)
{int num=0;for(int i=nodes[x].id;i!=-1;i=nodes[i].pre){//cout<<nodes[i].a<<" "<<nodes[i].b<<nodes[i].op<<endl;ans[num++] = nodes[i].op;}num-=2;for(int i=num;i>=0;i--){if(ans[i]==1) printf("FILL(2)\n");else if(ans[i]==2) printf("FILL(1)\n");else if(ans[i]==3) printf("DROP(1)\n");else if(ans[i]==4) printf("DROP(2)\n");else if(ans[i]==5) printf("POUR(1,2)\n");else if(ans[i]==6) printf("POUR(2,1)\n");}
}int main()
{while(scanf("%d%d%d",&A,&B,&C)!=EOF){int num=0;bool f = false;node now;nodes[0].a=0; nodes[0].b=0; nodes[0].step=0;nodes[0].pre=-1; nodes[0].id=0; nodes[0].op=-1;num++;memset(v,0,sizeof(v));queue<node> q;while(!q.empty()) q.pop();v[0][0]=1;q.push(nodes[0]);while(!q.empty()){now = q.front(); q.pop();//cout<<now.a<<" "<<now.b<<" "<<now.step<<endl;if(now.a==C || now.b==C){f = true;printf("%d\n",now.step);output(now.id);break;}//full 2nodes[num].a = now.a; nodes[num].b = B;if(!v[nodes[num].a][nodes[num].b]){nodes[num].id = num;nodes[num].pre = now.id;nodes[num].step = now.step + 1;nodes[num].op = 1;q.push(nodes[num]);v[nodes[num].a][nodes[num].b]=1;num++;}//full 1nodes[num].a = A; nodes[num].b = now.b;if(!v[nodes[num].a][nodes[num].b]){nodes[num].id = num;nodes[num].pre = now.id;nodes[num].step = now.step + 1;nodes[num].op = 2;q.push(nodes[num]);v[nodes[num].a][nodes[num].b]=1;num++;}//drop 1nodes[num].a = 0; nodes[num].b = now.b;if(!v[nodes[num].a][nodes[num].b]){nodes[num].id = num;nodes[num].pre = now.id;nodes[num].step = now.step + 1;nodes[num].op = 3;q.push(nodes[num]);v[nodes[num].a][nodes[num].b]=1;num++;}//drop 2nodes[num].a = now.a; nodes[num].b = 0;if(!v[nodes[num].a][nodes[num].b]){nodes[num].id = num;nodes[num].pre = now.id;nodes[num].step = now.step + 1;nodes[num].op = 4;q.push(nodes[num]);v[nodes[num].a][nodes[num].b]=1;num++;}// pour 1 2int tmp = B - now.b;if(now.a <= tmp){nodes[num].a = 0;nodes[num].b = now.b + now.a;}else{nodes[num].b = B;nodes[num].a = now.a - tmp;}if(!v[nodes[num].a][nodes[num].b]){nodes[num].id = num;nodes[num].pre = now.id;nodes[num].step = now.step + 1;nodes[num].op = 5;q.push(nodes[num]);v[nodes[num].a][nodes[num].b]=1;num++;}//pour 2 1tmp = A - now.a;if(now.b <= tmp){nodes[num].b = 0;nodes[num].a = now.a + now.b;}else{nodes[num].a = A;nodes[num].b = now.b - tmp;}if(!v[nodes[num].a][nodes[num].b]){nodes[num].id = num;nodes[num].pre = now.id;nodes[num].step = now.step + 1;nodes[num].op = 6;q.push(nodes[num]);v[nodes[num].a][nodes[num].b]=1;num++;}}if(!f) printf("impossible\n");}return 0;
}

这篇关于(POJ3414)Pots BFS, 记录路径的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/679513

相关文章

hdu1254(嵌套bfs,两次bfs)

/*第一次做这种题感觉很有压力,思路还是有点混乱,总是wa,改了好多次才ac的思路:把箱子的移动当做第一层bfs,队列节点要用到当前箱子坐标(x,y),走的次数step,当前人的weizhi(man_x,man_y),要判断人能否将箱子推到某点时要嵌套第二层bfs(人的移动);代码如下:

hdu2544(单源最短路径)

模板题: //题意:求1到n的最短路径,模板题#include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#i

poj 1734 (floyd求最小环并打印路径)

题意: 求图中的一个最小环,并打印路径。 解析: ans 保存最小环长度。 一直wa,最后终于找到原因,inf开太大爆掉了。。。 虽然0x3f3f3f3f用memset好用,但是还是有局限性。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#incl

poj 2195 bfs+有流量限制的最小费用流

题意: 给一张n * m(100 * 100)的图,图中” . " 代表空地, “ M ” 代表人, “ H ” 代表家。 现在,要你安排每个人从他所在的地方移动到家里,每移动一格的消耗是1,求最小的消耗。 人可以移动到家的那一格但是不进去。 解析: 先用bfs搞出每个M与每个H的距离。 然后就是网络流的建图过程了,先抽象出源点s和汇点t。 令源点与每个人相连,容量为1,费用为

Node.js学习记录(二)

目录 一、express 1、初识express 2、安装express 3、创建并启动web服务器 4、监听 GET&POST 请求、响应内容给客户端 5、获取URL中携带的查询参数 6、获取URL中动态参数 7、静态资源托管 二、工具nodemon 三、express路由 1、express中路由 2、路由的匹配 3、路由模块化 4、路由模块添加前缀 四、中间件

POJ 3057 最大二分匹配+bfs + 二分

SampleInput35 5XXDXXX...XD...XX...DXXXXX5 12XXXXXXXXXXXXX..........DX.XXXXXXXXXXX..........XXXXXXXXXXXXX5 5XDXXXX.X.DXX.XXD.X.XXXXDXSampleOutput321impossible

记录每次更新到仓库 —— Git 学习笔记 10

记录每次更新到仓库 文章目录 文件的状态三个区域检查当前文件状态跟踪新文件取消跟踪(un-tracking)文件重新跟踪(re-tracking)文件暂存已修改文件忽略某些文件查看已暂存和未暂存的修改提交更新跳过暂存区删除文件移动文件参考资料 咱们接着很多天以前的 取得Git仓库 这篇文章继续说。 文件的状态 不管是通过哪种方法,现在我们已经有了一个仓库,并从这个仓

【408DS算法题】039进阶-判断图中路径是否存在

Index 题目分析实现总结 题目 对于给定的图G,设计函数实现判断G中是否含有从start结点到stop结点的路径。 分析实现 对于图的路径的存在性判断,有两种做法:(本文的实现均基于邻接矩阵存储方式的图) 1.图的BFS BFS的思路相对比较直观——从起始结点出发进行层次遍历,遍历过程中遇到结点i就表示存在路径start->i,故只需判断每个结点i是否就是stop

Android Environment 获取的路径问题

1. 以获取 /System 路径为例 /*** Return root of the "system" partition holding the core Android OS.* Always present and mounted read-only.*/public static @NonNull File getRootDirectory() {return DIR_ANDR

图的最短路径算法——《啊哈!算法》

图的实现方式 邻接矩阵法 int[][] map;// 图的邻接矩阵存储法map = new int[5][5];map[0] = new int[] {0, 1, 2, 3, 4};map[1] = new int[] {1, 0, 2, 6, 4};map[2] = new int[] {2, 999, 0, 3, 999};map[3] = new int[] {3, 7