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 ...
随机推荐
- Android Canvas drawText实现中文垂直居中
目标: 把中文字符绘制到目标矩形的居中位置. 问题: Android的Canvas绘图,drawText里的origin是以baseline为基准的,直接以目标矩形的bottom传进drawText, ...
- android 40 Io编程
Io编程:内存卡和sd卡.字符串存入内存卡然后读出来. activity: package com.sxt.day06_06; import java.io.FileInputStream; impo ...
- 【网络通信】服务器端Socket监听80端口,建立连接传输数据时也是使用的80端口么?
1. 服务器端Socket监听80端口,建立连接传输数据时也是使用的80端口么? 答:对.建立连接时服务器会分配一个新的Socket,但是用的源端口号还是80端口.套接字是由协议类型.源IP.目的IP ...
- 为什么objc_msgSend必须用汇编实现
译者前言 总是看到有人说用汇编实现objc_msgSend是为了速度快,当然这个不可否认.但是难道没有别的原因?于是就看到了这篇文章,遂翻译之!=.= 我自己的理解就是,用汇编实现,是为了应对不同的“ ...
- jersey + tomcat 实现restful风格
本文参考 http://www.cnblogs.com/bluesfeng/archive/2010/10/28/1863816.html 环境: idea 15.0.2 jersey 1.3 tom ...
- oracle中的层级递归查询操作
oracle中的层级操作非常方便,在使用之后爱不释手,以前要实现该种数据查询操作,需要非常复杂的实现过程.在oracle中通过connect by可以实现前面的目的,通常情况下层级查询基本都能实现递归 ...
- Java方法-数组
[Java数组] 1. 用sort()方法对Java数组进行排序,及如何使用 binarySearch() 方法来查找数组中的元素 binarySearch() 返回值: 如果它包含在数组中,则返回搜 ...
- 同一台电脑上安装两个tomcat服务器
1.下载免安装版tomcat,解压成tomcat1.tomcat2: 2.修改tomcat2中conf下server.xml文件如下: <Server port="8005" ...
- PL/SQL Developer简单使用
表在以下:
- Objective-C中的分类与协议
分类 在谈分类之前,我们可以先探究下,OC中为什么出现分类这种机制,有什么好处? 假设你接到一个大项目:计算两个整数的和,差.接到任务的你马上动手.编写代码如下: #import <Founda ...