迭代版本的 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)