Pots(POJ - 3414)【BFS 寻找最短路+路径输出】
Pots(POJ - 3414)
算法
BFS
1.这道题问的是给你两个体积分别为A和B的容器,你对它们有三种操作,一种是装满其中一个瓶子,另一种是把其中一个瓶子的水都倒掉,还有一种就是把其中一个瓶子的水导入另一个瓶子中(可能会有剩余)。最后让你输出在能够得出体积为C的水的情况下操作的最小次数并且把过程输出。如果无法得出体积为C的水,则输出“impossible”。
2.这个题主要涉及两个点,一个是求出最小次数,还有一个就是把路径输出。对于这种有目标值的求最小次数问题,我们可以使用bfs解决。初始状态是两个瓶子都为空,最终状态是其中一个瓶子中的水的容量达到了目标值C。在每个状态下可以对瓶子进行上面描述的三种操作,细分下来其实只有6中操作,分别是:
- 将A瓶子装满水
- 将B瓶子装满水
- 将A瓶子中的水倒入B瓶子中
- 将B瓶子中的倒入A瓶子中
- 将A瓶子中的水全部抽走
- 将B瓶子中的水全部抽走
我们可以把每种状态都放入到队列中,当到达某种状态时,就分别执行上面6个操作,同时需要注意做好每种状态的标记,避免重复。
3.为了获得路径,我们可以把每次将新的状态插入队列中的同时用数组记录,可以用结构体来存放每个状态,该结构体中除了当前A瓶和B瓶中水的容量这两种属性外,还要有它本身在数组中的下标id,父状态在数组中的下标pre,到达此状态时的最小步数steps。由于bfs的特殊性,我们可以认为当达到目标值时的steps为最小最优的(至于为什么是最优的,我们可以简单的想bfs因为是一层一层的搜索的,所以可以认为第一个到达目标值的层数是最小的,当然前提是代价是一样的,比如这里每执行一步都表示一次,故可以用bfs实现,而如果执行每种操作的代价不同,就不能用bfs来实现了)。
C++代码
#include<iostream>
#include<queue>
#include<stack>
#include<cstdio>
using namespace std;
const int N = 1e6 + 10, M = 1e3;
int a, b, c;
bool st[M][M];
struct Status{
int ca, cb, pre;
int ope; //1表示装满A,2表示装满B,3表示A倒入B,4表示B倒入A,5表示倒掉A,6表示倒掉B
int steps;
int id;
Status(){
ca = 0, cb = 0, pre = -1;
ope = -1;
steps = 0;
id = 0;
}
}s[N];
int cnt;
//装满A
void fullA(Status& t)
{
t.ca = a;
t.ope = 1;
t.steps++;
t.id = cnt;
//s[cnt++] = t;
}
//倒掉A
void pourA(Status& t)
{
t.ca = 0;
t.ope = 5;
t.steps++;
t.id = cnt;
//s[cnt++] = t;
}
//装满B
void fullB(Status& t)
{
t.cb = b;
t.ope = 2;
t.steps++;
t.id = cnt;
//s[cnt++] = t;
}
//倒掉B
void pourB(Status& t)
{
t.cb = 0;
t.ope = 6;
t.steps++;
t.id = cnt;
// s[cnt++] = t;
}
//A倒入B
void pourA_B(Status& t)
{
if(t.ca >= b - t.cb)
{
t.ca -= b - t.cb;
t.cb = b;
}
else
{
t.cb += t.ca;
t.ca = 0;
}
t.ope = 3;
t.steps++;
t.id = cnt;
//s[cnt++] = t;
}
//B倒入A
void pourB_A(Status& t)
{
if(t.cb >= a - t.ca)
{
t.cb -= a - t.ca;
t.ca = a;
}
else
{
t.ca += t.cb;
t.cb = 0;
}
t.ope = 4;
t.steps++;
t.id = cnt;
//s[cnt++] = t;
}
void print(Status t)
{
cout << t.steps << endl;
stack<Status> sta;
sta.push(t);
while(true)
{
if(t.pre == -1) break;
t = s[t.pre];
sta.push(t);
}
while(sta.size())
{
Status tmp = sta.top();
sta.pop();
if(tmp.ope == 1)
puts("FILL(1)");
else if(tmp.ope == 2)
puts("FILL(2)");
else if(tmp.ope == 3)
puts("POUR(1,2)");
else if(tmp.ope == 4)
puts("POUR(2,1)");
else if(tmp.ope == 5)
puts("DROP(1)");
else if(tmp.ope == 6)
puts("DROP(2)");
}
}
void bfs()
{
Status tmp;
queue<Status> que;
que.push(tmp);
st[tmp.ca][tmp.cb] = true;
s[cnt++] = tmp;
while(que.size())
{
tmp = que.front();
que.pop();
//cout << "***" << tmp.ca << ", " << tmp.cb << endl;
if(tmp.ca == c || tmp.cb == c)
{
//cout << "**end** " << tmp.ca << " , " << tmp.cb << ", " << tmp.pre << endl;
//打印输出
print(tmp);
return ;
}
//循环那六种情况
for(int i = 1; i <= 6; i++)
{
Status tmp1 = tmp;
//cout << "tmp*** " << tmp.ca << ", " << tmp.cb << endl;
if(i == 1)
{
if(tmp1.ca == a) continue;
fullA(tmp1);
if(!st[tmp1.ca][tmp1.cb])
{
tmp1.pre = tmp.id;
st[tmp1.ca][tmp1.cb] = true;
que.push(tmp1);
s[cnt++] = tmp1;
//cout << "tmp1**1* " << tmp1.ca << ", " << tmp1.cb << endl;
}
}
else if(i == 2)
{
if(tmp1.cb == b) continue;
fullB(tmp1);
if(!st[tmp1.ca][tmp1.cb])
{
tmp1.pre = tmp.id;
st[tmp1.ca][tmp1.cb] = true;
que.push(tmp1);
s[cnt++] = tmp1;
//cout << "tmp1**2* " << tmp1.ca << ", " << tmp1.cb << endl;
}
}
else if(i == 3)
{
if(!(tmp1.ca)) continue;
pourA_B(tmp1);
if(!st[tmp1.ca][tmp1.cb])
{
tmp1.pre = tmp.id;
st[tmp1.ca][tmp1.cb] = true;
que.push(tmp1);
s[cnt++] = tmp1;
//cout << "tmp1**3* " << tmp1.ca << ", " << tmp1.cb << endl;
}
}
else if(i == 4)
{
if(!(tmp1.cb)) continue;
pourB_A(tmp1);
if(!st[tmp1.ca][tmp1.cb])
{
tmp1.pre = tmp.id;
st[tmp1.ca][tmp1.cb] = true;
que.push(tmp1);
s[cnt++] = tmp1;
//cout << "tmp1**4* " << tmp1.ca << ", " << tmp1.cb << endl;
}
}
else if(i == 5)
{
if(!(tmp1.ca)) continue;
pourA(tmp1);
if(!st[tmp1.ca][tmp1.cb])
{
tmp1.pre = tmp.id;
st[tmp1.ca][tmp1.cb] = true;
que.push(tmp1);
s[cnt++] = tmp1;
//cout << "tmp1**5* " << tmp1.ca << ", " << tmp1.cb << endl;
}
}
else if(i == 6)
{
if(!(tmp1.cb)) continue;
pourB(tmp1);
if(!st[tmp1.ca][tmp1.cb])
{
tmp1.pre = tmp.id;
st[tmp1.ca][tmp1.cb] = true;
que.push(tmp1);
s[cnt++] = tmp1;
//cout << "tmp1**6* " << tmp1.ca << ", " << tmp1.cb << endl;
}
}
}
}
puts("impossible");
}
int main()
{
cin >> a >> b >> c;
bfs();
return 0;
}
Pots(POJ - 3414)【BFS 寻找最短路+路径输出】的更多相关文章
- Pots POJ 3414
/* *POJ 3414 *简单模板bfs *编程应该为了方便理解,尽量提供接口 */ #include<cstdio> #include<algorithm> #includ ...
- Pots POJ - 3414 (搜索+记录路径)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22688 Accepted: 9626 Special J ...
- Pots POJ - 3414【状态转移bfs+回溯】
典型的倒水问题: 即把两个水杯的每种状态视为bfs图中的点,如果两种状态可以转化,即可认为二者之间可以连一条边. 有3种倒水的方法,对应2个杯子,共有6种可能的状态转移方式.即相当于图中想走的方法有6 ...
- POJ 1122 FDNY to the Rescue!(最短路+路径输出)
http://poj.org/problem?id=1122 题意:给出地图并且给出终点和多个起点,输出从各个起点到终点的路径和时间. 思路: 因为有多个起点,所以这里反向建图,这样就相当于把终点变成 ...
- POJ 3414 BFS 输出过程
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17456 Accepted: 7407 Special J ...
- POJ - 3414 bfs [kuangbin带你飞]专题一
状态搜索,每种状态下面共有六种选择,将搜索到的状态保存即可. d[i][j]表示状态A杯中水i升,B杯中水j升,总状态数量不会超过A杯的容量 * B杯的容量. AC代码 #include<cst ...
- 如何在MONO 3D寻找最短路路径
前段时间有个客户说他们想在我们的3D的机房中找从A点到B点的最短路径,然而在2D中确实有很多成熟的寻路算法,其中A*是最为常见的,而这个Demo也是用的A*算法,以下计算的是从左上角到右下角的最短路径 ...
- kuangbin专题 专题一 简单搜索 Pots POJ - 3414
题目链接:https://vjudge.net/problem/POJ-3414 题意:给你两个杯子,分别容量为A(1),B(2)和一个C,C是需要经过下列操作,得到的一个升数.(1) FILL(i) ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
随机推荐
- 快速解决Ubuntu/linux 环境下QT生成没有可执行文件(application/x-executable)
快速解决Ubuntu/linux 环境下QT生成没有可执行文件(application/x-executable)(转载) 问题描述 与windows环境下不同,linux选择debug构建时并不 ...
- sha1sum校验方法
sha1sum校验方法,我们可以保存到一个文件中.还可以根据已经得到的hash来确认文件.MD5类似. [root@ffcs211 test_dir]# sha1sum New.EXE 3fe44e8 ...
- linux下使用split命令分割文件且文件号从1开始
Linux里切割大文件的命令如下: split [OPTION] [INPUT [PREFIX]] 选项如下: -a : 指定后缀长度 -b : 每个文件多少字节 -d : 使用数字后缀而不是字母 - ...
- Vulkan相关资源
https://github.com/KhronosGroup/Khronosdotorg/blob/master/api/vulkan/resources.md Intel API without ...
- 安装cnpm设置npm淘宝镜像源
安装cnpm npm install -g cnpm 验证npm镜像源 npm config get registry 题外话:cnpm和npm区别? cnpm其实就是在npm的基础上将镜像源更换到国 ...
- 使用Json-lib将对象和Json互转
工程下载地址: https://files.cnblogs.com/files/xiandedanteng/jsonSample20200308.rar Depenency: <!-- 使用js ...
- Oracle SQL Developer中查看解释计划Explain Plan的两种方法
方法一: 比如要查看解释计划的SQL是:select * from hy_emp 那么在输入窗口输入: EXPLAIN PLAN FOR select * from hy_emp 之后执行,输出窗口会 ...
- OneDrive Weblist
OneIndex-Serverless 教程:https://zhuanlan.zhihu.com/p/74538287 https://github.com/LiuChangFreeman/OneI ...
- pytest测试框架 -- setup和teardown等
一.用例运行级别 1.函数级别(setup.teardown 或 setup_function.teardown_function): 仅对处于同作用域的测试函数有效(该函数定义不在类中,则对非类中测 ...
- 反射之hasattr() getattr() setattr() 函数
Python的hasattr() getattr() setattr() 函数使用方法详解 hasattr(object, name)判断object中有没有一个name字符串对应的方法或属性,返回B ...