数据结构源码--迷宫

来源:互联网 发布:js给div添加class属性 编辑:程序博客网 时间:2024/06/05 19:28
#include <string>#include <malloc.h>#include <conio.h>#include <stdlib.h>#include <iostream>#define STACK_INIT_SIZE 1000#define STACK_MORE 10#define OVERFLOW -2#define OK 1#define ERROR 0#define TRUE 1#define FALSE 0using namespace std;typedef struct{int line;//横坐标int row;//纵坐标}PosType;  // 迷宫类型typedef struct{int ord;           //通道块在路径上的序号PosType seat;      //通道块在迷宫中的坐标位置int di;            //从此通道块走向下一通道块的方向  东1;南2;西3;北4}SElemType;typedef struct{SElemType *base;   //定义后进先出的栈SElemType *top;int stacksize;}SqStack;typedef int MazeType[100][100];   //定义迷宫大小MazeType maze;   //定义一个迷宫SqStack S;       //定义一个栈PosType curpos;   //定义个迷宫表格SElemType e;      //int curstep;int InitStack(SqStack &S){        //构造一个空栈SS.base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));//分配空间if(!S.base)exit (OVERFLOW);S.top=S.base;S.stacksize=STACK_INIT_SIZE;return OK;}int StackEmpty(SqStack S){  //初始条件:栈S已存在。                             //操作结果:若栈为空则返回TRUE,否则返回FALSE;    if(S.base==S.top)return TRUE;else return FALSE;}int Push(SqStack &S,SElemType e){  //初始条件:栈已经存在。                                    //操作结果:在栈的顶部插入新的栈顶元素;    if(!S.base)exit (OVERFLOW);*S.top++=e;return OK;}int Pop(SqStack &S,SElemType &e){   //初始条件:栈已经存在;                                    //操作结果:删除栈顶元素,并以E返回其值    if(S.top==S.base)return ERROR;e=*--S.top;return OK;}int Pass(PosType curpos){             //判断是否通过if(maze[curpos.row][curpos.line]=='_')return OK;else return ERROR;}void FootPrint(PosType curpos){           //将迷宫通路设置成.maze[curpos.row][curpos.line]='.';}PosType NextPos(PosType curpos,int di){  //下个路径if(di==1)curpos.row++;                   //从右边开始遍历else if(di==2)curpos.line--;                  //下else if(di==3)curpos.row--;                   //左else if(di==4)curpos.line++;                  //上return curpos;}void MarkPrint(PosType curpos){maze[curpos.row][curpos.line]='^';}void PrintMaze(int row,int line){        //打印迷宫for(int i=0;i<row;i++){for(int j=0;j<line;j++)cout<<(char)maze[i][j]<<"   ";cout<<endl<<endl;}}void CreatMaze(int r,int l){   //根据提示创建迷宫的墙为*,通路为_,障碍为#int i,j;for(i=0;i<r;i++){maze[i][0]='*';        //左面墙maze[i][l-1]='*';      //右面墙}for(j=1;j<l-1+1;j++){maze[0][j]='*';     //上面墙maze[r-1][j]='*';   //下面墙}for(i=1;i<r-1;i++)for(j=1;j<l-1;j++)maze[i][j]='_';cout<<"请输入迷宫内障碍物的个数:";int num;cin>>num;cout<<"请依次输入障碍物的坐标:"<<endl;int x,y;for(i=1;i<=num;i++){cin>>x>>y;maze[x][y]='#';             //设置障碍物}cout<<endl<<"创建的迷宫的如下:"<<endl<<endl;PrintMaze(r,l);cout<<endl;}int MazePath(PosType start,PosType end){InitStack(S);curpos=start;// 设定当前位置为入口位置curstep=1;//第一步do{if(Pass(curpos)){//当前位置可以通过(未曾走过)FootPrint(curpos);//留下足迹e.ord=curstep;      //序号e.seat=curpos;       //坐标位置e.di=1;               //方向Push(S,e);              //加入路径if(curpos.row==end.row&&curpos.line==end.line)//到达终点return TRUE;curpos=NextPos(curpos,1);//下一位置是当前位置的东邻curstep++;//探索下一步}else{//当前位置不能通过if(!StackEmpty(S)){Pop(S,e);while(e.di==4&&!StackEmpty(S)){MarkPrint(e.seat);//留下不能通过的标志Pop(S,e);//退回一步}if(e.di<4){e.di++;//换下一个方向探索Push(S,e);curpos=NextPos(e.seat,e.di);//设定当前位置是该新方向上的相邻块}}}}while(!StackEmpty(S));return FALSE;}int main(){PosType start,end;         //用户自定义入口和出口int r,l;                   //用户自定义迷宫行数和列数cout<<"请输入迷宫(外围有墙)的行、列数(不大于100):";cin>>r>>l;                 //将行列数写入内存CreatMaze(r,l);            //绘制行数为r列数为l的迷宫cout<<"迷宫的起点坐标:";  //用户定义起点坐标cin>>start.row>>start.line;cout<<"迷宫的终点坐标:";  //用户定义终点坐标cin>>end.row>>end.line;cout<<endl;if(MazePath(start,end)){cout<<"迷宫的通路:"<<endl<<"(其中*表示外墙,#表示迷宫内障碍物,.为通路)"<<endl<<endl;PrintMaze(r,l);}else {        cout<<"迷宫没有通路!"<<endl;        main();}return 0;}

0 0
原创粉丝点击