用广度优先搜索解决迷宫问题是一个比较基础的方法。由于自己在算法方面基础不是很好,并没有达到能随手就写出BFS的水平,所以花了点时间写了一个BFS来解决比较基础的迷宫问题,权当练习,并供自己以后参考。
迷宫描述:迷宫是一个二维数组。其中数字0表示能通过,数字1表示为障碍,数字5代表起点,数字8代表终点。程序首先从laby.txt文件中读取迷宫并存储在二维数组laby中。迷宫显示如下:
迷宫存储:使用二维数组存储迷宫。二维数组中每个元素都是一个node,而每个node中包含:当前点的x,y值,前驱节点的x,y值,当前点的数据以及当前点是否已经被访问。记录前驱节点的x,y值方便最后的路径生成。
广度优先搜索的思想:使用队列来存取node。首先将起点压入队列。然后每次从队列中取出头节点,遍历其周围的四个节点,检视其是否已被访问以及是否能走,然后加入队列,并记录加入节点的前驱节点坐标。最后当到达终点的时候,沿着终点的前驱坐标一路回溯,就能得到整条最短路径了。不多说了,直接上代码吧。
#include <iostream>
#include <fstream>
#include <stack>
#include <vector>
#include <queue>
using namespace std;
const int MAX = 10;
struct node {
int x,y,fx,fy;
int content;
bool visited;
};
node laby[MAX][MAX];
void BFS(int sx, int sy,int ex, int ey) {
queue<node> myQueue;
node temp = laby[sx][sy];
myQueue.push(temp);
int delx[4] = {0,0,-1,1};
int dely[4] = {1,-1,0,0};
while (!myQueue.empty()) {
node currentNode = myQueue.front();
myQueue.pop();
int m = currentNode.x;
int n = currentNode.y;
currentNode.visited = true;
if (currentNode.x == ex && currentNode.y == ey) {
break;
}
for(int i = 0; i < 4;i++) {
int next_m = m + delx[i];
int next_n = n + dely[i];
if (next_m < 0 || next_m >= MAX || next_n < 0 || next_n >= MAX) continue;
if (laby[next_m][next_n].content != 1 && laby[next_m][next_n].visited == false) {
laby[next_m][next_n].fx = m;
laby[next_m][next_n].fy = n;
myQueue.push(laby[next_m][next_n]);
laby[next_m][next_n].visited = true;
}
}
}
int backx = ex, backy = ey;
int step = 0;
stack<node> showStack;
while (backx != sx || backy != sy) {
showStack.push(laby[backx][backy]);
int tempBackx = laby[backx][backy].fx;
int tempBacky = laby[backx][backy].fy;
backx = tempBackx;
backy = tempBacky;
step++;
}
cout<<"Path:"<<endl;
while (!showStack.empty()) {
node current = showStack.top();
showStack.pop();
cout<<'('<<current.x<<','<<current.y<<") ";
}
cout<<endl;
cout<<"total steps:"<<step<<endl;
}
int main(int argc, const char * argv[]) {
ifstream in;
in.open("laby.txt",ios::in);
if (!in) {
cerr<<"file not existed!"<<endl;
exit(1);
}
int sx,sy,ex,ey;
int curNum;
int m = 0, n = 0;
while (!in.eof()) {
in>>curNum;
laby[m][n].content = curNum;
laby[m][n].x = m;
laby[m][n].y = n;
laby[m][n].visited = false;
if (curNum == 5) {
sx = m;
sy = n;
}
if (curNum == 8) {
ex = m;
ey = n;
}
n ++;
if (n == MAX) {
n = 0;
m++;
}
}
for(int i = 0; i < MAX; i++) {
for(int j = 0; j < MAX; j++) {
cout<<laby[i][j].content<<" ";
}
cout<<endl;
}
cout<<endl;
BFS(sx, sy, ex, ey);
return 0;
}