Pots POJ - 3414【状态转移bfs+回溯】
典型的倒水问题:
即把两个水杯的每种状态视为bfs图中的点,如果两种状态可以转化,即可认为二者之间可以连一条边。
有3种倒水的方法,对应2个杯子,共有6种可能的状态转移方式。即相当于图中想走的方法有6种,依次枚举即可。
用一个二维数组标记状态,以免重复。
难点在于输出路径,即bfs回溯。
我的处理方法是,在bfs的队列基础上,用一个vector保存每一个可能的状态,即每个状态对应一个编号。同时结构体中不仅保存每个状态的信息,而且还要保存每个状态的对应编号和上一个状态的对应编号。
那么,当bfs到达终点后,就可以从后向前进行回溯,找到路径,然后反向输出即可。
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
int Va,Vb,C;
struct pot
{
int a,b,step,t,my,last;
};
bool vis[][];
int a[];
queue<pot>que;
vector<pot>pre;
int bfs()
{
while(!que.empty())
que.pop();
pot s=pot{,,-,,,-};
que.push(s);
vis[][]=;
pre.push_back(s);
while(!que.empty())
{
pot now=que.front();
que.pop();
for(int i=;i<;i++)
{
pot tmp=now;
if(i==)//fill(1)
tmp.a=Va;
else if(i==)//fill(2)
tmp.b=Vb;
else if(i==)//drop(1)
tmp.a=;
else if(i==)//drop(2)
tmp.b=;
else if(i==)//pour(1,2)
{
if(tmp.a>Vb-tmp.b)
{
tmp.a-=(Vb-tmp.b);
tmp.b=Vb;
}
else
{
tmp.b+=tmp.a;
tmp.a=;
}
}
else if(i==)//pour(2,1)
{
if(tmp.b>Va-tmp.a)
{
tmp.b-=(Va-tmp.a);
tmp.a=Va;
}
else
{
tmp.a+=tmp.b;
tmp.b=;
}
}
if(!vis[tmp.a][tmp.b])
{
tmp.step=i;
tmp.t=now.t+;
tmp.last=now.my;
pre.push_back(tmp);
tmp.my=pre.size()-;
vis[tmp.a][tmp.b]=;
que.push(tmp);
if(tmp.a==C||tmp.b==C)
return tmp.t;
}
}
}
return -;
}
void print(int u)
{
if(u==)
printf("FILL(1)\n");
else if(u==)
printf("FILL(2)\n");
else if(u==)
printf("DROP(1)\n");
else if(u==)
printf("DROP(2)\n");
else if(u==)
printf("POUR(1,2)\n");
else if(u==)
printf("POUR(2,1)\n");
}
int main()
{
while(scanf("%d%d%d",&Va,&Vb,&C)!=EOF)
{
memset(vis,,sizeof(vis));
pre.clear();
int ans=bfs();
if(ans==-)
printf("impossible\n");
else
{
printf("%d\n",ans);
memset(a,,sizeof(a));
int p=pre.size()-;
int cnt=;
while(pre[p].last!=-)
{
a[++cnt]=pre[p].step;
p=pre[p].last;
}
for(int i=cnt;i>=;i--)
print(a[i]);
}
}
return ;
}
Pots POJ - 3414【状态转移bfs+回溯】的更多相关文章
- Pots(POJ - 3414)【BFS 寻找最短路+路径输出】
Pots(POJ - 3414) 题目链接 算法 BFS 1.这道题问的是给你两个体积分别为A和B的容器,你对它们有三种操作,一种是装满其中一个瓶子,另一种是把其中一个瓶子的水都倒掉,还有一种就是把其 ...
- Pots POJ 3414
/* *POJ 3414 *简单模板bfs *编程应该为了方便理解,尽量提供接口 */ #include<cstdio> #include<algorithm> #includ ...
- poj 1324 状态压缩+bfs
http://poj.org/problem?id=1324 Holedox Moving Time Limit: 5000MS Memory Limit: 65536K Total Submis ...
- Pots POJ - 3414 (搜索+记录路径)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22688 Accepted: 9626 Special J ...
- 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了.然后第一次 ...
- BFS POJ 3414 Pots
题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ...
- POJ 3414 Pots
Pots Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- 广搜+输出路径 POJ 3414 Pots
POJ 3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13547 Accepted: 5718 ...
随机推荐
- 鸭子类型 - Duck Typing
还是先看定义 duck typing, 鸭子类型是多态(polymorphism)的一种形式.在这种形式中,不管对象属于哪个, 也不管声明的具体接口是什么,只要对象实现了相应的方法,函数就可以在对象上 ...
- HttpClient学习整理(一)
Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客户端发送Http请求变得容易,而且 ...
- phpmyadmin配置文件详解
PHPMyadmin配置文件config.inc.php或config.default.php内容及作用解析大致如下: /** * phpMyAdmin Configuration File * * ...
- Admin后台权限管理、三大认证
目录 APIView的请求生命周期 三大认证规则 权限六表 自定义User表 详细配置演示 models.py setting.py admin.py 使用过程: 控制填写信息的字段 控制添加权限 控 ...
- Element-UI ( Dropdow )下拉菜单组件command传输对象
通过 :command绑定对象数据,handleCommand方法处理数据 template <div v-for="(item, index) in FlyWarningList&q ...
- 工作五年的.neter的一些经历感想和对未来的一些疑惑
本次疫情在家办公快一个月了,节省了上下班的时间,外出活动时间,感觉有好多时间可以利用.人一闲下来就容易想事情,很多事情想不通心里堵的厉害,做事都提不起兴趣.至于想些什么呢,我给大家摆一下. 我的经历 ...
- The finally block does not always execute in try finally
A finally block does not always xecute. The code in the try block could go into an infinite loop, th ...
- 在Docker中运行SpringBoot程序
1.将SpringBoot项目中pom.xml的build插件更换为: <build> <plugins> <plugin> <groupId>org. ...
- Spring Boot自动配置如何工作
通过使用Mongo和MySQL DB实现的示例,深入了解Spring Boot的@Conditional注释世界. 在我以前的文章“为什么选择Spring Boot?”中,我们讨论了如何创建Sprin ...
- centos7下查看mysql配置文件适用顺序
mysql --help|grep 'my.cnf' [root@izm5e2q95pbpe1hh0kkwoiz ~]# mysql --help|grep 'my.cnf' order of pre ...