博客
关于我
Oh, my goddess(bfs)
阅读量:626 次
发布时间:2019-03-13

本文共 2414 字,大约阅读时间需要 8 分钟。

Shining Knight的迷宫救援

Shining Knight需要通过一个M x N 的迷宫来拯救被囚禁的Miss Ice。迷宫中用'O'表示空地,用'#'表示墙壁。Shining Knight从起点(1,1)出发,可以向四个方向移动,每移动一次需要1秒。另外,他也可以选择破坏一面墙壁,转化为空地,这种操作需要3秒。

问题描述

Shining Knight的目标是找到Miss Ice的位置,并且在最短时间内到达那里。时间限制是3000 ms,内存限制是65535 KB。请通过代码计算最短时间。

输入格式

输入包括多个部分,每个部分之间有空行:

  • 每行的前两个数字是M和N。
  • 接下来的M行,每行包含一个长度为N的字符串,字符串由'O'和'#'组成。
  • 最后一行给出Miss Ice的位置(x, y),其中1 ≤ x ≤ M,1 ≤ y ≤ N。
  • 输出要求

    输出Miss Ice被救援所需的最短时间。

    样例输入

    3 5O##########O#O#3 4

    样例输出

    14

    解题思路

    这个问题可以通过广度优先搜索(BFS)来解决,但由于墙壁的破坏需要额外时间,我们需要特别注意状态转换。每个状态不仅包括位置,还包括已经花费的时间。

  • 状态表示:每个节点的状态包括位置(x, y)和当前时间step。
  • 移动方式:
    • 移动到相邻空地:step + 1
    • 破坏墙壁:step + 3(如果目标是墙壁)
  • 优先队列:使用优先队列(例如堆)来按时间排序,确保先处理时间较小的状态。
  • 终止条件:当队列中取出节点时,该节点的位置等于目标位置(x, y),则返回当前的step。
  • 代码实现

    #include 
    #include
    #include
    using namespace std;struct Node { int x, y, step; Node(int x_, int y_, int step_) : x(x_), y(y_), step(step_) {}};bool operator<(const Node& a, const Node& b) { return a.step > b.step;}char map[55][55];int vis[55][55];int disx[] = {0, 0, 1, -1};int disy[] = {1, -1, 0, 0};int M, N, ex, ey;void bfs(int sx, int sy) { memset(vis, 0, sizeof(vis)); vis[sx][sy] = 1; priority_queue
    q; Node start(sx, sy, 0); q.push(start); while (!q.empty()) { Node a = q.top(); q.pop(); for (int i = 0; i < 4; ++i) { int x = a.x + disx[i]; int y = a.y + disy[i]; if (x < 1 || y < 1 || x > M || y > N || vis[x][y]) { continue; } if (map[x][y] == 'O') { if (!vis[x][y]) { Node b(x, y, a.step + 1); if (b.x == ex && b.y == ey) { printf("%d\n", b.step); return; } vis[x][y] = 1; q.push(b); } } else { Node b(x, y, a.step + 3); if (!vis[x][y]) { vis[x][y] = 1; q.push(b); } } } }}int main() { while (scanf("%d%d", &M, &N) != EOF) { for (int i = 1; i <= M; ++i) { scanf("%s", map[i] + 1); } scanf("%d%d", &ex, &ey); bfs(1, 1); } return 0;}

    解释

    • 结构体Node:用于存储当前位置和时间步。
    • 优先队列:按时间排序,确保最优解优先被处理。
    • 状态转换:处理移动和破坏墙壁两种操作,分别计算时间。
    • 终止条件:检查是否到达目标位置并输出时间。

    通过这种方法,可以高效地找到Shining Knight到达Miss Ice所需的最短时间。

    转载地址:http://izeaz.baihongyu.com/

    你可能感兴趣的文章
    Python中提示:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position
    查看>>
    python中数据类型存储_浅谈Python中的数据类型
    查看>>
    python中文件最重要的功能是什么-学 Python 都用来干嘛的?
    查看>>
    python中文版语言_高盛最新调查:Python超过汉语成为未来最重要技能,你准备学哪种编程语言?...
    查看>>
    Python中文问题SyntaxError
    查看>>
    Python中方括号和括号括起来的列表有什么区别?
    查看>>
    python中时间处理_Python中处理时间的几种方法小结
    查看>>
    Python中枚举串口(包括虚拟端口)的跨平台方法是什么?
    查看>>
    python中根号下a怎么写_python中根号怎么表示
    查看>>
    Python中正则表达式的巧妙使用
    查看>>
    Python中浮点数的二进制表示(位不是十六进制)
    查看>>
    Python中特殊函数和表达式 filter,map,reduce,lambda
    查看>>
    python中用socket检测端口_Python 用socket模块实现检测端口和检测web服务
    查看>>
    python中用于列表操作的plus和append有什么区别?
    查看>>
    python中用递归求和
    查看>>
    python中的@staticmethod
    查看>>
    python中的array函数作用_NumPy 数组使用大全
    查看>>
    python中的isinstance判断多个对象类型
    查看>>
    python中的keys、values、items
    查看>>
    Python中的lambda和apply结合使用
    查看>>