《算法笔记》8.2小节——搜索专题->广度优先搜索(BFS)问题 A: Jugs

embedded/2025/3/15 0:29:53/
题目描述

In the movie "Die Hard 3", Bruce Willis and Samuel L. Jackson were confronted with the following puzzle. They were given a 3-gallon jug and a 5-gallon jug and were asked to fill the 5-gallon jug with exactly 4 gallons. This problem generalizes that puzzle.

    You have two jugs, A and B, and an infinite supply of water. There are three types of actions that you can use: (1) you can fill a jug, (2) you can empty a jug, and (3) you can pour from one jug to the other. Pouring from one jug to the other stops when the first jug is empty or the second jug is full, whichever comes first. For example, if A has 5 gallons and B has 6 gallons and a capacity of 8, then pouring from A to B leaves B full and 3 gallons in A.

    A problem is given by a triple (Ca,Cb,N), where Ca and Cb are the capacities of the jugs A and B, respectively, and N is the goal. A solution is a sequence of steps that leaves exactly N gallons in jug B. The possible steps are

    fill A 
    fill B 
    empty A 
    empty B 
    pour A B 
    pour B A 
    success

    where "pour A B" means "pour the contents of jug A into jug B", and "success" means that the goal has been accomplished.

    You may assume that the input you are given does have a solution.

输入

Input to your program consists of a series of input lines each defining one puzzle. Input for each puzzle is a single line of three positive integers: Ca, Cb, and N. Ca and Cb are the capacities of jugs A and B, and N is the goal. You can assume 0 < Ca <= Cb and N <= Cb <=1000 and that A and B are relatively prime to one another. 

输出

Output from your program will consist of a series of instructions from the list of the potential output lines which will result in either of the jugs containing exactly N gallons of water. The last line of output for each puzzle should be the line "success". Output lines start in column 1 and there should be no empty lines nor any trailing spaces.

样例输入
3 7 1
9 32 6
样例输出
fill B
pour B A
empty A
pour B A
success
fill B
pour B A
empty A
pour B A
empty A
pour B A
empty A
pour B A
fill B
pour B A
empty A
pour B A
empty A
pour B A
empty A
pour B A
empty A
pour B A
fill B
pour B A
empty A
pour B A
empty A
pour B A
success

分析:原题应该是这个:UVA - 571 ,但是原题可以过的代码在Codeup上就是过不了。可能是因为codeup上只有检查到容器B的水为n,才符合要求。

首先应该可以看出来,两个壶的条件下,倒水的过程实际上就是求最大公约数的更相减损术。只要目标值是两个壶容量的最大公约数的倍数,就一定有解。如果不考虑步骤,只要一个壶一直往另一个壶倒水,总能够达到要求。

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define INF 0x3f3f3f3f
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,a) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#a<<"="<<(a)<<endl
#define db5(x,y,z,a,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#a<<"="<<(a)<<", "<<#r<<"="<<(r)<<endl
using namespace std;typedef struct node
{int cnt_a,cnt_b,fat,op;
}node;void print(node a)
{
//    db4(a.cnt_a,a.cnt_b,a.fat,a.op);if(a.op==1)printf("fill A\n");else if(a.op==2)printf("fill B\n");else if(a.op==3)printf("empty A\n");else if(a.op==4)printf("empty B\n");else if(a.op==5)printf("pour A B\n");else if(a.op==6)printf("pour B A\n");return;
}int main(void)
{#ifdef testfreopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);clock_t start=clock();#endif //testint ca,cb,n;while(~scanf("%d%d%d",&ca,&cb,&n)){if(n==0){printf("success\n");continue;}int flag[ca+5][cb+5],l=0,r=1;for(int i=0;i<=ca;++i)for(int j=0;j<=cb;++j)flag[i][j]=0;vector<node>sta;node temp;temp.cnt_a=temp.cnt_b=0,temp.fat=-1,temp.op=0;sta.push_back(temp);flag[0][0]=1;while(l<r){int ll=l,rr=r,f=0,index=-1;for(int i=ll;i<rr;++i){
//                db3(sta[i].cnt_a,sta[i].cnt_b,i);
//                if(flag[sta[i].cnt_a][sta[i].cnt_b]==1)continue;
//                else flag[sta[i].cnt_a][sta[i].cnt_b]=1;if(sta[i].cnt_b==n){f=1;index=i;break;}if(sta[i].cnt_a<ca)//fill A{temp.cnt_a=ca;temp.cnt_b=sta[i].cnt_b;temp.fat=i,temp.op=1;if(!flag[temp.cnt_a][temp.cnt_b])flag[temp.cnt_a][temp.cnt_b]=1,sta.push_back(temp),r++;}if(sta[i].cnt_b<cb)//fill B{temp.cnt_b=cb;temp.cnt_a=sta[i].cnt_a;temp.fat=i,temp.op=2;if(!flag[temp.cnt_a][temp.cnt_b])flag[temp.cnt_a][temp.cnt_b]=1,sta.push_back(temp),r++;}if(sta[i].cnt_a>0)//empty A{temp.cnt_a=0;temp.cnt_b=sta[i].cnt_b;temp.fat=i,temp.op=3;if(!flag[temp.cnt_a][temp.cnt_b])flag[temp.cnt_a][temp.cnt_b]=1,sta.push_back(temp),r++;}if(sta[i].cnt_b>0)//empty B{temp.cnt_b=0;temp.cnt_a=sta[i].cnt_a;temp.fat=i,temp.op=4;if(!flag[temp.cnt_a][temp.cnt_b])flag[temp.cnt_a][temp.cnt_b]=1,sta.push_back(temp),r++;}if(sta[i].cnt_b<cb&&sta[i].cnt_a>0)//pour A B{if(cb-sta[i].cnt_b>=sta[i].cnt_a)temp.cnt_b=sta[i].cnt_b+sta[i].cnt_a,temp.cnt_a=0;else temp.cnt_a=sta[i].cnt_a-cb+sta[i].cnt_b,temp.cnt_b=cb;temp.fat=i,temp.op=5;if(!flag[temp.cnt_a][temp.cnt_b])flag[temp.cnt_a][temp.cnt_b]=1,sta.push_back(temp),r++;}if(sta[i].cnt_b>0&&sta[i].cnt_a<ca)//pour B A{if(ca-sta[i].cnt_a>=sta[i].cnt_b)temp.cnt_a=sta[i].cnt_a+sta[i].cnt_b,temp.cnt_b=0;else temp.cnt_b=sta[i].cnt_b-ca+sta[i].cnt_a,temp.cnt_a=ca;temp.fat=i,temp.op=6;if(!flag[temp.cnt_a][temp.cnt_b])flag[temp.cnt_a][temp.cnt_b]=1,sta.push_back(temp),r++;}}l=rr;
//            db4(l,r,f,index);if(f==1){stack<node>ans;while(index!=-1){
//                    db1(index);ans.push(sta[index]);index=sta[index].fat;}while(!ans.empty()){node cnt=ans.top();ans.pop();print(cnt);}printf("success\n");break;}}
//        db3(ca,cb,n);}#ifdef testclockid_t end=clock();double endtime=(double)(end-start)/CLOCKS_PER_SEC;printf("\n\n\n\n\n");cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位#endif //testreturn 0;
}

