搜索算法 pots
题目链接 点击打开链接
| Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
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)
#include<iostream>
#include<cstring>
#include<queue>
#include<stdio.h>
using namespace std; struct AB
{
int pre;
int a,b;
int c;
};
AB q[10000];
int head,tail;
int A,B,C;
int aa,bb;
bool visit[101][101]; void FILL(int i)
{
if(i==1)
aa=A;
else bb=B;
} void POUR(int i,int j)
{
int a=aa,b=bb;
if(i==1)
{
if(a+b>B)
{
bb=B;
aa=a+b-B;
}
else
{
bb=a+b;
aa=0;
}
}
else
{
if(a+b>A)
{
aa=A;
bb=a+b-A;
}
else
{
aa=a+b;
bb=0;
}
}
} void DROP(int i)
{
if(i==1)
aa=0;
else bb=0;
} void operate(int i)
{
switch(i)
{
case 0:
{
FILL(1);
break;
}
case 1:
{
FILL(2);
break;
}
case 2:
{
POUR(1,2);
break;
}
case 3:
{
POUR(2,1);
break;
}
case 4:
{
DROP(1);
break;
}
case 5:
{
DROP(2);
break;
}
}
} void out(int i)
{
switch(i)
{
case 0:
{
cout<<"FILL(1)"<<endl;
break;
}
case 1:
{
cout<<"FILL(2)"<<endl;
break;
}
case 2:
{
cout<<"POUR(1,2)"<<endl;
break;
}
case 3:
{
cout<<"POUR(2,1)"<<endl;
break;
}
case 4:
{
cout<<"DROP(1)"<<endl;
break;
}
case 5:
{
cout<<"DROP(2)"<<endl;
break;
}
}
} int BFS()
{
head=tail=0;
q[tail].a=0;
q[tail].b=0;
q[tail++].pre=-1;
visit[0][0]=true;
while(1)
{
if(head==tail)
{
return 0;
}
if(q[head].a==C||q[head].b==C)
{
return head;
}
for(int i=0; i<6; i++)
{
aa=q[head].a;
bb=q[head].b;
operate(i);
if(!visit[aa][bb])
{
visit[aa][bb]=true;
q[tail].pre=head;
q[tail].a=aa;
q[tail].b=bb;
q[tail++].c=i;
}
}
head++;
}
} int main()
{
while(scanf("%d %d %d",&A,&B,&C)!=EOF)
{
memset(visit,false,sizeof(visit));
int t=BFS();
int way[10000],n=0;
if(!t)
cout<<"impossible"<<endl;
else
{
while(q[t].pre!=-1)
{
way[n++]=t;
t=q[t].pre;
}
cout<<n<<endl;
while(n--)
{
out(q[way[n]].c);
}
}
}
return 0;
}
搜索算法 pots的更多相关文章
- [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)
BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...
- 【小白学游戏常用算法】二、A*启发式搜索算法
在上一篇博客中,我们一起学习了随机迷宫算法,在本篇博客中,我们将一起了解一下寻路算法中常用的A*算法. 通常情况下,迷宫寻路算法可以使用深度优先或者广度优先算法,但是由于效率的原因,不会直接使用这些算 ...
- POJ 3414 Pots
Pots Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- 【转】ACM搜索算法总结 --By GreenHand
搜索是ACM竞赛中的常见算法,本文的主要内容就是分析它的 特点,以及在实际问题中如何合理的选择搜索方法,提高效率.文章的第一部分首先分析了各种基本的搜索及其各自的特点.第二部分在基本搜索方法的基础上提 ...
- grep之字符串搜索算法Boyer-Moore由浅入深(比KMP快3-5倍)
这篇长文历时近两天终于完成了,前两天帮网站翻译一篇文章“为什么GNU grep如此之快?”,里面提及到grep速度快的一个重要原因是使用了Boyer-Moore算法作为字符串搜索算法,兴趣之下就想了解 ...
- 广搜+输出路径 POJ 3414 Pots
POJ 3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13547 Accepted: 5718 ...
- Java基础之一组有用的类——使用二叉树搜索算法搜索某个作者(TryBinarySearch)
控制台程序. Arrays类中的binarySearch()静态方法使用二叉树搜索算法,在有序数组中查找包含给定值的元素.只有当数组的元素按升序方式排序时,该方法才是最有效的,否则就应在调用binar ...
- 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 ...
随机推荐
- Redis及其Sentinel配置项详细说明
Redis及其Sentinel配置项详细说明 http://lixiaohui.iteye.com/blog/2315516
- C++学习总结1
一.内存管理 一般new 与 delete 同时出现.假如释放一个对象用 delete p即可.多个对象用delet [ ]p 即:new与delete需要搭配好. C++继承了C的许多函数,mal ...
- nginx-location rewrite
location 语法 location 有”定位”的意思, 根据Uri来进行不同的定位. 在虚拟主机的配置中,是必不可少的,location可以把网站的不同部分,定位到不同的处理方式上. 比如, 碰 ...
- 分布式数据库数据从属与client与server的数据同步
老实说,眼下市面上很多产品,的确是不成熟的产品. 用过一些,给人蛋痛的感觉. 导言 分布还是集总 今天我们来探讨一个非常重要的问题. 每一个程序猿都有其思想,我的思想之中的一个,就是分布式. 分布式, ...
- 宜信开源微服务任务调度平台(SIA-TASK)
背景 无论是互联网应用或者企业级应用,都充斥着大量的批处理任务.常常需要一些任务调度系统帮助开发者解决问题.随着微服务化架构的逐步演进,单体架构逐渐演变为分布式.微服务架构.在此的背景下,很多原先的任 ...
- JAVA RMI远程方法调用简单实例(转载)
来源:http://www.cnblogs.com/leslies2/archive/2011/05/20/2051844.html RMI的概念 RMI(Remote Method Invocati ...
- 【转】Android IDA 动态调试最完善攻略,跨过各种坑
前提条件和运行环境一定要写清楚,不然会有很多坑,坑死人. (1)IDA 是最新的7.0版本 (2) JDB 使用Java安装目录下的 (3)系统是win10 使用命令窗口时有很大的差别 (4)手机是 ...
- React Native学习(二)之View
React Native组件解析(二)之View 0.JSX React的核心机制之一就是虚拟DOM:可以在内存中创建的虚拟DOM元素.React利用虚拟DOM来减少对实际DOM的操作从而提升性能. ...
- 【BZOJ1085】[SCOI2005]骑士精神 双向BFS
[BZOJ1085][SCOI2005]骑士精神 Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它 ...
- EasyDarwin开源流媒体服务器性能优化之Work-stealing优化方案
本文转自EasyDarwin开源团队成员Alex的博客:http://blog.csdn.net/cai6811376/article/details/52400226 EasyDarwin团队的Ba ...