Pots

时间:2022-07-13 13:47:53

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的更多相关文章

  1. POJ 3414 Pots

    Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status  ...

  2. 广搜&plus;输出路径 POJ 3414 Pots

    POJ 3414 Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13547   Accepted: 5718   ...

  3. Pots 分类: 搜索 POJ 2015-08-09 18&colon;38 3人阅读 评论&lpar;0&rpar; 收藏

    Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11885 Accepted: 5025 Special Judge D ...

  4. Pots of gold game&colon;看谁拿的钱多

    问题描述: Pots of gold game: Two players A & B. There are pots of gold arranged in a line, each cont ...

  5. (poj)3414 Pots (输出路径的广搜)

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  6. POJ 3414 Pots【bfs模拟倒水问题】

    链接: http://poj.org/problem?id=3414 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22009#probl ...

  7. Pots&lpar;bfs&rpar;

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8266   Accepted: 3507   Special Judge D ...

  8. POJ 3414 Pots 记录路径的广搜

    Description You are given two pots, having the volume of A and B liters respectively. The following ...

  9. poj 3414 Pots (bfs&plus;线索)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10071   Accepted: 4237   Special J ...

  10. POJ 3414 Pots&lpar;BFS&rpar;

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Description You are g ...

随机推荐

  1. WordCount示例深度学习MapReduce过程(1)

    我们都安装完Hadoop之后,按照一些案例先要跑一个WourdCount程序,来测试Hadoop安装是否成功.在终端中用命令创建一个文件夹,简单的向两个文件中各写入一段话,然后运行Hadoop,Wou ...

  2. ASP&period;NET MVC ActionFilterAttribute的执行顺序

    http://diaosbook.com/Post/2014/6/3/execution-order-of-actionfilter-aspnet-mvc ASP.NET MVC里面我们要自定义Act ...

  3. 读写XML

    XML: 代码: //实例化 XmlDocument xmldc = new XmlDocument(); //加载xml文件,参数是路径. xmldc.Load("C:/Users/Des ...

  4. JDK自带VM分析工具jps,jstat,jmap,jconsole

    一.概述 SUN 的JDK中的几个工具,非常好用.秉承着有免费,不用商用的原则.以下简单介绍一下这几种工具.(注:本文章下的所有工具都存在JDK5.0以上版本的工具集里,同javac一样,不须特意安装 ...

  5. 20175204 张湲祯 2018-2019-2《Java程序设计》

    Arrays和String单元测试 一.类的作用 1.- String类 charAt String的charAt的作用是将字符串中第i个位置上的字符(从0开始计数)赋值给n,其用法为n=string ...

  6. 2018-icpc沈阳-G-思维

    http://codeforces.com/gym/101955/problem/G 给出一个6000*6000的坐标系,有四种操作,一是加入放置一个点到某个空格子上,二是从某个有点的格子移走一个点, ...

  7. 《从Lucene到Elasticsearch:全文检索实战》学习笔记五

    今天我给大家讲讲tf-idf权重计算 tf-idf权重计算: tf-idf(中文词频-逆文档概率)是表示计算词项对于一个文档集或语料库中的一份文件的重要程度.词项的重要性随着它在文档中出现的次数成正比 ...

  8. (转)C&num; 的 String&period;CompareTo、 Equals和&equals;&equals;的比较

    String.CompareTo 语法 public int CompareTo(    string strB) 返回值 小于 0,实例小于参数 strB: 0,实例等于参数 strB: 大于 0, ...

  9. 组合数问题&lpar;NOIP2016&rpar;

    题目链接:组合数问题 这道题可以算当年第二简单的. 这里要用到两个技巧: 用杨辉三角递推计算组合数 运用前缀和 有了这两点,这道题就出来了. 我们先运用杨辉三角推出题目范围内所能用到的所有组合数,然后 ...

  10. vue导航栏实时获取URL设置当前样式,刷新也存在!

    很low 别喷, template代码: <div class="tab-itme"> <ul @click="clickit()"> ...