POJ-3414 Pots (BFS)
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) 题目分析:打眼一看就是BFS,还是普通的BFS。 代码如下:
# include<iostream>
# include<cstdio>
# include<string>
# include<queue>
# include<vector>
# include<cstring>
# include<algorithm>
using namespace std;
struct node
{
int a,b,t;
vector<string>op;
bool operator < (const node &a) const {
return t>a.t;
}
node & operator = (const node &p) {
a=p.a,b=p.b,t=p.t;
op.clear();
for(int i=;i<p.op.size();++i)
op.push_back(p.op[i]);
return *this;
}
};
int vis[][];
void bfs(int A,int B,int C)
{
priority_queue<node>q;
memset(vis,,sizeof(vis));
node sta;
sta.a=sta.b=sta.t=;
sta.op.clear();
vis[][]=;
q.push(sta);
while(!q.empty())
{
node u=q.top();
q.pop();
if(u.a==C||u.b==C){
printf("%d\n",u.t);
for(int i=;i<u.op.size();++i)
cout<<u.op[i]<<endl;
return ;
}
if(u.a<A){
node now=u;
now.a=A,now.b=u.b;
if(!vis[now.a][now.b]){
vis[now.a][now.b];
now.t=u.t+;
now.op.push_back("FILL(1)");
q.push(now);
}
}
if(u.b<B){
node now=u;
now.a=u.a,now.b=B;
if(!vis[now.a][now.b]){
vis[now.a][now.b];
now.t=u.t+;
now.op.push_back("FILL(2)");
q.push(now);
}
}
if(u.a>){
node now=u;
now.a=,now.b=u.b;
if(!vis[now.a][now.b]){
vis[now.a][now.b];
now.t=u.t+;
now.op.push_back("DROP(1)");
q.push(now);
}
}
if(u.b>){
node now=u;
now.a=u.a,now.b=;
if(!vis[now.a][now.b]){
vis[now.a][now.b];
now.t=u.t+;
now.op.push_back("DROP(2)");
q.push(now);
}
}
if(u.a<A&&u.b>){
node now=u;
now.a=min(A,u.a+u.b);
now.b=max(,u.b-A+u.a);
if(!vis[now.a][now.b]){
vis[now.a][now.b]=vis[now.b][now.a]=;
now.t=u.t+;
now.op.push_back("POUR(2,1)");
q.push(now);
}
}
if(u.a>&&u.b<B){
node now=u;
now.a=max(,u.a-B+u.b);
now.b=min(B,u.b+u.a);
if(!vis[now.a][now.b]){
vis[now.a][now.b]=vis[now.b][now.a]=;
now.t=u.t+;
now.op.push_back("POUR(1,2)");
q.push(now);
}
}
}
printf("impossible\n");
}
int main()
{
int A,B,C;
scanf("%d%d%d",&A,&B,&C);
bfs(A,B,C);
return ;
}
POJ-3414 Pots (BFS)的更多相关文章
- poj 3414 Pots ( bfs )
题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i) fill the ...
- POJ 3414 Pots(罐子)
POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ...
- poj 3414 Pots (bfs+线索)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10071 Accepted: 4237 Special J ...
- POJ 3414 Pots(BFS+回溯)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11705 Accepted: 4956 Special J ...
- poj 3414 Pots 【BFS+记录路径 】
//yy:昨天看着这题突然有点懵,不知道怎么记录路径,然后交给房教了,,,然后默默去写另一个bfs,想清楚思路后花了半小时写了120+行的代码然后出现奇葩的CE,看完FAQ改了之后又WA了.然后第一次 ...
- poj 3414 Pots【bfs+回溯路径 正向输出】
题目地址:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- poj 3414 Pots(广搜BFS+路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:id=3414">http://poj.org/probl ...
- 【POJ - 3414】Pots(bfs)
Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i) 将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...
- POJ 3414 Pots (dfs,这个代码好长啊QAQ)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- POJ 3414 pots (未解决)
http://poj.org/problem?id=3414 #include <iostream> #include <cstdio> #include <queue& ...
随机推荐
- Centos下安装git高版本2.1.2
安装依赖软件 # yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel asciidoc # yum in ...
- Linux内核分析--内核中的数据结构双向链表续【转】
在解释完内核中的链表基本知识以后,下面解释链表的重要接口操作: 1. 声明和初始化 实际上Linux只定义了链表节点,并没有专门定义链表头,那么一个链表结构是如何建立起来的呢?让我们来看看LIST_H ...
- 蚂蚁感冒|2014年蓝桥杯B组题解析第八题-fishers
蚂蚁感冒 长100厘米的细长直杆子上有n只蚂蚁.它们的头有的朝左,有的朝右. 每只蚂蚁都只能沿着杆子向前爬,速度是1厘米/秒. 当两只蚂蚁碰面时,它们会同时掉头往相反的方向爬行. 这些蚂蚁中,有1只蚂 ...
- 【附1】hystrix详述(1)
一.hystrix的作用 控制被依赖服务的延时和失败 防止在复杂系统中的级联失败 可以进行快速失败(不需要等待)和快速恢复(当依赖服务失效后又恢复正常,其对应的线程池会被清理干净,即剩下的都是未使用的 ...
- 【第一章】 第一个spring boot程序
环境: jdk:1.8.0_73 maven:3.3.9 spring-boot:1.2.5.RELEASE(在pom.xml中指定了) 注意:关于spring-boot的支持, 最少使用jdk7(j ...
- 论文笔记之:Continuous Deep Q-Learning with Model-based Acceleration
Continuous Deep Q-Learning with Model-based Acceleration 本文提出了连续动作空间的深度强化学习算法. 开始正文之前,首先要弄清楚两个概念:Mod ...
- 对某项目中Vuex用法的分析
上周五刚发布一个线上版本,趁着新的需求和bug还没到来,决定分析一下正在维护的一个使用Vue 2.0 开发的后台管理系统中Vuex部分代码.这部分代码不是我写的,加上我一直在“使用”现成的而不是“搭建 ...
- [Java] - MySQL数据库的时间设置问题.
之前有朋友做的项目时间格式设置为String,我感觉很不好,随后自己试了试. 首先在设置数据库类型时,选择的是timestamp, 而Java的实体中设置时间的属性类型为Date, (java.uti ...
- [Redis] - redis实战
Microsoft Windows [版本 10.0.17134.472] (c) Microsoft Corporation.保留所有权利. C:\Users\SeeClanUkyo>f: F ...
- 51nod 1444 破坏道路(最短路)
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1444 题意: 思路: 哇,思路爆炸. 因为每条边的权值都为1,所以可以直 ...