迷宫问题

简介: 题目连接:http://poj.org/problem?id=3414http://bailian.openjudge.cn/practice/3151/总时间限制: 1000ms 内存限制: 65536kB描述You are given two pots, having the volume of A and B liters respectively.
题目连接:
http://poj.org/problem?id=3414
http://bailian.openjudge.cn/practice/3151/
总时间限制: 1000ms 内存限制: 65536kB
描述

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

  1. FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;
  2. DROP(i)      empty the pot i to the drain;
  3. 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.

输入

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).

输出

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’.

样例输入
3 5 4
样例输出
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)

题目大意与题解:http://blog.csdn.net/freedomcheng/article/details/7385666

给出两个杯子容积,初始都为空杯子,给出目标水量,可以执行一些操作,分别是倒空一个杯子,倒满一个杯子,和将一个杯子的水倒到另一个中,问得到目标水量要进行至少多少次以及每次都是什么
FILL(1)表示倒满1杯子,POUR(2,1)表示将2杯子里的水倒进1杯子中,DROP(1)表示倒空1杯子。


本体为广度优先生成树,每次对六种操作进行广度搜索,用二维数组进行状态是否出现过的标记,并记录每次出现的节点的父节点,最后用递归进行输出。

POJ的题判与百炼的题判稍微不一样,下面是POJ的代码。

  1 #include<iostream>  
  2 #include<cstdio>  
  3 #include<cstring>  
  4 using namespace std;  
  5   
  6 int a,b,c;  
  7   
  8   
  9 struct node  
 10 {  
 11     int aa;//a杯子里的水量  
 12     int bb;//b杯子里的水量  
 13     int parent;//父节点编号  
 14     int ope;//执行什么操作得到该节点  
 15     int level;//层次  
 16     node(int x,int y,int p,int o,int l):aa(x),bb(y),parent(p),ope(o),level(l){}//构造函数,为了方便返回该结构体变量  
 17     node(){}//有自定义构造函数的时候必须有一个默认构造函数  
 18 };  
 19   
 20   
 21 node nod[1000000];  
 22 bool fl[205][205];  
 23 int stk[1000000];  
 24   
 25   
 26 void output(int p)  
 27 {  
 28     int top=0;  
 29     stk[0]=p;//数组模拟栈实现递归  
 30     while(nod[stk[top]].parent!=-1)  
 31     {  
 32         top++;  
 33         stk[top]=nod[stk[top-1]].parent;  
 34     }  
 35     for(int i=top;i>=0;i--)  
 36     {  
 37         switch(nod[stk[i]].ope)  
 38         {  
 39             case 0:  
 40             {  
 41                 printf("DROP(1)\n");  
 42             }  
 43             break;  
 44             case 1:  
 45             {  
 46                 printf("FILL(1)\n");  
 47             }  
 48             break;  
 49             case 2:  
 50             {  
 51                 printf("DROP(2)\n");  
 52             }  
 53             break;  
 54             case 3:  
 55             {  
 56                 printf("FILL(2)\n");  
 57             }  
 58             break;  
 59             case 4:  
 60             {  
 61                 printf("POUR(1,2)\n");  
 62             }  
 63             break;  
 64             case 5:  
 65             {  
 66                 printf("POUR(2,1)\n");  
 67             }  
 68             break;  
 69         }  
 70     }  
 71 }  
 72   
 73   
 74 void f1(node& n)  
 75 {  
 76     n.aa=0;  
 77 }  
 78 void f2(node& n)  
 79 {  
 80     n.aa=a;  
 81 }  
 82 void f3(node& n)  
 83 {  
 84     n.bb=0;  
 85 }  
 86 void f4(node&n)  
 87 {  
 88     n.bb=b;  
 89 }  
 90 void f5(node&n)  
 91 {  
 92     if(b-n.bb<n.aa)  
 93     {  
 94         n.aa=n.aa-(b-n.bb);  
 95         n.bb=b;  
 96     }  
 97     else  
 98     {  
 99         n.bb+=n.aa;  
100         n.aa=0;  
101     }  
102 }  
103 void f6(node&n)  
104 {  
105     if(a-n.aa<n.bb)  
106     {  
107         n.bb=n.bb-(a-n.aa);  
108         n.aa=a;  
109     }  
110     else  
111     {  
112         n.aa+=n.bb;  
113         n.bb=0;  
114     }  
115 }  
116 void(*fun[6])(node&n)={f1,f2,f3,f4,f5,f6};//函数指针数组,保存六个函数  
117   
118   
119 int main()  
120 {  
121     while(~scanf("%d%d%d",&a,&b,&c))  
122     {  
123         memset(fl,0,sizeof(fl));//标记数组,表示一种状态是否出现过  
124         int front=0;  
125         int tail=0;  
126         nod[tail++]=node(0,0,-1,-1,0);//构造函数的用处  
127         fl[0][0]=1;  
128         int res;  
129         int res_loc=-1;  
130         while(front<tail)  
131         {  
132             node nd=nod[front++];  
133             if(nd.aa==c||nd.bb==c)  
134             {  
135                 res=nd.level;//res记录得到结果时候的深度  
136                 res_loc=front-1;//表示得到的结果在数组中是第几个  
137                 break;  
138             }  
139             for(int i=0;i<6;i++)//进行六种操作,每次调用函数数组中的一个函数  
140             {  
141                 node nt=nd;  
142                 fun[i](nt);//调用第i个函数  
143                 if(fl[nt.aa][nt.bb]==0)  
144                 {  
145                     fl[nt.aa][nt.bb]=1;  
146                     nod[tail++]=node(nt.aa,nt.bb,front-1,i,nt.level+1);//依然是构造函数的应用  
147                 }  
148             }  
149         }  
150         if(res_loc==-1)  
151         {  
152             printf("impossible\n");  
153             continue;  
154         }  
155         printf("%d\n",res);  
156         output(res_loc);  
157     }  
158     return 0;  
159 }  
View Code

 

