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.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
随机推荐
- CodeForces 645D Robot Rapping Results Report
二分,拓扑排序. 二分答案,然后进行拓扑排序检查,若某次发现存在两个或者两个以上入度为$0$的节点,那么不可行. #pragma comment(linker, "/STACK:102400 ...
- [UWP小白日记-1]判断APP是否是第一次运行初始化SQLITE数据库
利用应用程序设置来实现此功能. 1.首先,获取APP设置的容器: ApplicationDataContainer localSettings = ApplicationData.Current.Lo ...
- google谷歌翻译插件-网页一键翻译
上个月转载的一篇博文,是推荐的四款非常实用的翻译插件,这几天看这个chrome插件网首页有新增了一个google谷歌翻译插件.我能说实话,这款插件比之前推荐的4款翻译插件更好用吗?也不能完全说是更好用 ...
- Swift中GCD与NSOperation相关
GCD Swift 3必看:从使用场景了解GCD新API 常用写法: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_ ...
- 阿里云ECS-Nginx阿里云客户端IP日志记录
#前端有SLB服务,记录客户端真实IP信息 log_format main 'realip:$http_x_forwarded_for slbip:$remote_addr-$remote_user ...
- androidstudio下载地址
google官网地址 https://developer.android.com/studio/index.html
- 高一的我曾对自己说"要放慢脚步去生活"!?
看了高一的时候自己写的日记,瞬间被自己感动到了.以下是当时的几段感慨: 慢是一种放松.是生活的一种良好心态,喜欢这样放慢步伐地生活,那是一种享受! 但我们生活在一个快节奏的时代,我们总是被迫卷进那潮流 ...
- namenode无法启动
查看日志错误信息关键语句: There appears to be a gap in the edit log. We expected txid 44353, but got txid 原因: n ...
- vs2015下载及预览与发布
VS2015 RC发布下载,通用Windows平台必备神器! 几个月前微软发布了Visual Studio 2015的技术预览版本,之后又先后发布了6个更新版本.如今,微软已在其官方页面中公布了最新开 ...
- day01(RESTful Web Service、SVN)
今日大纲 搭建SSM环境 基于SSM环境实现用户管理系统 学习RESTful Web Service 学习SVN 统一开发环境 JDK1.7 32? 64? -- 64 Eclipse 使用4.4.1 ...