问题:实现迷宫程序,要求输入一个迷宫数组,左上角(1,1)坐标处为入口,右下角(M,N)坐标处为出口。“1”代表墙壁,“0”代表通路,如下图所示:
如果迷宫有路,则输出迷宫路径。
(1,1)
(1,2)
…
如果迷宫 没有路,
则输出No path.
迷宫问题是典型的栈的应用实例,具体算法伪代码如下
我是自己定义了栈的一个类,如果觉得麻烦,C++为程序员提供了库文件,输入#include就可以调用以下函数
完整代码
#include <iostream>
using namespace std;
const int M = 5, N = 5;
typedef struct
{
int incX,incY;
} Direction;
typedef struct
{
int x,y;//当前坐标
int di;//当前方向
} Box;
const char* direct[4] = {
"right","down","left","up" };
class Stack
{
private:
Box* path;
int top;
public:
Stack();
~Stack();
void push(Box temp);
Box pop();
bool isEmpty();
bool isFull();
void displayPath();
class Empty {
};
class Full {
};
};
Stack::Stack()
{
top