Pots——BFS

【题目描述】

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

FILL(i) fill the pot i (1 ≤ i ≤ 2) from the tap;
DROP(i) empty the pot i to the drain;
POUR(i,j) pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).

Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.
Input

On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).
Output

The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.
Sample Input

3 5 4

Sample Output

6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

【题目分析】
是一道很裸的BFS,就是稍微复杂一点,在做过非常可乐(也是一道BFS)后,这道题简直一模一样,就是稍微复杂一点点,注意细节
还有就是题目要求输出步骤,这就需要保存一下路径,我用了一个vector(发现STL好好用)
【AC代码】

#include<cstdio>
#include<cstring>
#include<queue>
#include<climits>
#include<vector>using namespace std;const int MAXN=105;
int A,B,C;
int check[MAXN][MAXN];
struct node1
{int x; int a;
}tt;
struct node
{int a,b;vector<node1> path;int step;
}t,p,ans;bool BFS()
{queue<node> q;p.a=0; p.b=0; p.path.clear(); p.step=0;q.push(p);memset(check,-1,sizeof(check));check[0][0]=0;while(!q.empty()){p=q.front(); q.pop();if(p.a==C || p.b==C){ans=p;return true;}for(int i=1;i<=3;i++){if(i==1){for(int j=1;j<=2;j++){if(j==1 && p.a<A){t=p;t.a=A;if(check[t.a][t.b]!=-1) continue;tt.x=1; tt.a=1;t.path.push_back(tt);t.step++;check[t.a][t.b]=t.step;q.push(t);}else if(j==2 && p.b<B){t=p;t.b=B;if(check[t.a][t.b]!=-1) continue;tt.x=1; tt.a=2;t.path.push_back(tt);t.step++;check[t.a][t.b]=t.step;q.push(t);}}}else if(i==2){for(int j=1;j<=2;j++){if(j==1 && p.a>0){t=p;t.a=0;if(check[t.a][t.b]!=-1) continue;tt.x=2; tt.a=j;t.path.push_back(tt);t.step++;check[t.a][t.b]=t.step;q.push(t);}else if(j==2 &&p.b>0){t=p;t.b=0;if(check[t.a][t.b]!=-1) continue;tt.x=2; tt.a=j;t.path.push_back(tt);t.step++;check[t.a][t.b]=t.step;q.push(t);}}}else if(i==3){for(int j=1;j<=2;j++){if(j==1 && p.a>0 && p.b<B){t=p;if(p.a>=B-p.b){t.a=t.a+t.b-B;t.b=B;}else{t.b+=t.a;t.a=0;}if(check[t.a][t.b]!=-1) continue;tt.x=3; tt.a=1;t.path.push_back(tt);t.step++;check[t.a][t.b]=t.step;q.push(t);}else if(j==2 && p.b>0 && p.a<A){t=p;if(p.b>=A-p.a){t.b=t.a+t.b-A;t.a=A;}else{t.a+=t.b;t.b=0;}if(check[t.a][t.b]!=-1) continue;tt.x=3; tt.a=2;t.path.push_back(tt);t.step++;check[t.a][t.b]=t.step;q.push(t);}}}}}return false;
}int main()
{scanf("%d%d%d",&A,&B,&C);if(BFS()){printf("%d\n",ans.step);for(int i=0,j=ans.step;i<j;i++){if(ans.path[i].x==1){printf("FILL(%d)\n",ans.path[i].a);}else if(ans.path[i].x==2){printf("DROP(%d)\n",ans.path[i].a);}else if(ans.path[i].x==3){int tmp=(ans.path[i].a==1)?2:1;printf("POUR(%d,%d)\n",ans.path[i].a,tmp);}}}else{printf("impossible");}return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/384145.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

HDU - 4578Transformation——线段树+区间加法修改+区间乘法修改+区间置数+区间和查询+区间平方和查询+区间立方和查询

【题目描述】 HDU - 4578Transformation Problem Description Yuanfang is puzzled with the question below: There are n integers, a1, a2, …, an. The initial values of them are 0. There are four kinds of operations. Operation 1: Add c to each number between ax …

[C++基础]034_C++模板编程里的主版本模板类、全特化、偏特化(C++ Type Traits)

http://www.cnblogs.com/alephsoul-alephsoul/archive/2012/10/18/2728753.html 1. 主版本模板类 首先我们来看一段初学者都能看懂&#xff0c;应用了模板的程序&#xff1a; 1 #include <iostream>2 using namespace std;3 4 template<class T1, class T2>5 clas…

自定义类型: 结构体,枚举,联合

1.结构体 个人认为结构体和数组特别相似&#xff0c;只不过结构体和数组的区别在于结构体的成员可以是不同类型&#xff0c;而数组成员类型是相同的。 &#xff08;1&#xff09;结构体的声明 struct tag {成员列表//至少得有一个成员 }值列表;//值列表可以省略 struct {int a…

详解C++中的函数调用和下标以及成员访问运算符的重载

http://www.jb51.net/article/78436.htm 这篇文章主要介绍了详解C中的函数调用和下标以及成员访问运算符,讲到了这些二元运算符使用的语法及重载,需要的朋友可以参考下函数调用 使用括号调用的函数调用运算符是二元运算符。 语法 ?1primary-expression ( expression-list )备…

A计划——BFS

【题目描述】 可怜的公主在一次次被魔王掳走一次次被骑士们救回来之后&#xff0c;而今&#xff0c;不幸的她再一次面临生命的考验。魔王已经发出消息说将在T时刻吃掉公主&#xff0c;因为他听信谣言说吃公主的肉也能长生不老。年迈的国王正是心急如焚&#xff0c;告招天下勇士…

使用openssl的md5库

http://blog.csdn.net/sinat_35297665/article/details/78244523 在linux机器上&#xff0c;有一个命令可以计算出文件的md5值&#xff0c;那就是md5sum&#xff0c;如果没有的话&#xff0c;就需要安装RPM包&#xff1a;coreutils。 现在我们使用openssl的库也可以方便的计算出…

主席树入门

今天学习了一下主席树&#xff08;名字这么强的嘛&#xff09; 虽然直接理解起来不容易&#xff0c;但是这种解决问题的思想其实并不陌生。 我们可以首先来看维护整个区间第K大的线段树 我们将[l,r]区间内数字的多少用线段树进行维护&#xff0c;这样的话为了求取区间第k大&…

Socket网络编程--小小网盘程序(1)

http://www.cnblogs.com/wunaozai/p/3886588.html 这个系列是准备讲基于Linux Socket进行文件传输。简单的文件传输就是客户端可以上传文件&#xff0c;可以从服务器端下载文件。就这么两个功能如果再加上身份验证&#xff0c;就成了FTP服务器了&#xff0c;如果对用户的操作再…

使用 Verdaccio 构建自己的私有 npm 仓库

前言 无论你是公司的开发者&#xff0c;还是个人开发者&#xff0c;你可能都听说过或者使用过 npm&#xff0c;这是一个使用广泛的 JavaScript 包管理器。但是&#xff0c;你是否遇到过以下的问题&#xff1a;你需要一个私有的包存放地方&#xff0c;或者你需要在离线环境下使…

HDU - 4348To the moon——主席树+区间修改

HDU - 4348To the moon 【题目描述】 【题目分析】 题目中说明每次更新后时间都会加1&#xff0c;而且还会需要查询以前的区间&#xff0c;还会需要返回以前的时间&#xff0c;所以是很裸的主席树。区间查询的话我们同样需要用到lazy标记 通过这道题我发现线段树的操作还是很灵…

进入一个目录需要那些权限

1.文件访问者的分类 文件的访问者具体可分为以下几类&#xff1a; (1)拥有者 (2)组拥有者 (3)其他用户 这些都代表什么意思呢&#xff1f; 其中r表示只读&#xff0c;w表示只写&#xff0c;x表示可执行&#xff0c;第一个字母代表了文件的类型&#xff0c;其中文件类型可以分为…

Socket网络编程--小小网盘程序(2)

http://www.cnblogs.com/wunaozai/p/3887728.html 这一节将不会介绍太多的技术的问题&#xff0c;这节主要是搭建一个小小的框架&#xff0c;为了方便接下来的继续编写扩展程序。本次会在上一小节的基础上加上一个身份验证的功能。 因为网盘程序不像聊天程序&#xff0c;网盘是…

Linux下的重要目录

1.bin目录 主要防止系统下的各种必备可执行文件 2./proc 目录 这个目录相当于Windows下的计算机系统信息查看以及进程动态查看&#xff0c;可以查看计算机信息&#xff0c;用来存放当前计算机上的进程信息 3./sys 目录 (1)其中block目录用于存放块设备文件 (2)bus存放总线类型…

HDU - 6278 Just $h$-index主席树+二分

HDU - 6278 Just hhh-index 【题目描述】 【题目分析】 题目要求在区间[l,r][l,r][l,r]内大于h的数不少于h个&#xff0c;对于这种最大化问题&#xff0c;我们应该想到二分。 最小情况显然是1.最大情况显然是r−l1r-l1r−l1&#xff0c;对于一个hhh&#xff0c;我们如何判断能…

Socket网络编程--小小网盘程序(3)

http://www.cnblogs.com/wunaozai/p/3891062.html 接上一小节&#xff0c;这次增加另外的两张表&#xff0c;用于记录用户是保存那些文件。增加传上来的文件的文件指纹&#xff0c;使用MD5表示。 两张表如下定义: 1 create table files(2 fid int,3 filename varchar(64),4 md…

LInux下du, df, top, free, pstack, su, sudo, adduser, password命令

1.du命令&#xff1a;du [选项] 文件 (1)功能该命令是显示指定文件以及下的所有文件占用系统数据块的情况&#xff0c;如果没有文件&#xff0c;默认为是当前工作目录 -a    显示所有文件对系统数据块的使用情况 -b    显示数据块大小时以字节为基本单位 -c    除了显…

HDU - 5919 Sequence II——主席树+区间种类++逆序建树

【题目描述】 HDU - 5919 Sequence II 【题目分析】 题目给定一个数组&#xff0c;每次查询一个区间&#xff0c;找出区间内不同数字的个数x&#xff0c;然后输出按出现顺序第x/2向上取整个数字的位置。 按照要求&#xff0c;我们首先需要能够找出给定区间不同的数字个数。 首…

Socket网络编程--小小网盘程序(4)

http://www.cnblogs.com/wunaozai/p/3892729.html 在这一小节中实现了文件的下载&#xff0c;具体的思路是根据用户的uid和用户提供的文件名filename联合两张表&#xff0c;取得md5唯一标识符&#xff0c;然后操作这个标识符对应的文件发送给客户端。 实现下载的小小网盘程序 …

静态顺序表的实现

实现对顺序表的初始化&#xff0c;头插&#xff0c;头删&#xff0c;尾插&#xff0c;尾删&#xff0c; 任意下标的删除&#xff0c; 任意下标处的的元素删除&#xff0c;任意下标处的元素插入&#xff0c;任意元素的下标返回&#xff0c;任意下标处的元素返回&#xff0c; 删除…

树链剖分入门+HYSBZ - 1036树的统计Count

今天学习了树链剖分&#xff0c;记录一下。 【题目背景】 HYSBZ - 1036树的统计Count 【题目分析】 题目要求求任意结点之间路径的和以及路径上最大的结点&#xff0c;还有可能修改。如果正常做可能会很复杂&#xff08;我也不知道正常应该怎么做&#xff0c;应该要用到LCA什么…