迷宫

本文介绍了两种迷宫求解算法:递归版本和非递归版本(使用链式栈)。通过具体的C语言代码示例展示了如何寻找从起点到终点的路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

迷宫可以说,是我们小时候超级喜欢玩的游戏,怎么玩,大家都很清楚。


介绍:1,表示墙,0,表示此路可通 ,#表示已走过,!表示出口

                "1111111111111",
"1011101111111",
"1011101111111",
"1000000000011",
"10111111110!1",
"1111111111111"

这是我给出来的迷宫。


一、递归版本的;

MAZE.h

#ifndef _MAZE_H_
#define _MAZE_H_
 
void show(char str[][14],int line,int colu);
void get_exter(char str[][14],int x,int y,int line ,int colu);
#endif
MAZE.cpp

<pre name="code" class="html">#include<stdio.h>
#include<stdlib.h>
#include"MAZE.h"

void show(char str[][14],int line,int colu)
{
	for(int i = 0;i < line;i ++)
	{
		for(int j = 0;j < colu;j ++)
		{
			printf("%c",str[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}

void get_exter(char str[][14],int x,int y,int line ,int colu)
{
	if(str == NULL || line <= 0 || colu <= 0)
	{
		exit(-1);
	}
	if(str[x][y] == '1' || str[x][y] == '#' )
	{
		return ;
	}
	if(str[x][y] == '!')
	{
		printf("出口已找到,出口为:(%d , %d)\n",x,y);
		exit(0);
	}
	str[x][y] = '#';
    show(str,line,colu);
	get_exter(str,x-1,y,line,colu);//上面是否通路
	get_exter(str,x,y-1,line,colu);//左面是否通路
	get_exter(str,x+1,y,line,colu);//下面是否通路
	get_exter(str,x,y+1,line,colu);//右面是否通路
}


 

MAIN.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"MAZE.h"

int main()
{
	char str[][14] = 
	{
		"1111111111111",
		"1011101111111",
		"1011101111111",
		"1000000000011",
		"10111111110!1",
		"1111111111111"
	};
	int colu = strlen(str[0]);//列
	int line = sizeof(str)/sizeof(str[0]);//行
	get_exter(str,1,1,line,colu);
	return 0;
}

二、迷宫--非递归(数据栈的实现)

MAZE.h

#ifndef _MAZE_H_
#define _MAZE_H_

void show(char str[][14],int line,int colu);
void get_exter(char str[][14],int x,int y,int line ,int colu);
#endif
MAZE.C

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"MAZE.h"
#include"LINK_STACK.h"


void show(char str[][14],int line,int colu)
{
	for(int i = 0;i < line;i ++)
	{
		for(int j = 0;j < colu;j ++)
		{
			printf("%c",str[i][j]);
		}
		printf("\n");
	}
	printf("\n");
}

static bool is_pathways(LINKSTACH *p,char str[][14],int x,int y,int line ,int colu)//要么可走,要么出口
{
	 if(x < 0 || y < 0)
	 {
		 return false;
	 }
	 for(int i = 0;i < line;i ++)
	 {
		 for(int j = 0;j < colu;j ++)
		 {
			 if(str[x][y] == '0')
			 {
				 return true;
			 }
			 else if(str[x][y] == '!')
			 {
				 printf("出口找到了,出口坐标(%d, %d)\n",x,y);
				 destroy_stack(p);
				 exit(0);
			 }
		 }
	 }
	 return false;
}
static bool by_way(LINKSTACH *p,char str[][14],int x,int y,int line ,int colu)
{
	
	push(p,x);//先行后列,入栈
	push(p,y);
	str[x][y] = '#';
	if(is_pathways(p,str,x-1,y,line ,colu)) //判断上是否通
	{
		push(p,x-1);
		push(p,y);
		str[x-1][y] = '#';
		show(str,line,colu);
		return true;
	}
	if(is_pathways(p,str,x,y-1,line ,colu))//判断左是否通
	{
		push(p,x);
		push(p,y-1);
		str[x][y-1] = '#';
		show(str,line,colu);
		return true;
	}
	if(is_pathways(p,str,x+1,y,line ,colu))//判断下是否通
	{
		push(p,x+1);
		push(p,y);
		str[x+1][y] = '#';
		show(str,line,colu);
		return true;
	}
	if(is_pathways(p,str,x,y+1,line ,colu))//判断右是否通
	{
		push(p,x);
		push(p,y+1);
		str[x][y+1] = '#';
		show(str,line,colu);
		return true;
	}
	return false;
}
void get_exter(char str[][14],int x,int y,int line ,int colu)
{
	
	int tmp;
	int a = 0;
	int b = 0;
	LINKSTACH *p = init_stack();
	if(str == NULL || line <= 0 || colu <= 0)
	{
		exit(-1);
	}
	if(str[x][y] == '1'||str[x][y] == '!')
	{
		printf("出口给出有问题,请仔细检查!\n");
		exit(0);
	}
	else
	{
		while(str[x][y] != '!')
		{
			bool f = by_way(p,str,x,y,line,colu);
			if(!f)
			{
				pop(p,&tmp);
				pop(p,&tmp);
			}
			pop(p,&y);//上下左右都走不通,那么就往回返,也就是pop,一个值。
			pop(p,&x);
		}
	}
	
}

MAIN.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vld.h>
#include"MAZE.h"

int main()
{
	char str[][14] = 
	{
		"1111111111111",
		"1011101111111",
		"1011101111111",
		"1000000000011",
		"10111111110!1",
		"1111111111111"
	};
	int colu = strlen(str[0]);//列
	int line = sizeof(str)/sizeof(str[0]);//行
	get_exter(str,1,1,line,colu);
	
	return 0;
}

在非递归i时用到了栈,我用的是链式栈,可以进入这个网页参考链式栈。 http://blog.youkuaiyun.com/qq_35256722/article/details/52915814

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值