当我们学习了栈这种数据结构以后,我们就可以利用栈来实现一个简单的迷宫游戏 ,首先我们给出迷宫的地图,这是一个二维数组。
{0,0,1,0,0,0,0,0,0,0},
{0,0,1,1,1,1,1,0,0,0},
{0,0,0,0,0,0,1,0,0,0},
{0,0,0,1,0,0,1,0,0,0},
{0,0,0,1,1,1,1,0,0,0},
{0,0,0,1,0,0,0,0,0,0},
{0,0,0,1,0,0,0,0,0,0},
{0,0,0,1,1,1,1,0,0,0},
{0,0,0,0,0,0,1,0,0,0},
{0,0,0,0,0,0,1,0,0,0},
我们从数组的(9,6)这个位置开始走,直到(0,2)这个位置走到出口。下面是实现的代码
struct Seat
{
Seat(int x, int y)
:_x(x)
, _y(y)
{}
int _x;
int _y;
};
class Maze
{
public:
bool IsPass(Seat& Pcur)
{
if (IsExit(Pcur))
return true;
if (maze[Pcur._x][Pcur._y] == 1)
return true;
return false;
}
bool IsExit(const Seat& s)
{
if ((s._x < 0