迷宫(数据结构栈课设-可运行)

数据机构迷宫源码:用栈来完成一个迷宫。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>
#include<time.h>
#include<vector>
#define MAXSIZE 100
#define X 9
#define Y 9
typedef struct {
	int state;		//0是墙 1是未走过的路 2走过的路 3分岔路 4是返回的路
	int x;
	int y;
}SElemType;

typedef struct StackNode {
	SElemType data;
	struct StackNode* next;
}StackNode, * LinkStack;

void InitStack(LinkStack& S);//初始化栈
int StackEmpty(LinkStack S); //判栈空
int PushStack(LinkStack& S, SElemType e);//压栈
int PopStack(LinkStack& S, SElemType& e);//出栈
int Read(LinkStack& S, SElemType e);  //读取栈顶

void Printf_Map(int Map[][Y]);  //画迷宫

int RandMap(int* b_x, int* b_y, int* o_x, int* o_y);//读取迷宫

int Map[X][Y]; //地图二维数组

int main() {
	LinkStack S;
	SElemType e;
	int num = 0;
	int d, u, l, r;
	int x, y;
	int b_x, b_y, o_x, o_y;
	int return_Read = 1;
	InitStack(S);//初始化栈
	d = u = l = r = 0;//初始化四个方向
	Map[X][Y] = { 0 };//初始化地图

	RandMap(&b_x, &b_y, &o_x, &o_y);//随机产生地图后返回初始地址和终点地址
	e.x = b_x;
	e.y = b_y;   //给起始结点赋值
	x = o_x;     
	y = o_y;
	printf("起点%d %d 终点%d %d\n", b_x, b_y, o_x, o_y);


	e.state = 2;
	PushStack(S, e);   //起始结点入栈

	//将起点和终点打印在地图上
	Map[e.x][e.y] = 2;
	Map[o_x][o_y] = 1;
	Printf_Map(Map);
	system("pause");
	system("cls");




	while (1)
	{
		//向下走
		if (Map[e.x + 1][e.y] == 1)
		{
			d = 1;
			num++;
		}
		//向上走
		if (Map[e.x - 1][e.y] == 1)
		{
			u = 1;
			num++;
		}
		//向左走
		if (Map[e.x][e.y - 1] == 1)
		{
			l = 1;
			num++;
		}
		//向右走
		if (Map[e.x][e.y + 1] == 1)
		{
			r = 1;
			num++;
		}
		if (num == 0)
		{
			e.state = 2;//标记为走过的路
			//退回到分叉路口
			while (e.state != 3)
			{
				PopStack(S, e);
				return_Read = Read(S, e);
				Map[e.x][e.y] = 4;//把返回的路标记为4
			}
			PopStack(S, e);
			Read(S, e);
			Map[e.x][e.y] = 4;
			e.state = 3;
		}
		else if (num == 1)
		{
			e.state = 2;
		}
		else if (num > 1)
		{
			e.state = 3;
		}

		num = 0;
		if (r == 1)
		{
			e.y = e.y + 1;
		}
		else if (d == 1)
		{
			e.x = e.x + 1;
		}
		else if (l == 1)
		{
			e.y = e.y - 1;
		}
		else if (u == 1)
		{
			e.x = e.x - 1;
		}


		d = u = l = r = 0;
		PushStack(S, e);

		//退出条件 走到终点时
		if (e.x == o_x && e.y == o_y)
		{
			Map[e.x][e.y] = 2;
			Sleep(1000);
			system("cls");
			Printf_Map(Map);
			printf("\n成功走出\n");
			system("pause");
			printf("走出的倒序路径为");
			while (S != NULL)
			{
				printf("%d %d", S->data.x, S->data.y);
				printf("\n");
				S = S->next;
			}
			break;
		}

		Map[e.x][e.y] = 2;
		Printf_Map(Map);
		Sleep(1000);
		system("cls");//迷宫每走一步就清屏达到动态移动效果
		///////////////////////////////////////////////////////
		if (return_Read == 0)
		{
			//退出条件 本来就没有终点
			Map[e.x][e.y] = 2;
			Sleep(1000);
			system("cls");
			Printf_Map(Map);
			printf("\n该迷宫没有通路\n");
			system("pause");

			break;
		}
	}
	
	
	return 0;
}


//S为栈顶指针

void InitStack(LinkStack& S) {	//构造一个空栈,栈顶指针为空
	S = NULL;
}

int StackEmpty(LinkStack S) {    //判断栈空
	if (S == NULL)
		return 1;
	else
		return 0;
} 

int PushStack(LinkStack& S, SElemType e) {
	LinkStack p;
	p = new StackNode;	//生成新的节点P
	p->data = e;	    //将数据的新节点赋值成e
	p->next = S;		//将新节点插入栈顶
	S = p;				//将S向上移动成为新的栈顶指针
	return 0;
}
int PopStack(LinkStack& S, SElemType& e) {
	LinkStack p;
	if (S == NULL)	    //空栈
		return 0;
	e = S->data;	    //将栈顶节点数据的数据赋值到e里面
	p = S;				//栈顶增加一个p指针
	S = S->next;		//指针S向下移动
	delete p;			//释放栈顶空间
	return 1;
}

