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 ...
随机推荐
- IOS Using UIAlertView to show alerts
UIAlertView in other words, it's a dialog box. You want to show a message or ask user to confirm an ...
- volitile关键字
1.volatile关键字的两层语义 一旦一个共享变量(类的成员变量.类的静态成员变量)被volatile修饰之后,那么就具备了两层语义: 1)保证了不同线程对这个变量进行操作时的可见性,即一个线程修 ...
- PL/SQL学习笔记之异常
一:异常 程序执行过程中出现错误情况被称为异常,主要有两种类型的异常: 系统定义的异常 用户定义的异常 二:系统定义的异常 Exception Oracle Error SQLCODE 描述 ACCE ...
- 链接选项-rpath的一个问题记录
问题简述 大概是这么一个情况,有一个过去已经写好的程序,这个程序用于处理网络通信,接收一些操作指令.具体的指令操作通过运行时加载动态库的形式进行扩展.(类似于net-snmp二次开发的一种形式) 问题 ...
- 计算N个点和M个点之间的距离
KNN中,训练样本有train_count个,测试样本有test_count个,每个样本有attr_count个属性.现在需要快速计算test_count个测试样本和train_count个样本之间的 ...
- Effective Java 第三版——46. 优先考虑流中无副作用的函数
Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...
- C++ 重载运算符和重载函数
C++ 重载运算符和重载函数 C++ 允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算符重载. 重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是 ...
- GC调优在Spark应用中的实践[转]
作者:仲浩 出处:<程序员>电子刊5月B 摘要:Spark立足内存计算,常常需要在内存中存放大量数据,因此也更依赖JVM的垃圾回收机制.与此同时,它也兼容批处理和流式处理,对于程序 ...
- FAL_CLIENT和FAL_SERVER参数详解
FAL_CLIENT和FAL_SERVER参数详解 转载:http://openwares.net/database/fal_client_fal_server.html FAL_CLIENT和FAL ...
- HDOJ 1393 Weird Clock(明确题意就简单了)
Problem Description A weird clock marked from 0 to 59 has only a minute hand. It won't move until a ...