http://www.ppmy.cn/embedded/172621.html

相关文章

【每日学点HarmonyOS Next知识】抽屉效果、树状组件、离屏渲染、上下文获取、Tab声明周期

1、HarmonyOS 如何实现抽屉效果的控件&#xff1f; 使用半模态框实现抽屉效果参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-sheet-transition-V5#%E7%A4%BA%E4%BE%8B // xxx.ets Entry Component struct SheetTr…

关于Linux contOS 7 的防火墙

centos7 通过firewall-cmd命令添加防火墙白名单 。 查看防护墙状态 firewall-cmd --state 或 systemctl status firewalld active (running)-->表示防火墙已经开启&#xff1b;inactive (dead)-->表示防火墙已经关闭 开关防火墙命令 启动防火墙&#xff1a;systemctl …

当量子计算遇上互联网安全:挑战与革新之路

当量子计算遇上互联网安全&#xff1a;挑战与革新之路 量子计算&#xff0c;一个被誉为下一次科技革命的前沿技术&#xff0c;正在以惊人的速度发展。这项技术以其超越经典计算机的计算能力&#xff0c;为科学、医药和物流等领域带来了颠覆性变革。然而&#xff0c;对于互联网…

Blackbox.Ai体验:AI编程插件如何提升开发效率

文章目录 一、引言二、特色功能2.1 VSCode插件安装2.2 自动化网页生成功能2.3 自动化测试2.4 MCP服务器 三、编程功能评测3.1 测试一&#xff1a;代码生成3.2 测试二&#xff1a;代码翻译3.3 测试三&#xff1a;代码审查 四、总结 一、引言 最近&#xff0c;AI的热潮已经席卷各…

OpenBMC:BmcWeb 读取http请求头

OpenBMC:BmcWeb connect读取http请求-CSDN博客 如果是http请求或者处理过握手后的https请求,那么接下来就要调用doReadHeaders 1.doReadHeaders void doReadHeaders() {BMCWEB_LOG_DEBUG("{} doReadHeaders", logPtr(this));if (!parser){BMCWEB_LOG_CRITICAL(&qu…

[论文阅读]Trustworthiness in Retrieval-Augmented Generation Systems: A Survey

Trustworthiness in Retrieval-Augmented Generation Systems: A Survey [2409.10102] Trustworthiness in Retrieval-Augmented Generation Systems: A Survey 总览 提出了一个统一的框架&#xff0c;该框架从六个关键维度评估 RAG 系统的可信度&#xff1a;事实性、鲁棒性…

物理服务器抵御网络攻击的方法都有哪些?

网络攻击是各个企业都会面临的网络安全威胁&#xff0c;其中DDOS攻击是最为常见的一种网络攻击类型&#xff0c;物理服务器作为企业基础设施的重要组成部分&#xff0c;在面对分布式拒绝服务攻击&#xff0c;都有哪些抵御方法呢&#xff1f; 高防IP服务是物理服务器防御分布式拒…

PostgreSQL在Linux环境下的常用命令总结

标题 登录PgSQL库表基本操作命令新建库表修改库表 修改数据库名称&#xff1a;修改表名称修改表字段信息 删除库表 pgsql删除正在使用的数据库 须知&#xff1a; 以下所有命令我都在Linux环境中执行验证过&#xff0c;大家放心食用&#xff0c;其中的实际名称换成自己的实际…