Pots(BFS)
Pots
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 31 Accepted Submission(s) : 14
Special Judge
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.
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).
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’.
题意:给出不定数量的样例,每个样例一行,有三个整数,a,b,c,a和b代表两个杯子的容量,c是希望得到的液体体积,一开始两个杯子都是空的,如果利用两个杯子可以倒出c则输出最小的步数和倒出c的步骤;
对两个杯子可以进行以下操作:
FILL(i):装满第i个杯子;
POUR(i,j):从第i个杯子向第j个杯子中倒水;
DROP(i):清空第i个杯子;
思路:
利用BFS进行搜索,求出最小部属并输出;注意对于每一个状态都可能有六种操作,在进行每一种操作前都要进行判断该操作可以进行吗。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath> using namespace std;
struct node
{
int aa;
int bb;
int sgin;//父亲节点
int sp;//父亲节点到该点的操作
};
int a,b,c;
int ff=;
node sk[];//队列
bool sg[][];//记录节点的访问属性
int kiss[];//记录操作 void s1(node &s,int m)//操作1:把a倒满
{
s.aa=a;
s.sgin=m;//记录下父亲节点的位置
s.sp=;//记录下有父亲节点到达该点的操作
}
void s2(node &s,int m)//操作2:把b倒满
{
s.bb=b;
s.sgin=m;
s.sp=;
}
void s3(node &s,int m)//操作3:把a清空
{
s.aa=;
s.sgin=m;
s.sp=;
}
void s4(node &s,int m)//操作4:把b清空
{
s.bb=;
s.sgin=m;
s.sp=;
}
void s5(node &s,int m)//操作5:从a中向b中到溶液
{
if(s.aa+s.bb<=b){
s.bb+=s.aa;
s.aa=;
s.sgin=m;
s.sp=;
}
else{
s.aa=s.aa-(b-s.bb);
s.bb=b;
s.sgin=m;
s.sp=;
}
}
void s6(node &s,int m)//操作6:从b中向a中到溶液
{
if(s.bb+s.aa<=a){
s.aa+=s.bb;
s.bb=;
s.sgin=m;
s.sp=;
}
else{
s.bb=s.bb-(a-s.aa);
s.aa=a;
s.sgin=m;
s.sp=;
}
} int ss(node x)//找出步骤
{
while(){
if(x.sp==)
break;
kiss[ff++]=x.sp;
x=sk[x.sgin];
}
return ;
} int bfs()
{
sk[].aa=;//初始化条件
sk[].bb=;
sk[].sgin=;
sk[].sp=;//挑出步骤的跳出条件
sg[][]=true;
int le=,ri=;
while(le<ri){
node x=sk[le];
if(x.aa==c||x.bb==c){//利用两个杯子倒出了容量c
ss(x);
return ;//利用两个杯子可以倒出容量c,则返回1
}
if(x.aa!=a){//进行操作1时要判断a是否是满的,如果a是满的则不可以进行操作1
s1(x,le);
if(sg[x.aa][x.bb]==false){//操作1进行后,产生了新的点,检验该点被访问你过吗,若果该点别访问过着不可以插入到队列了面
sk[ri++]=x;
sg[x.aa][x.bb]=true;
}
x=sk[le];
}
if(x.bb!=b){//如果b是满的则不可以进行操作2
s2(x,le);
if(sg[x.aa][x.bb]==false){//操作2进行后,产生了新的点,检验该点被访问你过吗,若果该点别访问过着不可以插入到队列了面
sk[ri++]=x;
sg[x.aa][x.bb]=true;
}
x=sk[le];
}
if(x.aa!=){//如果a是空的则不可以进行操作3
s3(x,le);
if(sg[x.aa][x.bb]==false){//操作3进行后,产生了新的点,检验该点被访问你过吗,若果该点别访问过着不可以插入到队列了面
sk[ri++]=x;
sg[x.aa][x.bb]=true;
}
x=sk[le];
}
if(x.bb!=){//如果b是空的则不可以进行操作4
s4(x,le);
if(sg[x.aa][x.bb]==false){//操作4进行后,产生了新的点,检验该点被访问你过吗,若果该点别访问过着不可以插入到队列了面
sk[ri++]=x;
sg[x.aa][x.bb]=true;
}
x=sk[le];
}
if(x.aa!=){//如果a是空的则不可以进行操作5
s5(x,le);
if(sg[x.aa][x.bb]==false){//操作5进行后,产生了新的点,检验该点被访问你过吗,若果该点别访问过着不可以插入到队列了面
sk[ri++]=x;
sg[x.aa][x.bb]=true;
}
x=sk[le];
}
if(x.bb!=){//如果b是空的则不可以进行操作6
s6(x,le);
if(sg[x.aa][x.bb]==false){//操作6进行后,产生了新的点,检验该点被访问你过吗,若果该点别访问过着不可以插入到队列了面
sk[ri++]=x;
sg[x.aa][x.bb]=true;
}
}
le++;
}
return ;//利用两个杯子不可以倒出容量c,则返回0;
} int main()
{
// freopen("input.txt","r",stdin);
while(cin>>a>>b>>c){
memset(sg,false,sizeof(sg));
memset(kiss,,sizeof(kiss));
ff=;
int skn=bfs();
if(skn==){
cout<<ff<<endl;
ff--;
while(ff>=)
{
if(kiss[ff]==){
cout<<"FILL(1)"<<endl;
}
if(kiss[ff]==){
cout<<"FILL(2)"<<endl;
}
if(kiss[ff]==){
cout<<"DROP(1)"<<endl;
}
if(kiss[ff]==){
cout<<"DROP(2)"<<endl;
}
if(kiss[ff]==){
cout<<"POUR(1,2)"<<endl;
}
if(kiss[ff]==){
cout<<"POUR(2,1)"<<endl;
}
ff--;
}
}
else{
cout<<"impossible"<<endl;
}
}
return ;
}
网上他人的代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring> using namespace std; const int maxn=; string op[]={"","FILL(1)","FILL(2)","DROP(1)","DROP(2)","POUR(2,1)","POUR(1,2)"}; int l,r;
int a,b,c;
int vis[maxn][maxn],step[maxn*maxn]; struct node{
int x,y;
int opr;
int pre;
}info[maxn*maxn]; void Solve(int x,int y,int opr){
if(vis[x][y])
return ;
vis[x][y]=;
info[r].x=x;
info[r].y=y;
info[r].opr=opr;
info[r].pre=l;
r++;
} void Print(){
int ans=;
while(l!=){
step[ans++]=info[l].opr;
l=info[l].pre;
}
printf("%d\n",ans);
for(int i=ans-;i>=;i--)
cout<<op[step[i]]<<endl;
} void BFS(){
info[].x=;
info[].y=;
vis[][]=;
l=;
r=;
int tx,ty;
while(l!=r){
if(info[l].x==c || info[l].y==c){
Print();
return ;
} tx=a;
ty=info[l].y;
Solve(tx,ty,); tx=info[l].x;
ty=b;
Solve(tx,ty,); tx=;
ty=info[l].y;
Solve(tx,ty,); tx=info[l].x;
ty=;
Solve(tx,ty,); tx=info[l].x+min(a-info[l].x,info[l].y);
ty=info[l].y-min(a-info[l].x,info[l].y);
Solve(tx,ty,); tx=info[l].x-min(b-info[l].y,info[l].x);
ty=info[l].y+min(b-info[l].y,info[l].x);
Solve(tx,ty,); l++;
}
if(l>=r)
printf("impossible\n");
} int main(){ //freopen("input.txt","r",stdin); while(~scanf("%d%d%d",&a,&b,&c)){
memset(vis,,sizeof(vis));
BFS();
}
return ;
}
Pots(BFS)的更多相关文章
- poj3414 Pots (BFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12198 Accepted: 5147 Special J ...
- 【POJ - 3414】Pots(bfs)
Pots 直接上中文 Descriptions: 给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作 FILL(i) 将第i个容器从水龙头里装满(1 ≤ i ≤ 2); DRO ...
- POJ-3414 Pots (BFS)
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 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i) fill the ...
- poj3414 Pots(BFS)
题目链接 http://poj.org/problem?id=3414 题意 有两个杯子,容量分别为A升,B升,可以向杯子里倒满水,将杯子里的水倒空,将一个杯子里的水倒到另一个杯子里,求怎样倒才能使其 ...
- POJ 3414 Pots(罐子)
POJ 3414 Pots(罐子) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 You are given two po ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
随机推荐
- USACO 3.2 Stringsobits
StringsobitsKim Schrijvers Consider an ordered set S of strings of N (1 <= N <= 31) bits. Bits ...
- js 仿 asp中的 asc 和 chr 函数的代码
<script type="text/javascript">var str;var asc; str = "A";document.write(s ...
- js中style的属性
下面这些属性都是通过js的style来设置css.只是整理了一部分,详细的可以参考相应的学习网站,不好的地方欢迎大家拍砖. alignContent :"" 属性在弹性容器内的各项 ...
- ndk搭建与运行
1)打开Android开发者的官网http://developer.android.com/找到Develop点击.如果页面打不开,通过代理来访问. 2)进入后再点击Tools 3)进入后在左侧找到N ...
- Mysql表结构定义及相关语法
mysql语法及相关命令1.每个sql命令都需要使用分号来完成2.可以将一个命令写成多行3.可以通过\c来取消本行命令4.可以通过\g.exit.ctrl+c或者quit来退出当前客户端5.可以通过使 ...
- Design Pattern——Factory_DP
namespace TEST { //用于生成一个对应的操作类,这个工厂只是用来产生操作类的,不做其他只用 public class Factory { public Operate GetOpetr ...
- Django 1.8 - “No migrations to apply” when run migrate after makemigrations 解决办法
解决办法 1 删除应用migrations目录 2 删除MySQL中django_migrations中对应的行(delete from django_migrations where app='ap ...
- 拔高课程_day14_课堂笔记
今日大纲 Redis的持久化 Redis的主从 Redis的集群 mysql 优化 tomcat优化 Redis的持久化 持久化 持久化,就是将数据保存到磁盘,机器宕机或者重启数据不丢失,如果存储到内 ...
- Recover the String
Recover the String 题目链接:http://codeforces.com/contest/709/problem/D 构造 这题乍一看很难构造,但是如果知道了整个字符串中'0'和'1 ...
- 计算机网络课程优秀备考PPT之第五章网络层(五)
为了记录自己从2016.9~2017.1的<计算机网络>助教生涯,也为了及时梳理和整写笔记! 前期博客是, 计算机网络课程优秀备考PPT之第一章概述(一) 计算机网络课程优秀备考PPT之第 ...