(POJ3414)Pots BFS, 记录路径

2024-02-05 02:32
文章标签 路径 记录 bfs pots poj3414

本文主要是介绍(POJ3414)Pots BFS, 记录路径,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Pots
Description

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)

Source

Northeastern Europe 2002, Western Subregion

题意:
有两个锅1和2,容量分别为A,B,开始时都是空的。你可以做以下操作:
装满1或2
倒空1或2
将1倒进2(可能有剩余),将2倒进1
问最少多少步,可以使1或2中的容量为C

分析:
是一道BFS的题,不过要记录路径,所有这样定义状态:
struct node
{
int id,pre;//编号,上一个状态的编号
int a,b,step;
int op;//由上一状态到达这一状态操作的种类
}nodes[10010];
然后找到结果后输出即可。

没有1AC,将drop1写错了,当时样例过了。。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;int A,B,C;
struct node
{int id,pre;int a,b,step;int op;
}nodes[10010];bool v[110][110];
int ans[10010];void output(int x)
{int num=0;for(int i=nodes[x].id;i!=-1;i=nodes[i].pre){//cout<<nodes[i].a<<" "<<nodes[i].b<<nodes[i].op<<endl;ans[num++] = nodes[i].op;}num-=2;for(int i=num;i>=0;i--){if(ans[i]==1) printf("FILL(2)\n");else if(ans[i]==2) printf("FILL(1)\n");else if(ans[i]==3) printf("DROP(1)\n");else if(ans[i]==4) printf("DROP(2)\n");else if(ans[i]==5) printf("POUR(1,2)\n");else if(ans[i]==6) printf("POUR(2,1)\n");}
}int main()
{while(scanf("%d%d%d",&A,&B,&C)!=EOF){int num=0;bool f = false;node now;nodes[0].a=0; nodes[0].b=0; nodes[0].step=0;nodes[0].pre=-1; nodes[0].id=0; nodes[0].op=-1;num++;memset(v,0,sizeof(v));queue<node> q;while(!q.empty()) q.pop();v[0][0]=1;q.push(nodes[0]);while(!q.empty()){now = q.front(); q.pop();//cout<<now.a<<" "<<now.b<<" "<<now.step<<endl;if(now.a==C || now.b==C){f = true;printf("%d\n",now.step);output(now.id);break;}//full 2nodes[num].a = now.a; nodes[num].b = B;if(!v[nodes[num].a][nodes[num].b]){nodes[num].id = num;nodes[num].pre = now.id;nodes[num].step = now.step + 1;nodes[num].op = 1;q.push(nodes[num]);v[nodes[num].a][nodes[num].b]=1;num++;}//full 1nodes[num].a = A; nodes[num].b = now.b;if(!v[nodes[num].a][nodes[num].b]){nodes[num].id = num;nodes[num].pre = now.id;nodes[num].step = now.step + 1;nodes[num].op = 2;q.push(nodes[num]);v[nodes[num].a][nodes[num].b]=1;num++;}//drop 1nodes[num].a = 0; nodes[num].b = now.b;if(!v[nodes[num].a][nodes[num].b]){nodes[num].id = num;nodes[num].pre = now.id;nodes[num].step = now.step + 1;nodes[num].op = 3;q.push(nodes[num]);v[nodes[num].a][nodes[num].b]=1;num++;}//drop 2nodes[num].a = now.a; nodes[num].b = 0;if(!v[nodes[num].a][nodes[num].b]){nodes[num].id = num;nodes[num].pre = now.id;nodes[num].step = now.step + 1;nodes[num].op = 4;q.push(nodes[num]);v[nodes[num].a][nodes[num].b]=1;num++;}// pour 1 2int tmp = B - now.b;if(now.a <= tmp){nodes[num].a = 0;nodes[num].b = now.b + now.a;}else{nodes[num].b = B;nodes[num].a = now.a - tmp;}if(!v[nodes[num].a][nodes[num].b]){nodes[num].id = num;nodes[num].pre = now.id;nodes[num].step = now.step + 1;nodes[num].op = 5;q.push(nodes[num]);v[nodes[num].a][nodes[num].b]=1;num++;}//pour 2 1tmp = A - now.a;if(now.b <= tmp){nodes[num].b = 0;nodes[num].a = now.a + now.b;}else{nodes[num].a = A;nodes[num].b = now.b - tmp;}if(!v[nodes[num].a][nodes[num].b]){nodes[num].id = num;nodes[num].pre = now.id;nodes[num].step = now.step + 1;nodes[num].op = 6;q.push(nodes[num]);v[nodes[num].a][nodes[num].b]=1;num++;}}if(!f) printf("impossible\n");}return 0;
}

这篇关于(POJ3414)Pots BFS, 记录路径的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/679513

相关文章

Spring Retry 实现乐观锁重试实践记录

《SpringRetry实现乐观锁重试实践记录》本文介绍了在秒杀商品SKU表中使用乐观锁和MybatisPlus配置乐观锁的方法,并分析了测试环境和生产环境的隔离级别对乐观锁的影响,通过简单验证,... 目录一、场景分析 二、简单验证 2.1、可重复读 2.2、读已提交 三、最佳实践 3.1、配置重试模板

在 Spring Boot 中使用异步线程时的 HttpServletRequest 复用问题记录

《在SpringBoot中使用异步线程时的HttpServletRequest复用问题记录》文章讨论了在SpringBoot中使用异步线程时,由于HttpServletRequest复用导致... 目录一、问题描述:异步线程操作导致请求复用时 Cookie 解析失败1. 场景背景2. 问题根源二、问题详细分

MySQL9.0默认路径安装下重置root密码

《MySQL9.0默认路径安装下重置root密码》本文主要介绍了MySQL9.0默认路径安装下重置root密码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录问题描述环境描述解决方法正常模式下修改密码报错原因问题描述mysqlChina编程采用默认安装路径,

关于Spring @Bean 相同加载顺序不同结果不同的问题记录

《关于Spring@Bean相同加载顺序不同结果不同的问题记录》本文主要探讨了在Spring5.1.3.RELEASE版本下,当有两个全注解类定义相同类型的Bean时,由于加载顺序不同,最终生成的... 目录问题说明测试输出1测试输出2@Bean注解的BeanDefiChina编程nition加入时机总结问题说明

将sqlserver数据迁移到mysql的详细步骤记录

《将sqlserver数据迁移到mysql的详细步骤记录》:本文主要介绍将SQLServer数据迁移到MySQL的步骤,包括导出数据、转换数据格式和导入数据,通过示例和工具说明,帮助大家顺利完成... 目录前言一、导出SQL Server 数据二、转换数据格式为mysql兼容格式三、导入数据到MySQL数据

关于rpc长连接与短连接的思考记录

《关于rpc长连接与短连接的思考记录》文章总结了RPC项目中长连接和短连接的处理方式,包括RPC和HTTP的长连接与短连接的区别、TCP的保活机制、客户端与服务器的连接模式及其利弊分析,文章强调了在实... 目录rpc项目中的长连接与短连接的思考什么是rpc项目中的长连接和短连接与tcp和http的长连接短

Oracle查询优化之高效实现仅查询前10条记录的方法与实践

《Oracle查询优化之高效实现仅查询前10条记录的方法与实践》:本文主要介绍Oracle查询优化之高效实现仅查询前10条记录的相关资料,包括使用ROWNUM、ROW_NUMBER()函数、FET... 目录1. 使用 ROWNUM 查询2. 使用 ROW_NUMBER() 函数3. 使用 FETCH FI

Python MySQL如何通过Binlog获取变更记录恢复数据

《PythonMySQL如何通过Binlog获取变更记录恢复数据》本文介绍了如何使用Python和pymysqlreplication库通过MySQL的二进制日志(Binlog)获取数据库的变更记录... 目录python mysql通过Binlog获取变更记录恢复数据1.安装pymysqlreplicat

Servlet中配置和使用过滤器的步骤记录

《Servlet中配置和使用过滤器的步骤记录》:本文主要介绍在Servlet中配置和使用过滤器的方法,包括创建过滤器类、配置过滤器以及在Web应用中使用过滤器等步骤,文中通过代码介绍的非常详细,需... 目录创建过滤器类配置过滤器使用过滤器总结在Servlet中配置和使用过滤器主要包括创建过滤器类、配置过滤

python获取当前文件和目录路径的方法详解

《python获取当前文件和目录路径的方法详解》:本文主要介绍Python中获取当前文件路径和目录的方法,包括使用__file__关键字、os.path.abspath、os.path.realp... 目录1、获取当前文件路径2、获取当前文件所在目录3、os.path.abspath和os.path.re