快速切题 poj3414 Pots
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 10042 | Accepted: 4221 | 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) 应用时:10min
实际用时:57min
原因:六种操作都是不可逆转的.
思路:时间非常充裕到不需要建反边,数据太小
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=101;
int n,m;
typedef unsigned long long ull;
int A,B,C;
int vis[maxn][maxn];
int ans[maxn][maxn][maxn*maxn];
struct node{
int a,b;
node (int ta,int tb):a(ta),b(tb){}
};
void printop(int op){
switch(op){
case 0:
puts("FILL(1)");
break;
case 1:
puts("FILL(2)");
break;
case 2:
puts("POUR(1,2)");
break;
case 3:
puts("POUR(2,1)");
break;
case 4:
puts("DROP(1)");
break;
case 5:
puts("DROP(2)");
break;
}
}
void op(int &a,int &b,int op){
switch(op){
case 0:
a=A;
break;
case 1:
b=B;
break;
case 2:
if(b+a<=B){
b+=a;
a=0;
}
else {
a-=B-b;
b=B;
}
break;
case 3:
if(b+a<=A){
a+=b;
b=0;
}
else {
b-=A-a;
a=A;
}
break;
case 4:
a=0;
break;
case 5:
b=0;
break;
}
}
void bfs(){
queue <node> que;
que.push(node(0,0));
vis[0][0]=0;
while(!que.empty()){
node tp=que.front();que.pop();
int ta=tp.a;
int tb=tp.b;
if(tp.a==C||tp.b==C){
printf("%d\n",vis[tp.a][tp.b]);
for(int i=0;i<vis[ta][tb];i++){
int op=ans[ta][tb][i];
printop(op);
}
return ;
}
for(int i=0;i<6;i++){
int ta=tp.a;
int tb=tp.b;
op(ta,tb,i);
if(vis[ta][tb]==-1){
vis[ta][tb]=vis[tp.a][tp.b]+1;
for(int j=0;j<vis[tp.a][tp.b];j++){
ans[ta][tb][j]=ans[tp.a][tp.b][j];
}
ans[ta][tb][vis[tp.a][tp.b]]=i;
que.push(node(ta,tb));
}
}
}
puts("impossible");
}
int main(){
scanf("%d%d%d",&A,&B,&C);
memset(vis,-1,sizeof(vis));
bfs();
return 0;
}
快速切题 poj3414 Pots的更多相关文章
- POJ3414—Pots(bfs加回溯)
http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memor ...
- POJ-3414 Pots (BFS)
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- poj3414 Pots (BFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12198 Accepted: 5147 Special J ...
- POJ3414 Pots —— BFS + 模拟
题目链接:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ3414 Pots BFS搜素
题意:通过题目给出的三种操作,让任意一个杯子中的水到达一定量 分析:两个杯子最大容量是100,所以开个100*100的数组记录状态,最多1w个状态,所以复杂度很低,然后记录一下路径就好 注:代码写残了 ...
- POJ-3414.Pots.(BFS + 路径打印)
这道题做了很长时间,一开始上课的时候手写代码,所以想到了很多细节,但是创客手打代码的时候由于疏忽又未将pair赋初值,导致一直输出错误,以后自己写代码可以专心一点,可能会在宿舍图书馆或者Myhome, ...
- 快速切题sgu127. Telephone directory
127. Telephone directory time limit per test: 0.25 sec. memory limit per test: 4096 KB CIA has decid ...
- 快速切题sgu126. Boxes
126. Boxes time limit per test: 0.25 sec. memory limit per test: 4096 KB There are two boxes. There ...
- 快速切题 sgu123. The sum
123. The sum time limit per test: 0.25 sec. memory limit per test: 4096 KB The Fibonacci sequence of ...
随机推荐
- 关于STM32外接4—16MHz晶振主频处理方法
由于STM32F10x库官方采用的是默认的外接8MHz晶振,因此造成很多用户也采用了8MHz的晶振,但是,8MHz的晶振不是必须的,其他频点的晶振也是可行的,只需要在库中做相应的修改就行. 在论 ...
- 什么是BFC?
转载自知乎:https://zhuanlan.zhihu.com/p/25321647 一.常见定位方案 在讲 BFC 之前,我们先来了解一下常见的定位方案,定位方案是控制元素的布局,有三种常见方案: ...
- 打印图形|2014年蓝桥杯B组题解析第五题-fishers
打印图形 小明在X星球的城堡中发现了如下图形和文字: rank=3 rank=5 rank = 6 小明开动脑筋,编写了如下的程序,实现该图形的打印. 答案:f(a, rank-1, row, col ...
- Python 逗号的几种作用
转自http://blog.csdn.net/liuzx32/article/details/7831247 最近研究Python 遇到个逗号的问题 一直没弄明白 今天总算搞清楚了 1.逗号在参数传 ...
- LLDP协议、STP协议 笔记
参考: 数据链路层学习之LLDP 生成树协议 LLDP协议.STP协议 笔记 LLDP 提出背景: 随着网络技术的发展,接入网络的设备的种类越来越多,配置越来越复杂,来自不同设备厂商的设备也往往会增加 ...
- UVa 10163 仓库守卫
https://vjudge.net/problem/UVA-10163 题意: 有n个仓库,m个管理员,每个管理员有一个能力值P(接下来的一行有m个数,表示每个管理员的能力值) 每个仓库只能由一个管 ...
- sublime text3 (Mac) 快捷键
符号说明 符号 说明 ⌘ command ⌃ control ⌥ option ⇧ shift ↩ enter ⌫ delete 打开/关闭/前往 快捷键 功能 ⌘⇧N 打开一个新的sublime窗口 ...
- DB中字段为null,为空,为空字符串,为空格要怎么过滤取出有效值
比如要求取出微信绑定的,没有解绑的 未绑定,指定字段为null 绑定的,指定字段为某个字符串 解绑的,有的客户用的是更新指定字段为1,有的客户更新指定字段为‘1’ 脏数据的存在,比如该字段为空字符 ...
- java static关键字使用
一.使用static声明属性 1.使用static声明属性,则该属性属于全局变量(有些地方也称静态属性). 2.类的属性调用格式: 类名称.static 属性Preson.country=B城; 二. ...
- UltraDropDown
private void FruitInit() { //Create some fruit fruits.Add(-1,"apple"); fruits.Add(-2," ...