基于下面的思想:
here:目前的位置
dest:出口位置
path:路径栈
1:有障碍,0:有路
1、查找here周围的点,看看是否有路
2、如果有,则here=1,将原位置压入栈,再将here指向新的位置
3、没有,path.pop(pop),跳回步骤1。
4、如果没有点,即栈为空,则无法找到路径
void FindRouth()
{
const int ROW=12; //该迷宫外面一圈为障碍墙
const int COL=12;
const int TOTAL=(ROW-2)*(COL-2);
int i=0,j=0;
int maze[ROW][COL]; //迷宫矩阵
//读入迷宫数据,0表示有路,1表示有障碍
for(i=1;i<ROW-1;i++)
for(j=1;j<COL-1;j++)
cin>>maze[i][j];
//在迷宫外面加一圈围墙,方便比较
for(i=0;i<ROW;i=i+ROW-1)
for(j=0;j<COL;j=j+COL-1)
maze[i][j]=1;
CStack<CPostion> path(TOTAL-1); //路径栈
CPostion here; //Current Position
here.col=2;
here.row=2;
CPostion dest; //Out door Position
dest.col=9;
dest.row=9;
while(!(here==dest)) //进行比较
{
maze[here.row][here.col]=1;
if(maze[here.row-1][here.col]==0)
{
path.Push(here);
here.row=here.row-1;
continue;
}
if(maze[here.row][here.col-1]==0)
{
path.Push(here);
here.col=here.col-1;
continue;
}
if(maze[here.row+1][here.col]==0)
{
path.Push(here);
here.row=here.row+1;
continue;
}
if(maze[here.row][here.col+1]==0)
{
path.Push(here);
here.col=here.col+1;
continue;
}
if(!path.Pop(here))
{
cout<<"there is no route./n";
return;
}
}
cout<<"Success find rout./n";
while(path.Pop(here))
cout<<here;
}