POJ3414 Pots( BFS搜索)
题目:
给你两个容器,分别能装下A升水和B升水,并且可以进行以下操作
FILL(i) 将第i个容器从水龙头里装满(1 ≤ i ≤ 2);
DROP(i) 将第i个容器抽干
POUR(i,j) 将第i个容器里的水倒入第j个容器(这次操作结束后产生两种结果,一是第j个容器倒满并且第i个容器依旧有剩余,二是第i个容器里的水全部倒入j中,第i个容器为空)
现在要求你写一个程序,来找出能使其中任何一个容器里的水恰好有C升,找出最少操作数并给出操作过程
输入:
有且只有一行,包含3个数A,B,C(1<=A,B<=100,C<=max(A,B))
输出:
第一行包含一个数表示最小操作数K
随后K行每行给出一次具体操作,如果有多种答案符合最小操作数,输出他们中的任意一种操作过程,如果你不能使两个容器中的任意一个满足恰好C升的话,输出“impossible”
分析:
简单的BFS,难点在于回溯,给每个状态用数组记录路径
代码
#include<iostream>
#include<queue>
#include<string>
using namespace std;
const int INF = 0x3f3f3f3f;
int a, b, c;
int used[105][105];
struct node
{
int x, y;
int flag;
int path[1005];//数组中0-5分别表示6种不同操作
}st;
string print[6] = { "FILL(1)", "FILL(2)", "DROP(1)", "DROP(2)", "POUR(1,2)", "POUR(2,1)" };
void bfs()
{
queue<node> q;
for (int i = 0; i <= a; ++i)
{
for (int j = 0; j <= b; ++j)
used[i][j] = INF;
}
memset(used, 0, sizeof(used));
st.x = 0, st.y = 0;
st.flag = 0;
memset(st.path, -1, sizeof(st.path));
q.push(st);
used[st.x][st.y] = 1;
while (q.size())
{
node temp = q.front();
q.pop();
if (temp.x == c || temp.y == c)
{
cout << temp.flag << endl;
for (int i = 0; i < temp.flag; ++i)
cout << print[temp.path[i]] << endl;
return;
}
for (int i = 0; i < 6; ++i)
{
node now = temp;
now.flag++;
if (i == 0 && now.x != a)
{
now.x = a;
if (!used[now.x][now.y])
{
used[now.x][now.y] = 1;
now.path[temp.flag] = 0;
q.push(now);
}
}
else if (i == 1 && now.y != b)
{
now.y = b;
if (!used[now.x][now.y])
{
used[now.x][now.y] = 1;
now.path[temp.flag] = 1;
q.push(now);
}
}
else if (i == 2 && now.x != 0)
{
now.x = 0;
if (!used[now.x][now.y])
{
used[now.x][now.y] = 1;
now.path[temp.flag] = 2;
q.push(now);
}
}
else if (i == 3 && now.y != 0)
{
now.y = 0;
if (!used[now.x][now.y])
{
used[now.x][now.y] = 1;
now.path[temp.flag] = 3;
q.push(now);
}
}
else if (i == 4)
{
if (now.x + now.y > b)
{
now.x -= b - now.y;
now.y = b;
}
else
{
now.y += now.x;
now.x = 0;
}
if (!used[now.x][now.y])
{
used[now.x][now.y] = 1;
now.path[temp.flag] = 4;
q.push(now);
}
}
else if (i == 5)
{
if (now.x + now.y > a)
{
now.y -= a - now.x;
now.x = a;
}
else
{
now.x += now.y;
now.y = 0;
}
if (!used[now.x][now.y])
{
used[now.x][now.y] = 1;
now.path[temp.flag] = 5;
q.push(now);
}
}
}
}
cout << "impossible" << endl;
}
int main()
{
cin >> a >> b >> c;
bfs();
return 0;
}
POJ3414 Pots( BFS搜索)的更多相关文章
- POJ3414—Pots(bfs加回溯)
http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memor ...
- POJ3414 Pots —— BFS + 模拟
题目链接:http://poj.org/problem?id=3414 Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ-3414.Pots.(BFS + 路径打印)
这道题做了很长时间,一开始上课的时候手写代码,所以想到了很多细节,但是创客手打代码的时候由于疏忽又未将pair赋初值,导致一直输出错误,以后自己写代码可以专心一点,可能会在宿舍图书馆或者Myhome, ...
- POJ3414 Pots BFS搜素
题意:通过题目给出的三种操作,让任意一个杯子中的水到达一定量 分析:两个杯子最大容量是100,所以开个100*100的数组记录状态,最多1w个状态,所以复杂度很低,然后记录一下路径就好 注:代码写残了 ...
- POJ 3414 Pots (BFS/DFS)
Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7783 Accepted: 3261 Special Ju ...
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hiho_1139_二分+bfs搜索
题目 给定N个点和M条边,从点1出发,到达点T.寻找路径上边的个数小于等于K的路径,求出所有满足条件的路径中最长边长度的最小值. 题目链接:二分 最小化最大值,考虑采用二分搜索.对所有的边长进 ...
- hdu--1026--Ignatius and the Princess I(bfs搜索+dfs(打印路径))
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- BFS搜索
参考博客:[算法入门]广度/宽度优先搜索(BFS) 适用问题:一个解/最优解 重点:我们怎么运用队列?怎么记录路径? 假设我们要找寻一条从V0到V6的最短路径.(明显看出这条最短路径就是V0-> ...
- Horse Pro(带负坐标的bfs搜索)
Horse Pro bfs搜索,但图中存在负值坐标,两种方法解决. 用数组标记,将原点设为300,300 用map标记 http://oj.jxust.edu.cn/contest/Problem?i ...
随机推荐
- 【GIT】学习day01 | 内嵌git安装教程【外包杯】
Git是一个开源的分布式版本控制系统,可以有效.高速地处理从很小到非常大的项目版本管理 第一步:下载Git 下载地址https://git-scm.com/downloads 如果出现下面这种情况无法 ...
- 关于fstream对象的open方法报错183的问题
当使用fstream,ifstream,ofstream,这几种对象打开文件, 但文件已经存在的时候, 调用GetLastError()函数,会返回错误代码183, 这个代码代表该文件已经存在,是正常 ...
- C#中HttpWebQuest发起HTTP请求,如何设置才能达到最大并发和性能
在C#中使用HttpWebRequest发起HTTP请求时,达到最大并发和性能可以从以下几个方面改进: 1. ServicePointManager设置 ServicePointManager 类是一 ...
- 快速认识,前端必学编程语言:JavaScript
JavaScript是构建Web应用必学的一门编程语言,也是最受开发者欢迎的热门语言之一.所以,如果您还不知道JavaScript的用处.特点的话,赶紧补充一下这块基础知识. JavaScript 是 ...
- 三种方式查询三级分类Tree
话不多说,直接上代码 方式一:for循环嵌套一下 /** * 查询三级分类 * * @return */ @Override public List<GoodsType> findNode ...
- 使用SPEL自定义表达式
自定义表达式 Spring提供了一个可以自定义表达式的接口 package com.qbb.qmall.item; import org.junit.Test; import org.springfr ...
- SpringCloudGateway解决跨域问题
1.跨域问题详情 2.为什么会跨域? 官方定义:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/CORS 怎么出现的? 1.浏览器访问了一个业务 h ...
- 深入 K8s 网络原理(一)- Flannel VXLAN 模式分析
目录 1. 概述 2. TL;DR 3. Pod 间通信问题的由来 4. 测试环境准备 5. 从 veth 设备聊起 6. 网桥 cni0 6.1 在 Pod 内看网卡信息 6.2 在 host 上看 ...
- 使用汇编和反汇编引擎写一个x86任意地址hook
最简单的Hook 刚开始学的时候,用的hook都是最基础的5字节hook,也不会使用hook框架,hook流程如下: 构建一个jmp指令跳转到你的函数(函数需定义为裸函数) 保存被hook地址的至少5 ...
- 深入理解 Docker 核心原理:Namespace、Cgroups 和 Rootfs
通过这篇文章你可以了解到 Docker 容器的核心实现原理,包括 Namespace.Cgroups.Rootfs 等三个核心功能. 如果你对云原生技术充满好奇,想要深入了解更多相关的文章和资讯,欢迎 ...