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 ...
随机推荐
- Assigning retained object to unsafe property;object will be released after assignment
解决方法,将变量 @property (assign) UILabel *titleView; 改为 @property (retain) UILabel *titleView;
- 手机相机ISO是什么
要说什么是ISO还要从传统胶片相机说起,ISO被 称为感光度,它是衡量传统相机所使用胶片感光速度的国际统一指标,其数值反映了胶片感光时的速度(其实是银元素与光线的光化学反应速率).而对于现在并不 使用 ...
- Apache让一台虚拟主机接受多域名解析(转)
之前写了一篇文章关于linux下apache虚拟主机配置,配置那是相当简单: <VirtualHost *:80> ServerAdmin admin@example.com Docume ...
- android 03 TableLayout
MainActivity.java(默认的,什么都没有) package com.sxt.day02_02; import android.os.Bundle; import android.app. ...
- unlocker208安装之后看不到Apple macos选项,解决办法.
前段时间升级了win10,最新的unlocker208安装以后看不到mac os的选项,为什么呢?我们在管理员允许win-install.cmd的过程中,会在cmd中看到这么一句话:LookupErr ...
- Java多线程——其他工具类CyclicBarrier、CountDownLatch和Exchange
CyclicBarrier 适用于:创建一组任务,它们并行地执行任务,然后在进行下一个步骤之前等待,直至所有任务完成.它使得所有的并行任务都将在栅栏处列队,因此可以一致地向前移动. 表示大家彼此等待, ...
- 完整的 dataType=text/plain jquery ajax 登录验证
Html: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <m ...
- 【POJ2761】【fhq treap】A Simple Problem with Integers
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- Linux下安装SVN服务(CentOS7下)
1. 安装 centos(我这里使用的是CentOS7)下yum命令即可方便的完成安装 测试安装是否成功: 2. 建立版本库 创建svn数据目录(subversion默认是把/var/svn作为数据根 ...
- js string操作总结
var str = "0123456789"; console.log(str.substring(0)); //------------"0123456789" ...