搜索算法 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 ...
随机推荐
- iOS -- SKScene类
SKScene类 继承自 SKEffectNode:SKNode:UIResponder:NSObject 符合 NSCoding(SKNode)NSCopying(SKNode)NSObject ...
- xpath的匹配规则
starts-with 匹配一个属性开始位置的关键字 contains 匹配一个属性值中包含的字符串 text() 匹配的是显示文本信息,此处也可以用来做定位用 i.e. //input[starts ...
- [Algorithms] Queue & Priority Queue
In this lesson, you will learn how to create a queue in JavaScript. A queue is a first-in, first-out ...
- 百科知识 isz文件如何打开
使用UltraISO可以打开
- angular - 配置package.json -3
package.json 包含了所有的开发包以及全局包以及其它项目信息,我们这个项目需要用到 bootstrap,所以我们添加信息. 添加包信息以后,我们用 npm install 安装,npm包管理 ...
- angular - 新建项目 - 2
ng new testNg 新建项目后,从网络上拉取模板(最后缓存下来,我们下次创建项目的时间将会减少80%) 安装过程中,需要我们提供Git账号和姓名 最后,我们进入 useNg 然后,启动服务器 ...
- java性能监控工具jps-windows
jps Lists the instrumented Java Virtual Machines (JVMs) on the target system. This command is experi ...
- vue2.0 + vux (五)api接口封装 及 首页 轮播图制作
1.安装 jquery 和 whatwg-fetch (优雅的异步请求API) npm install jquery --save npm install whatwg-fetch --save 2. ...
- (updated on Mar 1th)Programming Mobile Applications for Android Handheld Systems by Dr. Adam Porter
本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. Lab - Inte ...
- Python生成8位随机字符串的一些方法
#第一种方法 import random import string seed = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP ...