迷宫可以说,是我们小时候超级喜欢玩的游戏,怎么玩,大家都很清楚。
介绍: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