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 ...
随机推荐
- Java 编码规范有感
应小组要求,开发测试都需要考阿里编码规范,因此,相当于是突击了一下关于编码规范方面的知识,目前做的项目后期需要进行项目迁移,数据迁移,功能迁移... 各种迁移... 阿里巴巴编码规范(Java)考试地 ...
- JAVA中面向对象
一.方法: 1.方法概述: 在JAVA中,方法就是用来完成解决某件事情或实现某个功能的办法. 2.方法的语法格式: 修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2,.....){ ...
- hibernate课程 初探单表映射2-3 session简介
hibernate流程: 1 配置对象Configurateion 读取 hibernate.cfg.xml 2 会话工厂SessionFactory 读取 user.hbm.xml(创建销毁相当耗费 ...
- weexpack 创建项目, 打包Android 和 ios
1: 首先确保 node.js版本大于6.0, 不然使用 weexpack 时会报错(部分使用es6语言,node版本太低不支持es6) 下面所有都是命令行执行的命令 2: 全局安装 weexpack ...
- Cloneable接口的作用
Cloneable接口是一个[标记接口],就是没有任何内容 implements Cloneable表示该对象能被克隆,能使用Object.clone()方法.如果没有implements Clone ...
- python3基础09(装饰器的使用)
#!/usr/bin/env python# -*- coding:utf-8 -*- import time #方式1class People: def __init__(self, name, a ...
- 使用NIO快速复制Java文件
package com.test.test; import java.io.File;import java.io.FileInputStream;import java.io.FileOutputS ...
- Excel2Dataset
//获取用户打开的Excel文档路径 private stringkkk() { OpenFileDialog selectFile = new OpenFileDialog(); selectFil ...
- byte[] 中需要除去的特定 byte
/// <summary> /// 去掉byte[]中特定的byte /// </summary> /// <param name="SourceByteArr ...
- 碰到一个微软的bug:CWinAppEx::GetString
在调试公司项目代码的时候,有一个系统设置的功能,里面需要从注册表中去读取数据,因为使用了MFC框架所以,为了简化代码直接使用了CWinAppEx::GetString .CWinAppEx::SetS ...