定义一个二维数组:
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。Input一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。Output左上角到右下角的最短路径,格式如样例所示。Sample Input0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0Sample Output(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
之前用深度优先做过这道题,今天用广度优先做一下。
比较棘手的就是找到最短路径之后如何输出,可以写一个结构体,里面存放坐标点,步数和上一个点的下标。这样的话,求出最短路之后,就可以通过终点坐标反推出之前的坐标,一直到起点。
可以通过栈来把坐标点逆序。
#include<iostream>
#include<stack>
using namespace std;
struct note {
int x, y; //x,y为坐标
int last; //last为上个点的下标值
int step; //step记录步数
};
int maze[5][5];
int book[5][5]; //标记
note que[9999]; //结构体数组,形成一个队列结构
void print(note xy) { //xy为终点,根据last.step可以找出上一个点,一直到起点
stack<note> sta;
sta.push(xy);
while (sta.top().last!=-1) { //把它们依次放入栈中
sta.push(que[sta.top().last]);
}
while (!sta.empty()) { //再pop,可以得到逆序的排列
cout << "(" << sta.top().x << ", " << sta.top().y << ")" << endl;
sta.pop();
}
}
note bfs() {
int next[4][2] = { 1,0,-1,0,0,1,0,-1 }; //方向数组
int flag = 0;
int head = 0, tail = 0, tx, ty;
que[tail].x = 0; //先把起点坐标放入队列
que[tail].y = 0;
que[tail].step = 0;
que[tail].last = -1;
tail++;
while (head<tail) { //队列为空退出循环
for (int i = 0; i < 4; i++) {
tx = que[head].x + next[i][0];
ty = que[head].y + next[i][1];
if (tx < 0 || ty < 0 || tx>4 || ty>4) { //坐标越界寻找其他的坐标
continue;
}
if (maze[tx][ty] == 0 && book[tx][ty] == 0) { //坐标没有越界并且该点没有障碍物并且没有走过
book[tx][ty] = 1; //把这个点放入队列
que[tail].x = tx;
que[tail].y = ty;
que[tail].last = head;
que[tail].step = que[head].step + 1;
tail++;
}
if (tx == 4 && ty == 4) { //如果到达终点,退出循环
flag = 1;
break;
}
}
if (flag) {
break;
}
head++;
}
return que[tail-1]; //返回终点
}
int main() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cin >> maze[i][j];
}
}
print(bfs());
return 0;
}