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 ...
随机推荐
- PHP setcookie()用法
定义和用法 setcookie() 函数向客户端发送一个 HTTP cookie. cookie 是由服务器发送到浏览器的变量.cookie 通常是服务器嵌入到用户计算机中的小文本文件.每当计算机通过 ...
- The program 'unzip' is currently not installed. You can install it by typing:
linux解压遇到下面问题: The program 'unzip' is currently not installed. You can install it by typing: sudo ap ...
- base、self标签
以新的窗口打开页面 self在自己的窗口打开
- h5 本地存储
H5本地存储有两个API,一个是Web Storage,还有一个是Web SQL.不管是哪一个,都是基于JavaScript语言来使用,接下来我就教你怎么使用H5本地存储,本文篇幅较大,JS代码较多, ...
- 构建第一个Spring Boot2.0应用之集成mybatis、Druid(七)
一.环境: IDE:IntelliJ IDEA 2017.1.1 JDK:1.8.0_161 Maven:3.3.9 springboot:2.0.2.RELEASE 二.说明: 本文综合之 ...
- Oracle数据库基础--SQL查询经典例题
Oracle基础练习题,采用Oracle数据库自带的表,适合初学者,其中包括了一些简单的查询,已经具有Oracle自身特点的单行函数的应用 本文使用的实例表结构与表的数据如下: emp员工表结构如下: ...
- LeetCode Implement strStr() 实现strstr()
如题 思路:暴力就行了.1ms的暴力!!!别的牛人写出来的,我学而抄之~ int strStr(char* haystack, char* needle) { ; ; ; ++i) { ; ; ++j ...
- linux 命令——9 touch (转)
linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. 1.命令格式: touch [选项]... 文件... 2.命令参数: -a ...
- hangfire使用
1 . NuGet 命令行执行 Install-Package Hangfire2.首先在ConfigureServices 方法中注册服务: services.AddHangfire(r=>r ...
- hdu1150&&POJ1325 Machine Schedule---最小点覆盖
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1150 题目大意: 给你两台机器A和B,A机器有n种模式,B机器有m种模式,初始时都是0,现在给你k个 ...