假如我们给定一个如下所示的迷宫,蓝色表示边界或不可通过的区域(用1表示),白色表示可以通过的区域(用0表示),绿色表示起始点,红色表示到达的终点。这样可以用一个数组表示这个Maze。
我们要做的是找到从起始点到终点的最短路径。要求用4领域可通过。
下面是我用 Queue 实现的寻径算法:
#include "stdafx.h"
#include <iostream>
#include "Queue.h"
#include <stack>
#include <iomanip>
using namespace std;
class Position
{
public:
Position(int x=0,int y=0):row(x),col(y){}
int row;
int col;
};
const int m=7;
const int n=8;
int Maze[m+2][n+2] = { 1,1,1,1,1,1,1,1,1,1,
1,0,0,0,0,0,1,0,0,1,
1,0,1,1,1,0,0,1,0,1,
1,0,0,1,0,1,0,0,0,1,
1,1,0,1,0,1,1,0,1,1,