博客
关于我
POJ - 3984 迷宫问题(bfs+路径标记)
阅读量:274 次
发布时间:2019-03-01

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

编写一个程序来找出5x5迷宫中的最短路径。迷宫由二维数组表示,1是墙壁,0是可以走的路。目标是从左上角(0,0)到右下角(4,4),并输出最短路径。

步骤说明

  • 读取输入:将5x5的迷宫数据读入二维数组。
  • 初始化数据结构
    • 访问标记数组vis,记录已访问的点。
    • 路径存储数组path,记录每个点的前驱坐标。
  • 广度优先搜索(BFS)
    • 使用队列处理点。
    • 从起点(0,0)开始,扩展四个方向。
    • 记录路径和步数,直到找到终点(4,4)。
  • 输出路径:从终点反向遍历路径数组,输出所有点的坐标。
  • 代码实现

    #include 
    #include
    #include
    using namespace std;struct Node { int x, y; int pre_x, pre_y; int step;};int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};void bfs(int a[5][5], bool vis[5][5], Node path[50], int& steps) { queue
    q; Node start = {0, 0, -1, -1, 0}; q.push(start); vis[start.x][start.y] = true; steps = 0; while (!q.empty()) { Node current = q.front(); q.pop(); steps++; if (current.x == 4 && current.y == 4) break; for (int i = 0; i < 4; ++i) { int new_x = current.x + dir[i][0]; int new_y = current.y + dir[i][1]; if (new_x >= 0 && new_x < 5 && new_y >= 0 && new_y < 5) { if (a[new_x][new_y] == 0 && !vis[new_x][new_y]) { vis[new_x][new_y] = true; Node next = {new_x, new_y, current.x, current.y, steps}; path[steps] = next; q.push(next); if (new_x == 4 && new_y == 4) { steps++; break; } } } } }}int main() { int maze[5][5]; for (int i = 0; i < 5; ++i) { for (int j = 0; j < 5; ++j) { scanf("%d", &maze[i][j]); } } bool vis[5][5] = {{false for _ in range(5)}}; Node path[50]; int steps = 0; bfs(maze, vis, path, steps); if (steps == 0) { cout << "(0,0)" << endl; return 0; } for (int i = steps; i >= 0; --i) { if (path[i].x == 4 && path[i].y == 4) { break; } } for (int i = steps; i >= 0; --i) { if (path[i].x == 4 && path[i].y == 4) { break; } if (i == 0) { continue; } cout << "(" << path[i].x << ", " << path[i].y << ")" << endl; } return 0;}

    输出结果

    样例输入对应的输出路径为:

    (0, 0)(1, 0)(2, 0)(2, 1)(2, 2)(2, 3)(2, 4)(3, 4)(4, 4)

    代码解释

  • 读取输入:使用scanf读取5x5的迷宫数据。
  • 初始化数据结构
    • vis数组记录每个点是否被访问过。
    • path数组存储路径信息,每个节点包含前驱坐标和步数。
  • BFS函数
    • 从起点开始,使用队列处理每个点。
    • 展开四个方向,检查是否在迷宫范围内、是否是可走路和是否未被访问。
    • 记录路径和步数,直到找到终点。
  • 输出路径:从路径数组中提取并反向输出所有点的坐标。
  • 该程序使用广度优先搜索算法,确保找到最短路径,并以指定格式输出。

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

    你可能感兴趣的文章
    Plotly-Dash:如何过滤具有多个数据框列的仪表板?
    查看>>
    Plotly:如何为 x 轴上的时间序列设置主要刻度线/网格线的值?
    查看>>
    Plotly:如何从 x 轴删除空日期?
    查看>>
    Plotly:如何从单条迹线制作堆积条形图?
    查看>>
    Plotly:如何以 Root 样式绘制直方图,仅显示直方图的轮廓?
    查看>>
    Plotly:如何使用 Plotly Express 组合散点图和线图?
    查看>>
    Plotly:如何使用 plotly.graph_objects 和 plotly.express 定义图形中的颜色?
    查看>>
    Plotly:如何使用 Python 对绘图对象条形图进行颜色编码?
    查看>>
    Plotly:如何使用 updatemenus 更新一个特定的跟踪?
    查看>>
    Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?
    查看>>
    Plotly:如何向烛台图添加交易量
    查看>>
    Plotly:如何在 plotly express 中找到趋势线的系数?
    查看>>
    Plotly:如何在桑基图中设置节点位置?
    查看>>
    Plotly:如何处理重叠的颜色条和图例?
    查看>>
    Plotly:如何手动设置 plotly express 散点图中点的颜色?
    查看>>
    Plotly:如何结合 make_subplots() 和 ff.create_distplot()?
    查看>>
    Plotly:如何绘制累积的“步骤“;直方图?
    查看>>
    PLSQL developer12安装图解
    查看>>
    PLSQL window操作
    查看>>
    plsql 存储过程 测试
    查看>>