POJ 3414 BFS 输出过程
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 17456 | Accepted: 7407 | 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)
Source
#include "cstdio"
#include "stdlib.h"
#include "iostream"
#include "algorithm"
#include "string"
#include "cstring"
#include "queue"
#include "cmath"
#include "vector"
#include "map"
#include "set"
#define mj
#define db double
#define ll long long
using namespace std;
const int N=1e2+;
int cas=;
int a,b,c;
int ok=;
int v[N][N];
char s[][]={"","FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};
struct P
{
int a,b,t;
char s[];//记录操作
}; void bfs()
{
queue<P>q;
P p,u;
p.a=,p.b=,p.t=,p.s[]='';
v[p.a][p.b]=;
q.push(p);
while(!q.empty()){
u=q.front();
q.pop();
if(u.a==c || u.b==c){
ok=;
printf("%d\n",u.t);
for(int i=;i<u.t;i++){
printf("%s\n",s[u.s[i]-'']);//输出操作
}
return ;
}
if(u.a<a){
p.a=a;
p.b=u.b;
p.t=u.t+;
strcpy(p.s,u.s);//复制之前的操作
p.s[u.t]=+'';
if(!v[p.a][p.b]){
v[p.a][p.b]=;
q.push(p);
}
}
if(u.b<b){
p.b=b;
p.a=u.a;
p.t=u.t+;
strcpy(p.s,u.s);
p.s[u.t]=+'';
if(!v[p.a][p.b]){
v[p.a][p.b]=;
q.push(p);
}
}
if(u.a!=){
p.a=;
p.b=u.b;
p.t=u.t+;
strcpy(p.s,u.s);
p.s[u.t]=+'';
if(!v[p.a][p.b]){
v[p.a][p.b]=;
q.push(p);
}
}
if(u.b!=){
p.b=;
p.a=u.a;
p.t=u.t+;
strcpy(p.s,u.s);
p.s[u.t]=+'';
if(!v[p.a][p.b]){
v[p.a][p.b]=;
q.push(p);
}
}
if(u.a!= && u.b<b){
if(b-u.b>=u.a){
p.b=u.a+u.b;
p.a=;
}
else{
p.a=u.a+u.b-b;
p.b=b;
}
p.t=u.t+;
strcpy(p.s,u.s);
p.s[u.t]=+'';
if(!v[p.a][p.b]){
v[p.a][p.b]=;
q.push(p);
}
}
if(u.b!= && u.a<a){
if(a-u.a>=u.b){
p.a=u.a+u.b;
p.b=;
}
else{
p.b=u.a+u.b-a;
p.a=a;
}
p.t=u.t+;
strcpy(p.s,u.s);
p.s[u.t]=+'';
if(!v[p.a][p.b]){
v[p.a][p.b]=;
q.push(p);
}
}
}
}
int main()
{
scanf("%d %d %d",&a,&b,&c);
memset(v,, sizeof(v));
bfs();
if(!ok) printf("impossible\n");
return ;
}
POJ 3414 BFS 输出过程的更多相关文章
- POJ - 3414 bfs [kuangbin带你飞]专题一
		状态搜索,每种状态下面共有六种选择,将搜索到的状态保存即可. d[i][j]表示状态A杯中水i升,B杯中水j升,总状态数量不会超过A杯的容量 * B杯的容量. AC代码 #include<cst ... 
- 【BFS】POJ 3414
		直达 -> POJ 3414 Pots 相似题联动–>HDU 1495 非常可乐 题意:两个壶倒水,三种操作,两个桶其中一个满足等于C的最少操作,输出路径.注意a,b互倒的时候能不能倒满, ... 
- poj 3414 Pots 【BFS+记录路径 】
		//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ... 
- BFS POJ 3414 Pots
		题目传送门 /* BFS:六种情况讨论一下,BFS轻松解决 起初我看有人用DFS,我写了一遍,TLE..还是用BFS,结果特判时出错,逗了好长时间 看别人的代码简直是受罪,还好自己终于发现自己代码的小 ... 
- Pots(POJ - 3414)【BFS 寻找最短路+路径输出】
		Pots(POJ - 3414) 题目链接 算法 BFS 1.这道题问的是给你两个体积分别为A和B的容器,你对它们有三种操作,一种是装满其中一个瓶子,另一种是把其中一个瓶子的水都倒掉,还有一种就是把其 ... 
- 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 ... 
- POJ 3414 Pots(罐子)
		POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ... 
- Pots POJ 3414
		/* *POJ 3414 *简单模板bfs *编程应该为了方便理解,尽量提供接口 */ #include<cstdio> #include<algorithm> #includ ... 
随机推荐
- Spring Junit测试(非web,即不包含Controller测试)
			使用Spring-Test对Spring框架进行单元测试 配置过程: lib加入导入spring-test.jar和junit包 或者使用Maven依赖: <dependency> < ... 
- 初学struts2-入门案列
			1.所需类库 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactI ... 
- springboot mybatis 自动生成代码(maven+IntelliJ IDEA)
			1.在pom文件中加入需要的依赖(mybatis-generator-core) 和 插件(mybatis-generator-maven-plugin) <dependency> < ... 
- IO模型与soketserver实现并发
			一 IO模型介绍 为了更好地了解IO模型,我们需要事先回顾下:同步.异步.阻塞.非阻塞 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非 ... 
- Hibernate笔记7--JPA CRUD
			1.环境搭建,注意包结构的问题,src下建立名为META-INF的文件夹,放persistence.xml,位置放错,读不到会报错. <?xml version="1.0" ... 
- SpringEL和资源调用
			Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似于JSP的EL表达式语言. Spring开发中经常涉及调用各种资源的情况,包含普通文件.网址.配置文件.系统环境变量等, ... 
- thinkphp分页+条件查询
			最近项目上面有一个带条件查询的分页列表,一开始form用的post,点击第二页就没有跳转成功,原因是分页是get请求,post数据链接到其他页面就会被清除. 解决办法: 1.form表单method= ... 
- 微信小程序(底部导航的实现)
			详情请看官方文档介绍: https://mp.weixin.qq.com/debug/wxadoc/dev/framework/config.html 在根目录配置文件app.json中配置底部导航: ... 
- 【CF1000C】Covered Points Count(离散化+差分)
			点此看题面 大致题意: 给出\(n\)条线段,分别求有多少点被覆盖\(1\)次.\(2\)次...\(n\)次. 正常的算法 好吧,这道题目确实有个很简单的贪心做法(只可惜我做的时候没有想到,结果想了 ... 
- 如何在python中读写和存储matlab的数据文件(*.mat)
			使用sicpy.io即可.sicpy.io提供了两个函数loadmat和savemat,非常方便. 以前也有一些开源的库(pymat和pymat2等)来做这个事, 不过自从有了numpy和scipy以 ... 
