热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

POJ3414Pots(倒水问题BFS+打印路径)

POJ-3414Pots题目大意就是给你A,B两个杯子,及他们的最大容量,三种操作方法,让你判断最少用多少此方法可以让任

POJ - 3414 Pots


题目大意

就是给你A, B两个杯子,及他们的最大容量,三种操作方法,让你判断最少用多少此方法可以让任意一个被子里装有 C 升水。并打印路径。

分析

这里很容易想到广搜,设初始状态为(i,j),一共就只有六种变化
A杯倒满,B杯倒满,A杯倒出完,B杯倒出完,A到给B,B到给A。
任意一种状态i 或者 j达到 C 就停止,注意要打印路径,我这里用一个string存的操作顺序。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#pragma comment (linker, "/STACK:256000000")
using namespace std;
#define db(x) cout<typedef long long ll;
const int N &#61; 100 &#43; 10;
const int INF &#61; 0x3f3f3f;int flag &#61; 0;
int a, b, c; //A, B, c, 盒子容量
int vis[N][N]; //保存容量状态
string str[10] &#61; {"", "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)"};struct node
{int x, y; //A, B盒子容量int step; //操作数string s; //操作路径node(int x, int y, int step, string s):x(x),y(y),step(step),s(s){}
};void bfs()
{queue<node> q;vis[0][0] &#61; 1;q.push(node(0, 0, 0, "0"));while(!q.empty()){node temp &#61; q.front();q.pop();if(temp.x &#61;&#61; c || temp.y &#61;&#61; c){flag &#61; 1;cout << temp.step << endl;for (int i &#61; 1; i < temp.s.length(); i&#43;&#43;){cout << str[temp.s[i] - &#39;0&#39;] << endl;}return;}if(temp.x < a){ //将第一个瓶子装满if(!vis[a][temp.y]){vis[a][temp.y] &#61; 1;q.push(node(a, temp.y, temp.step &#43; 1, temp.s &#43; "1"));}}if(temp.y < b){ //将第二个瓶子装满if(!vis[temp.x][b]){vis[temp.x][b] &#61; 1;q.push(node(temp.x, b, temp.step &#43; 1, temp.s &#43; "2"));}}if(temp.x !&#61; 0){ //将第一个瓶子倒完if(!vis[0][temp.y]){vis[0][temp.y] &#61; 1;q.push(node(0, temp.y, temp.step &#43; 1, temp.s &#43; "3"));}}if(temp.y !&#61; 0){ //将第二个瓶子装满if(!vis[temp.x][0]){vis[temp.x][0] &#61; 1;q.push(node(temp.x, 0, temp.step &#43; 1, temp.s &#43; "4"));}}if(temp.x !&#61; 0 && temp.y < b){ //将第一个瓶子倒进第二个瓶子int xx, yy;if(temp.x <&#61; b - temp.y){xx &#61; 0;yy &#61; temp.y &#43; temp.x;}else{yy &#61; b;xx &#61; temp.x - b &#43; temp.y;}if(!vis[xx][yy]){vis[xx][yy] &#61; 1;q.push(node(xx, yy, temp.step &#43; 1, temp.s &#43; "5"));}}if(temp.y !&#61; 0 && temp.x < a){ //将第二个瓶子倒进第一个瓶子int xx, yy;if(temp.y <&#61; a - temp.x){yy &#61; 0;xx &#61; temp.x &#43; temp.y;}else{xx &#61; a;yy &#61; temp.y - a &#43; temp.x;}if(!vis[xx][yy]){vis[xx][yy] &#61; 1;q.push(node(xx, yy, temp.step &#43; 1, temp.s &#43; "6"));}}}
}int main()
{cin >> a >> b >> c;bfs();if(!flag)cout << "impossible" << endl;return 0;
}


推荐阅读
  • 题目Link题目学习link1题目学习link2题目学习link3%%%受益匪浅!-----&# ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 本题涉及一棵由N个节点组成的树(共有N-1条边),初始时所有节点均为白色。题目要求处理两种操作:一是改变某个节点的颜色(从白变黑或从黑变白);二是查询从根节点到指定节点路径上的第一个黑色节点,若无则输出-1。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 本文探讨了如何在模运算下高效计算组合数C(n, m),并详细介绍了乘法逆元的应用。通过扩展欧几里得算法求解乘法逆元,从而实现除法取余的计算。 ... [详细]
  • 本文详细探讨了VxWorks操作系统中双向链表和环形缓冲区的实现原理及使用方法,通过具体示例代码加深理解。 ... [详细]
  • 本文探讨了《魔兽世界》中红蓝两方阵营在备战阶段的策略与实现方法,通过代码展示了双方如何根据资源和兵种特性进行战士生产。 ... [详细]
  • 本题探讨了一种字符串变换方法,旨在判断两个给定的字符串是否可以通过特定的字母替换和位置交换操作相互转换。核心在于找到这些变换中的不变量,从而确定转换的可能性。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 本文基于刘洪波老师的《英文词根词缀精讲》,深入探讨了多个重要词根词缀的起源及其相关词汇,帮助读者更好地理解和记忆英语单词。 ... [详细]
  • 本文详细介绍了C语言中链表的两种动态创建方法——头插法和尾插法,包括具体的实现代码和运行示例。通过这些内容,读者可以更好地理解和掌握链表的基本操作。 ... [详细]
author-avatar
手机用户2602926791
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有