迭代版本的 BFS,用于解决迷宫问题
#include <iostream>
#include <stack>
#include <vector>
#include <queue>
using namespace std;
struct Grid
{
int x;
int y;
int weight;
};
struct Record
{
vector<Grid> from;
};
bool IsVisited(vector<vector<int>> visited, int x, int y)
{
return visited[y][x] == 1;
}
bool IsValid(vector<vector<int>> maze, int x, int y)
{
return maze[y][x] != -1;
}
bool IsDestination(Grid dest, int x, int y)
{
return (x == dest.x && y == dest.y);
}
Grid Direction[] =
{
{0,-1},
{-1,0}, {1,0},
{0,1 }
};
void BFS_FindPath(vector<vector<int>> maze, Grid src, Grid dest, int h, int w)
{
bool isDestination;
vector<vector<Record>> record(h, vector<Record>(w));
vector<vector<int>> visited(h, vector<int>(w, 0));
queue<Grid> gridQueue;
gridQueue.push(src);
Grid curGrid = gridQueue.front();
while(!gridQueue.empty())
{
curGrid = gridQueue.front();
visited[curGrid.y][curGrid.x] = 1;
gridQueue.pop();
if(IsDestination(dest, curGrid.x, curGrid.y))
{
isDestination = true;
break;
}
for(int i = 0; i < 4; i++)
{
int next_x = curGrid.x + Direction[i].x;
int next_y = curGrid.y + Direction[i].y;
if(IsValid(maze, next_x, next_y))
{
if(!IsVisited(visited, next_x, next_y))
{
gridQueue.push(Grid{next_x, next_y});
record[next_y][next_x].from.push_back(curGrid);
}
}
}
}
if(isDestination)
{
while(curGrid.x != src.x || curGrid.y != src.y)
{
cout << "(" << curGrid.y << " " << curGrid.x << ")";
curGrid = record[curGrid.y][curGrid.x].from.front();
}
cout << "(" << src.y << " " << src.x << ")";
}
return;
}
void ReadMaze(vector<vector<int>>& maze, int w, int h)
{
for(int i = 0; i < h; i++)
{
for(int j = 0; j < w; j++)
{
cin >> maze[i][j];
}
}
}
int main()
{
int w,h;
cin >> w >> h;
Grid src;
cin >> src.y >> src.x;
Grid dest;
cin >> dest.y >> dest.x;
vector<vector<int>> maze(h, vector<int>(w));
ReadMaze(maze, w, h);
BFS_FindPath(maze, src, dest, h, w);
return 0;
}
用DFS在某种情况下耗时会非常久,例如
所以采用BFS可以更快的得到最短路径(并非权重最小,程序并未加上权重判断)
Input:
10 10 3 6 8 8 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 2 1 1 1 1 1 5 1 -1 -1 1 9 9 9 1 1 -1 1 -1 -1 1 1 1 1 1 1 -1 1 -1 -1 1 -1 -1 -1 -1 -1 -1 1 -1 -1 1 9 9 9 1 1 1 1 -1 -1 1 1 1 1 1 1 1 1 -1 -1 1 1 1 1 1 1 1 1 -1 -1 1 1 1 1 1 1 1 2 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
Out:
(8 8)(7 8)(6 8)(5 8)(4 8)(3 8)(2 8)(1 8)(1 7)(1 6)(2 6)(3 6)
分类:
杂项
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)