博客
关于我
POJ 2312Battle City(BFS-priority_queue 或者是建图spfa)
阅读量:432 次
发布时间:2019-03-06

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

BFS算法实现

#include 
#include
#include
#include
#include
using namespace std;struct node { int x, y; int step; node(int x, int y, int step) : x(x), y(y), step(step) {}};bool operator>(node a, node b) { return a.step > b.step;}priority_queue
, greater
> q;bool bfs() { while (!q.empty()) { node cur = q.top(); q.pop(); if (map[cur.x][cur.y] == 'T') { cout << cur.step << endl; return true; } for (int i = 0; i < 4; ++i) { int xx = cur.x + dir[i][0]; int yy = cur.y + dir[i][1]; if (map[xx][yy] == 'B' || map[xx][yy] == 'R') continue; else { cout << cur.step + 1 << endl; true; } q.push(node(xx, yy, cur.step + 2)); } } return false;}int main() { while (cin >> n >> m && (n || m)) { for (int i = 1; i <= n; ++i) { cin >> map[i]; map[i][0] = map[i][m+1] = 'R'; for (int j = 1; j <= m; ++j) { if (map[i][j] == 'Y') { q.push(node(i, j, 0)); map[i][j] = 'R'; } } map[0][j] = map[n+1][j] = 'R'; } if (!bfs()) { cout << -1 << endl; } }}

SPFA算法实现

#include 
#include
#include
#include
#include
#include
#define N 900#define INF 0x3f3f3f3fusing namespace std;struct node { int to; int dist; node(int to, int dist) : to(to), dist(dist) {}};vector
g[N];int vis[N], d[N];char map[N][N];int dir[4][2] = {0, 1, 1, 0, -1, 0, 0, -1};int ss, tt;int n, m;queue
q;bool spfa() { q.push(ss); memset(vis, 0, sizeof(vis)); vis[ss] = 1; memset(d, 0x3f, sizeof(d)); d[ss] = 0; while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = 0; int len = g[u].size(); for (int i = 0; i < len; ++i) { int v = g[u][i].to; if (d[v] > d[u] + g[u][i].dist) { d[v] = d[u] + g[u][i].dist; if (!vis[v]) { q.push(v); vis[v] = 1; } } } } return d[tt] != INF;}int main() { while (cin >> n >> m && (n || m)) { for (int i = 0; i < n; ++i) { cin >> map[i]; for (int j = 0; j < m; ++j) { if (map[i][j] == 'R') ss = i * m + j; else if (map[i][j] == 'S') tt = i * m + j; else if (map[i][j] == 'B') { for (int k = 0; k < 4; ++k) { int x = i + dir[k][0]; int y = j + dir[k][1]; if (x < 0 || x >= n || y < 0 || y >= m) continue; if (map[x][y] == 'R' || map[x][y] == 'S') continue; int dist = 1; if (map[i][j] == 'B' && map[x][y] != 'B') dist = 2; if (map[i][j] == 'B' && map[x][y] == 'B') dist = 1; g[i * m + j].push_back(node(x * m + y, dist)); } } } } if (!spfa()) { cout << -1 << endl; } }}

代码解释

这两段代码分别实现了BFS和SPFA算法,用于解决迷宫问题中的最短路径问题。BFS算法在处理权重为1的边时效率较高,而SPFA算法则更适合处理权重不一致的复杂图结构。通过优先队列管理节点的步数,确保每次处理的节点是当前已知的最优路径节点。代码中使用了优先队列和结构体来封装节点信息,保证了代码的清晰和可维护性。

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

你可能感兴趣的文章
opencv9-膨胀和腐蚀
查看>>
OpenCV_ cv2.imshow()
查看>>
opencv_core.dir/objects.a(vs_version.rc.obj)‘ is incompatible with i386:x86-64 output
查看>>
opencv——图像缩放1(resize)
查看>>
opencv——最简单的视频读取
查看>>
Opencv——模块介绍
查看>>
OpenCV与AI深度学习 | 2024年AI初学者需要掌握的热门技能有哪些?
查看>>
OpenCV与AI深度学习 | CIB-SE-YOLOv8: 优化的YOLOv8, 用于施工现场的安全设备实时检测 !
查看>>
OpenCV与AI深度学习 | CoTracker3:用于卓越点跟踪的最新 AI 模型
查看>>
OpenCV与AI深度学习 | OpenCV中八种不同的目标追踪算法
查看>>
OpenCV与AI深度学习 | OpenCV图像拼接--Stitching detailed使用与参数介绍
查看>>
OpenCV与AI深度学习 | OpenCV如何读取仪表中的指针刻度
查看>>
OpenCV与AI深度学习 | OpenCV常用图像拼接方法(一) :直接拼接
查看>>
OpenCV与AI深度学习 | OpenCV常用图像拼接方法(三):基于特征匹配拼接
查看>>
OpenCV与AI深度学习 | OpenCV常用图像拼接方法(二) :基于模板匹配拼接
查看>>
OpenCV与AI深度学习 | OpenCV常用图像拼接方法(四):基于Stitcher类拼接
查看>>
OpenCV与AI深度学习 | OpenCV快速傅里叶变换(FFT)用于图像和视频流的模糊检测(建议收藏!)
查看>>
OpenCV与AI深度学习 | SAM2(Segment Anything Model 2)新一代分割一切大模型介绍与使用(步骤 + 代码)
查看>>
OpenCV与AI深度学习 | T-Rex Label !超震撼 AI 自动标注工具,开箱即用、检测一切
查看>>
OpenCV与AI深度学习 | YOLO11介绍及五大任务推理演示(目标检测,图像分割,图像分类,姿态检测,带方向目标检测)
查看>>