相关文章
|
人工智能 定位技术 知识图谱
Geo专家于磊老师:Geo优化过程中必须关注的12个核心指标深度解读
本文深入解析生成式AI时代下的Geo优化新范式,提出以权威性、可信度和用户意图匹配为核心的12大指标体系。结合于磊老师“两大核心+四轮驱动”方法论,通过金融、医疗、教育等实战案例,展现如何重构内容价值,实现获客效率提升与品牌数字资产沉淀。
794 1
|
9月前
|
人工智能 监控 安全
“养龙虾”OpenClaw保姆级教程:全平台部署(阿里云+本地)多模型免费API配置+安全防护+避坑指南
GitHub 26万星、4个月刷屏全球、人人争当“龙虾养殖户”(OpenClaw用户昵称)——2026年开春,这款开源AI智能体以“能说会做”的特质爆火出圈。它不像传统AI只停留在聊天层面,而是能直接接管电脑:整理收件箱、归档文件、汇总报表、监控服务器,甚至读PDF、总结文献,成为无数人心中的“数字代理人”。
2570 3
|
存储 缓存 开发者
Mooncake 最新进展:SGLang 和 LMCache 基于 Mooncake 实现高效 PD 分离框架
近期,Mooncake 项目与 SGLang、vLLM 等主流大模型推理框架实现合作和适配,这些开源大模型推理框架可以通过使用 Mooncake 发布的 whl 包,支持 pip安装,docker 镜像部署等,实现了 PD 分离框架,极大提升了模型推理效率。
|
安全 Linux 网络安全
Centos7安装Docker
安装docker Centos7安装Docker 1.卸载Docker 2.安装docker 3.启动Docker CentOS7安装DockerCompose 1.下载 2.修改文件权限 3.Base自动补全命令:
6837 0
|
敏捷开发 数据可视化 开发工具
如何使用分支管理代码更改?
如何使用分支管理代码更改?
975 57
|
算法 JavaScript 前端开发
在JavaScript中实现基本的碰撞检测算法,我们通常会用到矩形碰撞检测,也就是AABB(Axis-Aligned Bounding Box)碰撞检测
【6月更文挑战第16天】JavaScript中的基本碰撞检测涉及AABB(轴对齐边界框)方法,常用于2D游戏。`Rectangle`类定义了矩形的属性,并包含一个`collidesWith`方法,通过比较边界来检测碰撞。若两矩形无重叠部分,四个条件(关于边界相对位置)均需满足。此基础算法适用于简单场景,复杂情况可能需采用更高级的检测技术或物理引擎库。
758 6
|
Web App开发 SQL 前端开发
前端页面加载性能指标之LCP
本文介绍了 Largest Contentful Paint (LCP),一种衡量网页加载性能的指标,专注于视口内最大图片或文本块的完全渲染时间,旨在提升用户对主要内容加载速度的感知。文章还探讨了LCP的测量方法和优化策略,如图像优化、懒加载等,以帮助改善网页性能。
2581 5
|
机器学习/深度学习 分布式计算 数据可视化
Python在数据科学中的应用与挑战
本文探讨了Python编程语言在数据科学领域的广泛应用及其面临的主要挑战。Python因其简洁的语法、强大的库支持和活跃的社区,已成为数据科学家的首选工具。然而,随着数据量的激增和复杂性的增加,Python也面临着性能瓶颈、内存管理等问题。本文将通过具体案例分析,展示Python在数据处理、分析和可视化方面的优势,同时讨论如何克服其在大规模数据处理中的局限性,为读者提供实用的解决方案和优化建议。
|
人工智能 安全 Linux
龙蜥社区首推 AI 原生操作系统路线,三大重磅计划协同生态布局未来
第二届龙蜥大会圆满落幕,龙蜥操作系统装机量突破800万。
|
缓存 网络协议 前端开发
【最佳实践】CDN访问慢的分析思路和优化方案
使用CDN加速以后还是存在访问慢的情况,如何去分析定位问题、优化网站速度、解决用户问题是一个十分重要的课题。本文介绍了CDN加速访问慢的分析思路,通过归纳的一些原因结合搜集的信息去进一步判断定位问题,帮助用户在遇到问题时有一个更清晰的思考方法论。
3215 0

热门文章

最新文章