(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

相关文章

Python获取中国节假日数据记录入JSON文件

《Python获取中国节假日数据记录入JSON文件》项目系统内置的日历应用为了提升用户体验,特别设置了在调休日期显示“休”的UI图标功能,那么问题是这些调休数据从哪里来呢?我尝试一种更为智能的方法:P... 目录节假日数据获取存入jsON文件节假日数据读取封装完整代码项目系统内置的日历应用为了提升用户体验,

Spring Boot 配置文件之类型、加载顺序与最佳实践记录

《SpringBoot配置文件之类型、加载顺序与最佳实践记录》SpringBoot的配置文件是灵活且强大的工具,通过合理的配置管理,可以让应用开发和部署更加高效,无论是简单的属性配置,还是复杂... 目录Spring Boot 配置文件详解一、Spring Boot 配置文件类型1.1 applicatio

MySQL INSERT语句实现当记录不存在时插入的几种方法

《MySQLINSERT语句实现当记录不存在时插入的几种方法》MySQL的INSERT语句是用于向数据库表中插入新记录的关键命令,下面:本文主要介绍MySQLINSERT语句实现当记录不存在时... 目录使用 INSERT IGNORE使用 ON DUPLICATE KEY UPDATE使用 REPLACE

Python 中的异步与同步深度解析(实践记录)

《Python中的异步与同步深度解析(实践记录)》在Python编程世界里,异步和同步的概念是理解程序执行流程和性能优化的关键,这篇文章将带你深入了解它们的差异,以及阻塞和非阻塞的特性,同时通过实际... 目录python中的异步与同步:深度解析与实践异步与同步的定义异步同步阻塞与非阻塞的概念阻塞非阻塞同步

Python Dash框架在数据可视化仪表板中的应用与实践记录

《PythonDash框架在数据可视化仪表板中的应用与实践记录》Python的PlotlyDash库提供了一种简便且强大的方式来构建和展示互动式数据仪表板,本篇文章将深入探讨如何使用Dash设计一... 目录python Dash框架在数据可视化仪表板中的应用与实践1. 什么是Plotly Dash?1.1

Linux修改pip和conda缓存路径的几种方法

《Linux修改pip和conda缓存路径的几种方法》在Python生态中,pip和conda是两种常见的软件包管理工具,它们在安装、更新和卸载软件包时都会使用缓存来提高效率,适当地修改它们的缓存路径... 目录一、pip 和 conda 的缓存机制1. pip 的缓存机制默认缓存路径2. conda 的缓

Spring Boot中定时任务Cron表达式的终极指南最佳实践记录

《SpringBoot中定时任务Cron表达式的终极指南最佳实践记录》本文详细介绍了SpringBoot中定时任务的实现方法,特别是Cron表达式的使用技巧和高级用法,从基础语法到复杂场景,从快速启... 目录一、Cron表达式基础1.1 Cron表达式结构1.2 核心语法规则二、Spring Boot中定

Windows系统下如何查找JDK的安装路径

《Windows系统下如何查找JDK的安装路径》:本文主要介绍Windows系统下如何查找JDK的安装路径,文中介绍了三种方法,分别是通过命令行检查、使用verbose选项查找jre目录、以及查看... 目录一、确认是否安装了JDK二、查找路径三、另外一种方式如果很久之前安装了JDK,或者在别人的电脑上,想

国内环境搭建私有知识问答库踩坑记录(ollama+deepseek+ragflow)

《国内环境搭建私有知识问答库踩坑记录(ollama+deepseek+ragflow)》本文给大家利用deepseek模型搭建私有知识问答库的详细步骤和遇到的问题及解决办法,感兴趣的朋友一起看看吧... 目录1. 第1步大家在安装完ollama后,需要到系统环境变量中添加两个变量2. 第3步 “在cmd中

Python中Windows和macOS文件路径格式不一致的解决方法

《Python中Windows和macOS文件路径格式不一致的解决方法》在Python中,Windows和macOS的文件路径字符串格式不一致主要体现在路径分隔符上,这种差异可能导致跨平台代码在处理文... 目录方法 1:使用 os.path 模块方法 2:使用 pathlib 模块(推荐)方法 3:统一使