搜索算法 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 ...
随机推荐
- andriod打开摄像头和打开相册
package com.example.yanlei.picture; import android.support.v7.app.AppCompatActivity; import android. ...
- 八卦某 G 的前端开发方式及流程--百度FEX前端nwind信息搜集神技能
他山之石,可以攻玉. 话说本人从毕业到现在一直在某 B 公司工作,前些年折腾过不少开发方式和工具,但总觉得或许有更好的方案,所以很好奇其它公司内部是如何工作的,我曾经浏览过某 Y 公司内部无所不包的 ...
- 自己定义ImageView,实现点击之后算出点击的是身体的哪个部位
近期也是由于项目的原因,所以有机会接触到这边的算法. 此文重点不是怎样实现的思路和原理, 有须要的同事能够借鉴一下 废话不多说,直接上代码: <span style="font-siz ...
- Oracle-31-对视图DML操作
一.对视图进行DML操作 1.创建一个视图v_person create or replace noforceview v_person as select *from person where id ...
- mysql:“Access denied for user 'root@IP地址'"
请仔细.再仔细确认你的用户名.密码.IP是否有误! 可悲的我老犯这种低级错误,以为用户没权限访问,唉..
- VS + Qt5Designer + Anaconda环境配置
最近打算做一个模型训练工具,从来都不喜欢做UI的我,最终把目光放在了QtDesigner上.配环境的过程中在网上翻阅了不少博客,但大多是pycharm或者是VScode,使用VS的似乎不多.所以打算记 ...
- linux关机命令详解(转载)
在linux下一些常用的关机/重启命令有shutdown.halt.reboot.及init,它们都可以达到重启系统的目的,但每个命令的内部工作过程是不同的. Linux centos重启命令: 1. ...
- SICP 习题 (1.38)解题总结
SICP 习题1.38 紧跟着习题1.37的方向,要求我们用习题1.37中定义的cont-frac过程计算数学家欧拉大师在论文De Fractionibus Continuis 中提到的e-2的连分式 ...
- VS重置命令:devenv.exe/resetuserdata
VS命令行下执行下面的命令: devenv.exe/resetuserdata
- PHP中的面向对象 中的类(class)
2.11 上午讲的是面向对象中的类(class),一个非常抽象的概念, 类里面成员的定义有 public$abc; private$abc(私有变量): protect $abc(受保护的变量): 下 ...