int Read(LinkStack& S, SElemType e)
{
	if (S == NULL)
	{
		return 0;
	}
	e = S->data;
	return 1;
}

void Printf_Map(int Map[][Y])  //打印地图
{
	int i, j;
	for (i = 0; i < X; i++)
	{
		for (j = 0; j < Y; j++)
		{
			printf("%d ", Map[i][j]);
		}
		printf("\n");
	}
}

int RandMap(int* b_x, int* b_y, int* o_x, int* o_y)
{
	//int Map[X][Y] = { 0 };
	int i, j, k;
	int x, y;
	int location; 
	int temp;
	int begin_x, begin_y;
	int out_x, out_y;
	//初始化地图
	for (i = 1; i <= X - 1; i = i + 2)
	{
		for (j = 1; j <= Y - 1; j = j + 2)
		{
			Map[i][j] = 1;//找到迷宫的九宫格中心
		}
	}


	/*
		666
	*/

	//Printf_Map(Map);

	//生成随机地图
	srand((int)time(NULL));
	for (i = 1; i <= X - 1; i = i + 2)
	{
		for (j = 1; j <= Y - 1; j = j + 2)
		{
			temp = rand() % 2 + 1; //随机生成1~2
			for (k = 0; k < temp; k++)
			{
				while (1)
				{
					x = i;
					y = j;
					location = rand() % 4;
					if (x == 1 && location == 0)
					{
						continue;
					}
					else if (x == X - 2 && location == 1)
					{
						continue;
					}
					else if (y == 1 && location == 2)
					{
						continue;
					}
					else if (y == Y - 2 && location == 3)
					{
						continue;
					}
					else
						break;
				}
				switch (location)
				{
				case 0: x = x - 1; break;
				case 1: x = x + 1; break;
				case 2: y = y - 1; break;
				case 3: y = y + 1; break;
				}
				Map[x][y] = 1;
			}
		}
	}
	//随机产生一个起点
	srand((int)time(NULL));
	while (1)
	{
		begin_x = rand() % (X);
		begin_y = rand() % (Y);
		if (begin_x == 0 && begin_y == 0)
		{
			continue;
		}
		else if (begin_x == 0 && begin_y == Y - 1)
		{
			continue;
		}
		else if (begin_x == X - 1 && begin_y == 0)
		{
			continue;
		}
		else if (begin_x == X - 1 && begin_y == Y - 1)
		{
			continue;
		}
		if (begin_x == 0 || begin_x == X || begin_y == 0 || begin_y == Y)
		{
			break;
		}
	}
	Map[begin_x][begin_y] = 1;

	//随机产生一个终点
	srand((int)time(NULL));
	while (1)
	{
		out_x = rand() % (X);
		out_y = rand() % (Y);
		if (out_x == 0 && out_y == 0)
		{
			continue;
		}
		else if (out_x == 0 && out_y == Y - 1)
		{
			continue;
		}
		else if (out_x == X - 1 && out_y == 0)
		{
			continue;
		}
		else if (out_x == X - 1 && out_y == Y - 1)
		{
			continue;
		}
		if (out_x == begin_x || out_y == begin_y)
		{
			continue;
		}
		if (out_x == 0 || out_x == X || out_y == 0 || out_y == Y)
		{
			break;
		}
	}
	//Map[out_x][out_y] = 1;
	//Printf_Map(Map);

	//指针传参
	*b_x = begin_x;
	*b_y = begin_y;
	*o_x = out_x;
	*o_y = out_y;
	return 0;
}

