迷宫图案,白色代表通道,黑色代表墙。
迷宫入口坐标(1,1),出口坐标(8,8)
0 1 2 3 4 5 6 7 8 9
0■■■■■■■■■■
1■□□■□□□■□■
2■□□■□□□■□■
3■□□□□■■□□■
4■□■■■□□□□■
5■□□□■□□□□■
6■□■□□□■□□■
7■□■■■□■■□■
8■■□□□□□□□■
9■■■■■■■■■■
程序源代码(VC++
6.0下编译通过)
# include
<iostream>
using namespace std;
const int SIZE_X=10;
const int SIZE_Y=10;
//data struct
struct mPoint
{
int x;
int y;
bool
can_move_to;
mPoint(int
rx=0,int ry=0,bool rcan_move_to=false)
{
x=rx;
y=ry;
can_move_to=rcan_move_to;
next=NULL;
}
mPoint
*next;
};
//function declaration
class mStack
{
public:
mStack();//constructor
int
push(mPoint point);
mPoint
pop();
int
getLength();
mPoint
getTop();
void
printStack();
private:
mPoint
*base;//base pointer
mPoint *top;
//top pointer
int length;
//length of stack
};
int main()
{
mPoint
mpArray[SIZE_X][SIZE_Y];
bool
initArray[SIZE_X][SIZE_Y]={{false,false,false,false,false,false,false,false,false,false},
{false,true
,true ,false,true ,true ,true ,false,true ,false},
{false,true
,true ,false,true ,true ,true ,false,true ,false},
{false,true
,true ,true ,true ,false,false,true ,true ,false},
{false,true
,false,false,false,true ,true ,true ,true ,false},
{false,true
,true ,true ,false,true ,true ,true ,true ,false},
{false,true
,false,true ,true ,true ,false,true ,true ,false},
{false,true
,false,false,false,true ,false,false,true ,false},
{false,false,true ,true ,true ,true ,true ,true ,true
,false},
{false,false,false,false,false,false,false,false,false,false}};//迷宫矩阵
for(int
i=0;i<SIZE_X;i++)//init
for(int j=0;j<SIZE_Y;j++)
{
mpArray[i][j].x=i;
mpArray[i][j].y=j;
mpArray[i][j].can_move_to=initArray[i][j];
}
mPoint startp(1,1,true);//entry
mPoint endp(8,8,true); //exit
mStack mpath;
mpath.push(startp);
mPoint mp=startp;
while(true)
{
if(mp.x==endp.x &&
mp.y==endp.y)
break;//success
if(mpArray[mp.x+1][mp.y].can_move_to)//search down
{
mpArray[mp.x+1][mp.y].can_move_to=false;
mpath.push(mPoint(mp.x+1,mp.y));