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 ...
随机推荐
- 性能测试学习第二天_性能测试工具概述Loadrunner介绍
性能测试工具概述Loadrunner介绍 http://www.51testing.com/html/42/n-6542.html 其中,T直接影响用户体验时间 性能测试的原理: 记录一个访问过程的通 ...
- lambda匿名函数,sorted排序,filter()筛选,map()映射
一丶匿名函数 语法: 函数名 = lambda参数:返回值 # 普通的正常的函数 def func(n): return n * n ret = func(9) print(ret) # 匿名函数 a ...
- maven 配置Hibernate
1.首先在maven中添加jar包依赖 <dependency> <groupId>org.hibernate</groupId> <artifactId&g ...
- redis在Windows下以后台服务一键搭建集群(单机--伪集群)
redis在Windows下以后台服务一键搭建集群(单机--伪集群) 一.概述 此教程介绍如何在windows系统中同一台机器上布置redis伪集群,同时要以后台服务的模式运行.布置以脚本的形式,一键 ...
- pyinstaller打包python源程序访问hive
1.需求 使用hvie server一段时间后,业务部门需要自己不定时的查询业务数据,之前这一块都是他们提需求我们来做,后来发现这样重复一样的工作放在我们这边做是在没有效率,遂提出给他们工具或者web ...
- git版本管理工具 标签(Tag) / 版本回退 / 分支的简单使用
a.标签 标签,可以使用这个功能来标记发布结点. 举个例子, 假如我们的项目版本目前是1.2版本, 上级要求这个版本要在半个月后再进行上传至Appstore, 并要求我们未来的半个月内,去写1.3版本 ...
- Python实现购物小程序
一.需求 1.登录 { ‘xxx1’:{'passwd':'123','role':1,'moeny':10000,"carts":['mac']}, 'xxx1':{'passw ...
- datetime 插件
1 写一段文本 <div id="nomarl-wrap"> <div class="form-group"> <label c ...
- PAT (Advanced Level) Practise - 1094. The Largest Generation (25)
http://www.patest.cn/contests/pat-a-practise/1094 A family hierarchy is usually presented by a pedig ...
- mahout算法解析
1.下载ubuntu的iso 2.安装虚拟机,vmware,最好英文原版 3.在vm里面安装ubuntu,安装vm tools 4.本地连接设置为共享上网,虚拟机采用NAT,打开ubuntu,自动连接 ...