Pots
poj3414:http://poj.org/problem?id=3414
题意:给你两个罐子的体积,然后让你只用这两个罐子量出给定k体积的水。
题解:这里可以把两个罐子看成是一个二维的图,然后体积的水就是图中其中一个坐标是k的点。可以直接BFS,每次操作就相当于从当前的点向外扩展,并且记录当前的路径,即可。
其中可以用1,2,3,4,5,6六个数字来分别对应六种操作,然后用一个int类型的数组记录路径就可以。
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cstdio>
#include<queue>
using namespace std;
struct Node{
int counts1;//第一个pot中当前的水
int counts2;//第二个pot中当前的水
int step;//当前的步数
int path[];//记录到达这一步之前的以及这一步的路径
};
int counts[][];//counts【i】【j】表示到达第一个pot中是i,
int a,b,k; //第二个pot中是j这种状态所需要的最小的步数
void print(int x){//打印出对印标记的输出路径上每一个数字代表一种操作,六个数字对印六种操作
if(x==)printf("FILL(1)\n");
if(x==)printf("FILL(2)\n");
if(x==)printf("DROP(1)\n");
if(x==)printf("DROP(2)\n");
if(x==)printf("POUR(2,1)\n");
if(x==)printf("POUR(1,2)\n");
}
void BFS(){
bool flag=false;//标记是否有解
for(int i=;i<=;i++)
for(int j=;j<=;j++)//初始化
counts[i][j]=;
queue<Node>Q;
Node tt;//队首元素
tt.counts1=;//初始时候都是0,0
tt.counts2=;
tt.step=;
counts[][]=;//别忘了注意这里的初始化0
Q.push(tt);
while(!Q.empty()){
Node temp=Q.front();//取出队首元素
Q.pop();
int step=temp.step;
int sum1=temp.counts1;
int sum2=temp.counts2;
if(sum1==k){//只要其中一个罐子出现k,则直接输出
printf("%d\n",step);
for(int i=;i<=step;i++)//输出路径
print(temp.path[i]);
flag=true;
break;
}
if(sum2==k){
printf("%d\n",step);
for(int i=;i<=step;i++)
print(temp.path[i]);
flag=true;
break;
}
if(sum1<a&&counts[a][sum2]>step+){//当第一个罐子没满,这是可以把第一个罐子灌满
Node ttt;
ttt.counts1=a;
ttt.counts2=sum2;
ttt.step=step+;
for(int i=;i<=step;i++)//复制之前的路径
ttt.path[i]=temp.path[i];
ttt.path[step+]=;
Q.push(ttt);
counts[a][sum2]=step+; }
if(sum2<b&&counts[sum1][b]>step+){//灌满第二个罐子
Node ttt;
ttt.counts1=sum1;
ttt.counts2=b;
ttt.step=step+;
for(int i=;i<=step;i++)
ttt.path[i]=temp.path[i];
ttt.path[step+]=;
Q.push(ttt);
counts[sum1][b]=step+;
}
if(sum1>&&counts[][sum2]>step+){//清空第一个罐子
Node ttt;
ttt.counts1=;
ttt.counts2=sum2;
ttt.step=step+;
for(int i=;i<=step;i++)
ttt.path[i]=temp.path[i];
ttt.path[step+]=;
Q.push(ttt);
counts[][sum2]=step+;
}
if(sum2>&&counts[sum1][]>step+){//清空第二个罐子
Node ttt;
ttt.counts1=sum1;
ttt.counts2=;
ttt.step=step+;
for(int i=;i<=step;i++)
ttt.path[i]=temp.path[i];
ttt.path[step+]=;
Q.push(ttt);
counts[sum1][]=step+;
}
if(sum2+sum1>=a&&counts[a][sum2+sum1-a]>step+){//把第二个导入第一个并且第一个装满之后,第二个还有剩余
Node ttt;
ttt.counts1=a;
ttt.counts2=sum2+sum1-a;
ttt.step=step+;
for(int i=;i<=step;i++)
ttt.path[i]=temp.path[i];
ttt.path[step+]=;
Q.push(ttt);
counts[a][sum2+sum1-a]=step+;
}
if(sum2+sum1<a&&counts[sum1+sum2][]>step+){////把第二个导入第一个并且第一个装满之后,第二个没有剩余
Node ttt;
ttt.counts1=sum1+sum2;
ttt.counts2=;
ttt.step=step+;
for(int i=;i<=step;i++)
ttt.path[i]=temp.path[i];
ttt.path[step+]=;
Q.push(ttt);
counts[sum1+sum2][]=step+;
}
if(sum2+sum1>=b&&counts[sum1+sum2-b][b]>step+){//与上面正好相反
Node ttt;
ttt.counts1=sum1+sum2-b;
ttt.counts2=b;
ttt.step=step+;
for(int i=;i<=step;i++)
ttt.path[i]=temp.path[i];
ttt.path[step+]=;
Q.push(ttt);
counts[sum1+sum2-b][b]=step+;
}
if(sum2+sum1<b&&counts[][sum1+sum2]>step+){
Node ttt;
ttt.counts1=;
ttt.counts2=sum1+sum2;
ttt.step=step+;
for(int i=;i<=step;i++)
ttt.path[i]=temp.path[i];
ttt.path[step+]=;
Q.push(ttt);
counts[][sum1+sum2]=step+;
} }
if(!flag)printf("impossible\n");
}
int main(){
scanf("%d%d%d",&a,&b,&k);
BFS();
}
Pots的更多相关文章
- POJ 3414 Pots
Pots Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status ...
- 广搜+输出路径 POJ 3414 Pots
POJ 3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13547 Accepted: 5718 ...
- 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 ...
- (poj)3414 Pots (输出路径的广搜)
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 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...
- Pots(bfs)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8266 Accepted: 3507 Special Judge D ...
- POJ 3414 Pots 记录路径的广搜
Description You are given two pots, having the volume of A and B liters respectively. The following ...
- poj 3414 Pots (bfs+线索)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10071 Accepted: 4237 Special J ...
- POJ 3414 Pots(BFS)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Description You are g ...
随机推荐
- MS SQL 性能优化
http://blog.csdn.net/dba_huangzj/article/details/50455543
- Qt c++11
借助 Qt 5 的信号槽语法,我们可以将一个对象的信号连接到 Lambda 表达式,例如: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 // !!! Q ...
- 创建文档和自定义的qt assistant
利用qt制作帮助文档 1. 创建文档即是一些html文件,这里可以使用qt提供的工具像是qdoc 和Doxygen生成帮助的html文档. 2. 组织文档结构用于qt ...
- common-lang——StringUtils
1.文字省略处理 最多显示几个字 StringUtils.abbreviate("中华人民共和国", 5); // output:中华.. 2.文字中间省略 最多显示几个字符 St ...
- iOS viewController 和 view 的创建消失生命周期总结
控制器创建的生命周期 1. 如果从stroryBoard 中产生一个controller,那么会先调用initWithCoder:, awakeFromNib, loadView,viewDidLoa ...
- idea使用笔记
常用快捷键 ctrl+shift+f12 编辑器全屏 win8下输入法不跟随 使用微软输入法即可 默认设置 之前创建maven工程 每次都要选择自己的版本,原来有个默认全局设置 创建maven模板工程 ...
- codevs1506传话(kosaraju算法)
- - - - - - - - 一个()打成[] 看了一晚上..... /* 求强连通分量 kosaraju算法 边表存图 正反构造两个图 跑两边 分别记下入栈顺序 和每个强连通分量的具体信息 */ ...
- Unity3D 相机跟随主角移动
这里给主相机绑定一个脚本. 脚本写为: using UnityEngine; using System.Collections; public class camerafollow : MonoBeh ...
- 关于 Repository和UnitOfWork 模式的关系
本以为,关于这方面的理解,园子中的文章已经很多的了,再多做文章真的就“多做文章了”,但是最近发现,还是有必要的,首先,每个人对于同一事物的理解方式和出发点都是不同的,所以思考的方式得到结果也是不同的. ...
- VMware复制Centos6虚拟机要改的地方
1.删除文件 /etc/udev/rules.d/70-persistent-net.rules (它会绑定你网卡信息) 2.重新配置 # vi /etc/sysconfig/network-scr ...