M - Pots
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)
不怎么会bfs所以写的比较差,其实就是广搜,一点点找这个状态可以做哪些操作,然后慢慢搜,可以做的就存起来,然后一直下去,记得存走过的位置
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
//#define pb push_back
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
//#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
typedef long double ld;
typedef double db;
const ll mod=1e9+100;
const db e=exp(1);
using namespace std;
const double pi=acos(-1.0);
string op[6]={"FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(1,2)","POUR(2,1)"};//存放操作,便于输出
//操作中,0是1加水,1是2加水 ,2是1倒水,3是2倒水,4是1倒给2,5是2倒给1
int A,B,C;
int visit[110][110];//用来存走过的状态,免得又走回去
struct water
{
int x,y,bits;
int a[1000];//记录经过的操作
water(){}
};
water ans;//放答案
queue<water>v;//做一个队列
void bfs(water q)
{
water t,tt;
while(!v.empty())
{
t=v.front();
v.pop();
if(t.x ==C||t.y ==C)//走到要求的了可以跳出了
{
ans=t;
return ;
}
if(visit[t.x][t.y])
continue;
else
visit[t.x][t.y]=1;
rep(i,0,6)
{
if(i==0)
{
if(t.x!=A)//判断可以做这个操作吗
{
tt=t;
tt.x=A;
tt.a[t.bits]=0;
tt.bits ++;
v.push(tt);
}
}
else if(i==1)
{
if(t.y!=B)//判断可以做这个操作吗
{
tt=t;
tt.y=B;
tt.a[t.bits]=1;
tt.bits++;
v.push(tt);
}
}
else if(i==2)
{
if(t.x)//判断可以做这个操作吗
{
tt=t;
tt.x=0;
tt.a[t.bits]=2;
tt.bits++;
v.push(tt);
}
}
else if(i==3)
{
if(t.y)//判断可以做这个操作吗
{
tt=t;
tt.y=0;
tt.a[t.bits]=3;
tt.bits++;
v.push(tt);
}
}
else if(i==4)
{
if(t.x)//判断可以做这个操作吗
{
tt=t;
if(t.x+t.y<=B)//判断会有多余的水吗
{
tt.x=0;
tt.y=t.x+t.y;
}else
{
tt.y=B;
tt.x=t.x+t.y-B;
}
tt.a[t.bits]=4;
tt.bits++;
v.push(tt);
}
}
else if(i==5)
{
if(t.y)//判断可以做这个操作吗
{
tt=t;
if(t.x+t.y<=A)//判断会有多余的水吗
{
tt.y=0;
tt.x=t.x+t.y;
}else
{
tt.x=A;
tt.y=t.x+t.y-A;
}
tt.a[t.bits]=5;
tt.bits++;
v.push(tt);
}
}
}
}
}
int main()
{
cin>>A>>B>>C;
water q;//
q.x=0;
q.y=0;
q.bits=0;
mm(visit,0);
mm(q.a,0);
v.push(q); //这些是初始化操作
bfs(q);
if(ans.bits) //最后如果有操作次数就输出,没输出impossible。。之前交了一发发现没加上这个
{
cout<<ans.bits<<endl;
rep(i,0,ans.bits)//输出操作
cout<<op[ans.a[i]]<<endl;
}
else
cout<<"impossible";
return 0;
}
M - Pots的更多相关文章
- 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 ...
- Pots 分类: 搜索 POJ 2015-08-09 18:38 3人阅读 评论(0) 收藏
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11885 Accepted: 5025 Special Judge D ...
- Pots of gold game:看谁拿的钱多
问题描述: Pots of gold game: Two players A & B. There are pots of gold arranged in a line, each cont ...
- (poj)3414 Pots (输出路径的广搜)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- POJ 3414 Pots【bfs模拟倒水问题】
链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...
- Pots(bfs)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8266 Accepted: 3507 Special Judge D ...
- POJ 3414 Pots 记录路径的广搜
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- poj 3414 Pots (bfs+线索)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10071 Accepted: 4237 Special J ...
- POJ 3414 Pots(BFS)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description You are g ...
随机推荐
- Go语言之高级篇beego框架之cookie与session
1.cookie的用法 this.Ctx.SetCookie("name", name, maxage, "/") this.Ctx.SetCookie(&qu ...
- Spark:java api读取hdfs目录下多个文件
需求: 由于一个大文件,在spark中加载性能比较差.于是把一个大文件拆分为多个小文件后上传到hdfs,然而在spark2.2下如何加载某个目录下多个文件呢? public class SparkJo ...
- Andorid源码 4.4 TAG
Fetching project platform/frameworks/opt/timezonepickerremote: Counting objects: 11169, doneremote: ...
- leetcode笔记:Validate Binary Search Tree
一. 题目描写叙述 Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is ...
- Memcache及telnent命令具体解释
1.启动Memcache 经常使用參数 memcached 1.4.3 -p <num> 设置port号(默认不设置为: 11211) -U <num> U ...
- js 引擎 和 html 渲染引擎
- 微软自然语言理解平台LUIS:从零开始,帮你开发智能音箱
今年微软开发者大会Build 2017上展示了一款Invoke智能音箱,受到了媒体和大众的广泛关注.近两年,不少大公司纷纷涉足该领域,使得智能音箱逐渐成为一款热门的人工智能家用电器.智能音箱的兴起也改 ...
- mysql乱码问题解决办法
最近开发一下小项目,遇到了最常见的乱码问题. 1.数据库使用utf-8 utf-8_generic_ci编码,使用csv上传并导入数据,插入数据的时候出现了问题,有很大部分数据没有被导入,所以使用m ...
- 图文剖析自己定义View的绘制(以自己定义滑动button为例)
自己定义View一直是横在Android开发人员面前的一道坎. 一.View和ViewGroup的关系 从View和ViewGroup的关系来看.ViewGroup继承View. View的子类.多是 ...
- Spring Boot 2.0 整合 FreeMarker 模板引擎
本篇博文将和大家一起使用Spring Boot 2.0 和FreeMarker 模板引擎整合实战. 1. 创建新的项目 2. 填写项目配置信息 3. 勾选web 模块 4. 勾选freemarker模 ...