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 ...
随机推荐
- const char*, char const* and char *const 分类: C/C++ OpenCV 2014-11-08 18:10 114人阅读 评论(0) 收藏
const char*, char const*, char*const的区别问题几乎是C++面试中每次都会有的题目. 事实上这个概念谁都有只是三种声明方式非常相似很容易记混. Bjarne在他的 ...
- 在LINUX中跟踪函数调用----http://stackoverflow.com/
http://stackoverflow.com/questions/311840/tool-to-trace-local-function-calls-in-linux I am looking f ...
- Java SE 6 新特性: 编译器 API
新 API 功能简介 JDK 6 提供了在运行时调用编译器的 API,后面我们将假设把此 API 应用在 JSP 技术中.在传统的 JSP 技术中,服务器处理 JSP 通常需要进行下面 6 个步骤: ...
- Objective-C--Runtime机制
个人理解: 简单来说,Objective-C runtime是一个实现Objective-C语言的C库.对象可以用C语言中的结构体表示,而方法(methods)可以用C函数实现.事实上,他们 差不多也 ...
- Android(java)学习笔记200:Android中View动画之 XML实现 和 代码实现
1.Animation 动画类型 Android的animation由四种类型组成: XML中: alph 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动 ...
- windows10UWP:如何在xaml中设置控件为 public ?
windows10UWP中,由于使用页面导航,操作在不同一个页面的控件需求经常遇到. 如果要对另一个page里面的控件进行操作,那么这个控件必须设置为 public .在 xaml 设置控件的方法是: ...
- SQL查询显示行号、随机查询、取指定行数据
转自:walkingp 1.显示行号 如果数据没有删除的情况下主键与行号是一致的,但在删除某些数据,行号就与主键不一致了,这时需要查询行号就需要用新的方法,在SQL Server2005之前,需要使用 ...
- mailsend - Send mail via SMTP protocol from command line
Introduction mailsend is a simple command line program to send mail via SMTP protocol. I used to sen ...
- SQL存储过程基于字段名传入的字符串拼接.
--定义存储过程. Create PROCEDURE Usp_Static ), ), --分组字段. ), --统计字段. ), --表头字段. ) --聚会函数. AS ) --存储游标执行的列. ...
- SQLite 入门教程(三)好多约束 Constraints
一.约束 Constraints 在上一篇随笔的结尾,我提到了约束, 但是在那里我把它翻译成了限定符,不太准确,这里先更正一下,应该翻译成约束更贴切一点. 那么什么是约束呢? 我们在数据库中存储数据的 ...