- 数据结构学到了栈,有很多典型的例子,比如说表达式求值、括号匹配检验等,老师布置了迷宫求解的作业,根据课本上的伪代码段改了一下,还是比较简单的~
- 算法核心就是,暴力求解。假如把一只老鼠放到迷宫里,无论如何,只要时间够,它总会跑出来的。这就是暴力求解。
- 编写程序遇到的小问题:path[ ]数组定义的不够大,导致最后打印路径的时候打印不全。因为暴力求解可能走很多冤枉路 所以如果 用一维数组保存路径坐标的话 要尽量大一点~
- 建立一个工程 有头文件 主函数 函数的实现三个文件 完整代码如下~
#ifndef MAZE_H_INCLUDED
#define MAZE_H_INCLUDED
//走迷宫,迷宫型定义为二维数组,墙的地方画上#
const int N=10;
//int maze[N][N];
#define stack_init_size 100
#define stackincrement 10//存储空间分配增量
typedef struct{
int x;
int y;
}PosType;//坐标
typedef struct{
int ord;//通道块在路径上的序号
PosType seat;//通道块在迷宫中的坐标位置
int di;//从此通道块走向下一通道块的方向
}SElemType;//栈的元素类型
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;//当前已分配的存储空间
}SqStack;
//int InitMaze(int N);
int StackEmpty(SqStack S);
void InitStack(SqStack &S);
void Push(SqStack &S,SElemType e);
void Pop(SqStack &S,SElemType &e);
//把路线打印出来
void PrintSqStack(SqStack S);
PosType NextPos(PosType seat,int di);
SqStack MazePath(int maze[10][10], PosType start, PosType ending);
#endif // MAZE_H_INCLUDED
#include<iostream>
#include"maze.h"
#include <stdlib.h>
#include<stack>
using namespace std;
//走迷宫,迷宫型定义为二维数组,墙的地方画上#
//迷宫数组初始化
int StackEmpty(SqStack S)
{
if(S.base==S.top)
return 1;
else
return 0;
}
void InitStack(SqStack &S)
{
S.base=(SElemType *)malloc(stack_init_size * sizeof(SElemType));
if(!S.base)exit(0);
S.top=S.base;
S.stacksize=stack_init_size;//100
}
void Push(SqStack &S,SElemType e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType *)realloc(S.base,(S.stacksize+stackincrement)*sizeof(SElemType));
if(!S.base)exit(0);
S.top=S.base+S.stacksize;
S.stacksize+=stackincrement;
}
*S.top ++= e;
}
void Pop(SqStack &S,SElemType &e)//用e返回pop出来的值
{
if(S.top==S.base)exit(0);
e = * --S.top;
}
PosType NextPos(PosType seat,int di)
{
PosType curpos;
curpos.x=seat.x;
curpos.y=seat.y;
if(di==1) curpos.y++;
else if(di==2) curpos.x++;
else if(di==3) curpos.y--;
else if(di==4) curpos.x--;
return curpos;
}
SqStack MazePath(int maze[10][10], PosType start, PosType ending)//返回一个栈,里面存储了正确的路线
{
SElemType e;
SqStack S;
InitStack(S);
PosType curpos=start;
int curstep=1;
do
{
if(maze[curpos.x][curpos.y]!=0) //当前位置不是墙,也是未曾走到过的通道块
{
maze[curpos.x][curpos.y]=0;//走过了
e.ord=curstep;
e.seat=curpos;
e.di=1;//向东走 东南西北
Push(S,e);
// cout<<"("<<e.seat.x<<","<<e.seat.y<<")"<<endl;
if(curpos.x==ending.x && curpos.y==ending.y)
return S;
curpos=NextPos(curpos,1);//下一位置是当前位置的东临
curstep++;//探索下一步
}
//当前位置已经走过了
else {
if(!StackEmpty(S))
{
Pop(S,e);
while(e.di==4 && !StackEmpty(S))
{
maze[e.seat.x][e.seat.y]=0;//留下不能通过的标记
Pop(S,e);
/* curpos.x=e.seat.x;
curpos.y=e.seat.y;*/
}//之道拿出一个有用的e来为止进行下面的判断
if(e.di<4)//肯定已经走过了
{
e.di++;
Push(S,e);
curpos=NextPos(e.seat,e.di);
}
}//当前位置已经走过了
}
}while (!StackEmpty(S));
return S;//空栈
}
void PrintSqStack(SqStack S)//把路线打印出来
{
int i=0,j=0,path_x[150],path_y[150],path_order[150];
SElemType e;
if(S.base==S.top)
{
cout<<"ERROR! The Stack is NULL!";
}
else
{
while(!StackEmpty(S))//当栈不空
{
Pop(S,e);
path_x[i]=e.seat.x;
path_y[i]=e.seat.y;
path_order[i]=e.ord;
i++;
}
for(j=i-1;j>0;j--)
{
cout<<"("<<path_x[j]<<","<<path_y[j]<<")->";
}
cout<<"("<<path_x[0]<<","<<path_y[0]<<")"<<endl;
}
}
#include<iostream>
#include"maze.h"
#include<stack>
using namespace std;
int main()
{
int i,j;
int maze[N][N]={{0,0,0,0,0,0,0,0,0,0},
{0,1,1,0,1,1,1,0,1,0},
{0,1,1,0,1,1,1,0,1,0},
{0,1,1,1,1,0,0,1,1,0},
{0,1,0,0,0,1,1,1,1,0},
{0,1,1,1,0,1,1,1,1,0},
{0,1,0,1,1,1,0,1,1,0},
{0,1,0,0,0,1,0,0,1,0},
{0,0,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0},
};
// int maze[N][N]=InitMaze(N);
PosType start,ending;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
cout<<maze[i][j]<<" ";
}
cout<<endl;
}
cout<<"Please enter the 'start' and the 'end'."<<endl;
cin>>start.x>>start.y>>ending.x>>ending.y;
SqStack S = MazePath(maze, start, ending);
PrintSqStack(S);
return 0;
}