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.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
随机推荐
- XTU 1249 Rolling Variance
$2016$长城信息杯中国大学生程序设计竞赛中南邀请赛$G$题 前缀和. 把公式化开来,会发现只要求一段区间的和以及一段区间的平方和就可以了. #pragma comment(linker, &quo ...
- css-div下内容垂直居中
1.多行行文字在固定高度的div中垂直居中,只兼容高级浏览器和移动端 .detail { width: 395px; height: 289px; display: -webkit-box; -web ...
- Java基本知识
一.I/O 分字节流和字符流 字节流由InputStream和OutputStream读入和写入 DataInputStream继承自FilterInputStream,可以读取基本数据类型(char ...
- 从P1到P7——我在淘宝这7年(转)
作者: 赵超 发布时间: 2012-02-25 14:47 阅读: 114607 次 推荐: 153 [收藏] (一) 2011-12-08 [原文链接] 今天有同事恭喜我,我才知道自己在淘 ...
- winform窗体对象 单例模式与泛型结合
实现弹出窗体对象的单例模式 结合泛型后,可以用于所有窗体的弹出操作 public class BaseFrm<T> where T : Form, new() { //定义一个静态的,私 ...
- 实战荟萃-UI篇
一. 前言 平时在处理问题的时候,经常会遇到一些奇奇怪怪的问题,今天在这里将其记录下来.这里将会列举几个常用的UI问题进行讲解 二. 导航栏 iOS导航栏绝对是个巨坑.和很多朋友聊天都是自己实现了一套 ...
- 更新sdk
更新sdk,遇到了更新下载失败问题: Fetching https://dl-ssl.google.com/android/repository/addons_list-2.xmlFetched Ad ...
- iOS蓝牙开发
蓝牙常见名称和缩写 MFI ======= make for ipad ,iphone, itouch 专们为苹果设备制作的设备 BLE ==== buletouch low energy,蓝牙4.0 ...
- 安装Redis无错流程
1.参考文章<安装3.0.3版本配置文章参考>http://www.iyunv.com/thread-89612-1-1.html 2.安装tcl组件包(安装Redis需要tcl支持) 下 ...
- Nginx将项目配置在子目录
问题:一个完整的项目需要整合在另外一个项目中,作为一个子模块存在 有两个项目prject1 根目录/www/project1与project2 /www/project2,现在是想将probject1 ...