博客
关于我
每日一题-bfs最短路径变式
阅读量:320 次
发布时间:2019-03-04

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

为了解决这个问题,我们需要找到从迷宫入口到算法书遗落位置的最短路径,同时避免经过无法进入的房间、墨菲斯托的房间和莉莉丝的房间。我们可以使用广度优先搜索(BFS)来解决这个问题,并考虑两种情况:路径中不经过任何F或M房间,或者必须经过一个F或M房间。

方法思路

  • 情况一:计算从入口(1,1)到目标位置(r,c)的最短路径,且路径中不经过任何F或M房间。
  • 情况二:计算从入口(1,1)到每个F房间的最短路径,然后从每个F房间到目标位置的最短路径,取最小值。
  • 情况三:计算从入口(1,1)到每个M房间的最短路径,然后从每个M房间到目标位置的最短路径,取最小值。
  • 最后,比较这三种情况的最小值,返回最小值;如果无法找到路径,返回"IMPOSSIBLE"。

    解决代码

    #include 
    using namespace std;const int INF = 1e9;const int maxn = 1000 + 5;char g[maxn][maxn];int dx[] = {0, 1, 0, -1};int dy[] = {1, 0, -1, 0};int bfs(int x0, int y0, int x1, int y1, char forbidden) { if (x0 == x1 && y0 == y1) return 0; vector
    > dist(maxn, vector
    (maxn, INF)); dist[x0][y0] = 0; queue
    > q; q.push({x0, y0}); while (!q.empty()) { auto [x, y] = q.front(); q.pop(); for (int i = 0; i < 4; ++i) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx < 1 || nx > maxn || ny < 1 || ny > maxn) continue; if (g[nx][ny] == forbidden) continue; if (dist[nx][ny] == INF) { dist[nx][ny] = dist[x][y] + 1; q.push({nx, ny}); if (nx == x1 && ny == y1) { return dist[nx][ny]; } } } } return dist[x1][y1];}int main() { int t; cin >> t; for (int _t = 0; _t < t; ++_t) { int n, m, r, c; cin >> n >> m >> r >> c; g = {{0}}; for (int i = 1; i <= n; ++i) { string s; cin >> s; for (int j = 1; j <= m; ++j) { g[i][j] = s[j-1]; } } int ans1 = bfs(1, 1, r, c, 'F'); int ans2 = INF; for (int x = 1; x <= n; ++x) { for (int y = 1; y <= m; ++y) { if (g[x][y] == 'F') { int d1 = bfs(1, 1, x, y, 'M'); int d2 = bfs(x, y, r, c, 'F'); if (d1 != INF && d2 != INF) { int total = d1 + d2; if (total < ans2) { ans2 = total; } } } } } int ans3 = INF; for (int x = 1; x <= n; ++x) { for (int y = 1; y <= m; ++y) { if (g[x][y] == 'M') { int d1 = bfs(1, 1, x, y, 'F'); int d2 = bfs(x, y, r, c, 'F'); if (d1 != INF && d2 != INF) { int total = d1 + d2; if (total < ans3) { ans3 = total; } } } } } int ans = min(ans1, ans2, ans3); if (ans == INF) { cout << "IMPOSSIBLE" << endl; } else { cout << ans << endl; } }}

    代码解释

  • BFS函数:用于计算从起点到终点的最短路径,避开特定类型的房间。
  • 主函数:读取输入数据,调用BFS函数计算三种情况的最短路径,并输出结果。
  • 情况一:计算不经过F或M房间的最短路径。
  • 情况二:计算经过F房间的最短路径。
  • 情况三:计算经过M房间的最短路径。
  • 结果判断:比较三种情况的结果,输出最小值或"IMPOSSIBLE"。
  • 转载地址:http://unrq.baihongyu.com/

    你可能感兴趣的文章
    NOIP2014 提高组 Day2——寻找道路
    查看>>
    noip借教室 题解
    查看>>
    NOIP模拟测试19
    查看>>
    NOIp模拟赛二十九
    查看>>
    Vue3+element plus+sortablejs实现table列表拖拽
    查看>>
    Nokia5233手机和我装的几个symbian V5手机软件
    查看>>
    non linear processor
    查看>>
    Non-final field ‘code‘ in enum StateEnum‘
    查看>>
    none 和 host 网络的适用场景 - 每天5分钟玩转 Docker 容器技术(31)
    查看>>
    None还可以是函数定义可选参数的一个默认值,设置成默认值时实参在调用该函数时可以不输入与None绑定的元素...
    查看>>
    NoNodeAvailableException None of the configured nodes are available异常
    查看>>
    Vue.js 学习总结(16)—— 为什么 :deep、/deep/、>>> 样式能穿透到子组件
    查看>>
    nopcommerce商城系统--文档整理
    查看>>
    NOPI读取Excel
    查看>>
    NoSQL&MongoDB
    查看>>
    NoSQL介绍
    查看>>
    NoSQL数据库概述
    查看>>
    Notadd —— 基于 nest.js 的微服务开发框架
    查看>>
    NOTE:rfc5766-turn-server
    查看>>
    Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
    查看>>