数据结构源码--迷宫

#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 0
using 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){        //构造一个空栈S
	S.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
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值