#include using std::cout; using std::cin; #include #include #include #define OVERFLOW -2 #define INIT_SIZE 100 //存储空间初始分配量 #define INCREMENT 10 //存储空间分配增量 typedef struct{ int r; int c; }PostType;//迷宫中r行c列的位置 typedef struct{ int ord; //当前位置在路径上的序号 PostType seat;//当前坐标 int di; //往下一坐标的方向 }SElemType; //元素类型 typedef struct{ SElemType* base;//基址,构造前销毁后为空 SElemType* top;//顶 int stackSize; //容量 }Stack; //类型 int InitStack(Stack &S){ //构造空s S.base=(SElemType*)malloc(INIT_SIZE *sizeof(SElemType)); if(!S.base) exit(OVERFLOW);//存储分配失败 S.top=S.base; S.stackSize=INIT_SIZE; return 0; } int StackEmpty(Stack S)//若s为空返回TRUE,否则返回FALSE { if(S.top==S.base) return 1; return 0; } int Push(Stack &S,SElemType e){ //插入元素e为新的顶元素 if(S.top-S.base >=S.stackSize){//满,加空间 S.base=(SElemType *)realloc(S.base,(S.stackSize+INCREMENT)*sizeof(SElemType)); if(!S.base) exit(OVERFLOW); //存储分配失败 S.top=S.base+S.stackSize; S.stackSize+=INCREMENT; } *S.top++=e; return 1; } int Pop(Stack &S,SElemType &e){//若不空删除,顶元素用e返回 if(S.top==S.base) return 0; e=*--S.top; return 1; } int DestroyStack(Stack &S){//销毁S, free(S.base); S.top=S.base; return 1; } #define MAXLEN 16//迷宫包括外墙最大行列数目 typedef struct{ int r; int c; char adr[MAXLEN][MAXLEN]; }MazeType; //迷宫类型 int InitMaze(MazeType &maze){ //初始化迷宫若成功返回TRUE,否则返回FALSE int i,j; cout<>maze.r>>maze.c; //迷宫行和列数 for(i=0;i<=maze.c+1;i++){//迷宫行外墙 maze.adr[0][i]='#'; maze.adr[maze.r+1][i]='#'; } for(i=0;i<=maze.r+1;i++){//迷宫列外墙 maze.adr[i][0]='#'; maze.adr[i][maze.c+1]='#'; } for(i=1;i<=maze.r;i++) for(j=1;j<=maze.c;j++) maze.adr[i][j]=' ';//初始化迷宫 int m=1,n=maze.c; for(m;m1;n--) maze.adr[6][n]='#'; maze.adr[4][5]='#'; maze.adr[4][6]='#'; maze.adr[3][3]='#'; maze.adr[3][2]='#'; maze.adr[5][7]='#'; maze.adr[5][6]='#'; maze.adr[8][3]='#'; maze.adr[7][2]='#'; maze.adr[7][5]='#'; return 1; }//InitMaze int Pass(MazeType maze,PostType curpos){ if(maze.adr[curpos.r][curpos.c]==' ') return 1; else return 0; }//Pass int FootPrint(MazeType &maze,PostType curpos){ //若走过并且可通返回TRUE,否则返回FALSE //在返回之前销毁S maze.adr[curpos.r][curpos.c]='!';//"*"表示可通 return 1; }//FootPrint PostType NextPos(PostType &curpos,int i){ //指示并返回下一位置的坐标 PostType cpos; cpos=curpos; switch(i){ case 1 : cpos.c+=1; break; case 2 : cpos.r+=1; break; case 3 : cpos.c-=1; break; case 4 : cpos.r-=1; break; default: exit(0); } return cpos; }//Nextpos int MarkPrint(MazeType &maze,PostType curpos){ maze.adr[curpos.r][curpos.c]='@';//"@"表示曾走过但不通 return 1; } int MazePath(MazeType &maze,PostType start,PostType end){ //若迷宫maze存在从入口start到end的通道则求得一条存放在中 Stack S; PostType curpos; int curstep;//当前序号,1.2.3.4分别表示东,南,西,北方向 SElemType e; InitStack(S); curpos=start; //置"当前位置"为"入口位置" curstep=1; //探索第一步 do{ if(Pass(maze,curpos)){//当前位置可以通过,即是未曾走到过的通道 FootPrint(maze,curpos);//留下足迹 e.ord=curstep; e.seat=curpos; e.di=1; Push(S,e); //加入路径 if(curpos.r==end.r&& curpos.c==end.c) if(!DestroyStack(S))//销毁失败 exit(OVERFLOW); else return 1; //到达出口 else{ curpos=NextPos(curpos,1); //下一位置是当前位置的东邻 curstep++; //探索下一步 } } else{ //当前位置不通 if(!StackEmpty(S)){ Pop(S,e); while(e.di==4 && !StackEmpty(S)){ MarkPrint(maze,e.seat); Pop(S,e); //留下不能通过的标记,并退一步 } if(e.di < 4){ e.di++;//换下一个方向探索 Push(S,e); curpos=NextPos(e.seat,e.di);//定当前位置是该新方向上的相邻位置 } } } }while(!StackEmpty(S)); if(!DestroyStack(S))//销毁失败 exit(OVERFLOW); else return 0; }//MazePath void PrintMaze(MazeType &maze){ //将标记路径信息的迷宫输出 int i,j; cout<<"\n——!为所求迷宫路线路线——:\n\n"; cout<<" "; for(i=0;i<=maze.r+1;i++)//打印列数名 cout<<" "<<i; cout<<"\n\n"; for(i=0;i<=maze.r+1;i++){ cout<<" "<<i;//打印行名 for(j=0;j<=maze.c+1;j++) cout<<" "<<maze.adr[i][j];//输出迷宫路径 cout<<"\n\n"; } } void main(){ MazeType maze; PostType start,end; char cmd; do{ cout<<"-------建立迷宫--------\n"; if(!InitMaze(maze)){ cout<<"\n——建立有误——!!!\n"; exit(OVERFLOW); } do{ cout<>start.r>>start.c; if(start.r>maze.r || start.c>maze.c){ cout<maze.r || start.c>maze.c); do{ cout<>end.r>>end.c; if(end.r>maze.r || end.c>maze.c){ cout<maze.r || end.c>maze.c); if(!MazePath(maze,start,end)) cout<<"\n不能求得路径!\n"; else PrintMaze(maze); cout<>cmd; }while(cmd=='y' || cmd=='Y'); }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值