C语言迷宫代码分享

#include<stdio.h>
#include<stdbool.h>
#include <malloc.h>
#define M 8
#define N 8
#define maxsize 100
int mg[M+2][N+2]=
{
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},//0
{1, 0, 0, 1, 0, 0, 0, 1, 0, 1},//1
{1, 0, 0, 1, 0, 0, 0, 1, 0, 1},//2
{1, 0, 0, 0, 0, 1, 1, 0, 0, 1},//3
{1, 0, 1, 1, 1, 0, 0, 0, 0, 1},//4
{1, 0, 0, 0, 1, 0, 0, 0, 0, 1},//5
{1, 0, 1, 0, 0, 0, 1, 0, 0, 1},//6
{1, 0, 1, 1, 1, 0, 1, 1, 0, 1},//7
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1},//8
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},//9
}; //定义一个迷宫(二维数组),1表示有障碍物
typedef struct{
int i;
int j;
int di;
}box;
typedef struct{
box data[maxsize];
int top;
}StType;
bool mgpath(int xi , int yi, int xe, int ye)
{
int i, j, k, di, find;
StType st;
st.top = -1;
st.top++;
st.data[st.top].i=xi;
st.data[st.top].j=yi;
st.data[st.top].di = -1;
mg[xi][yi] = -1;
while(st.top > -1)
{
i = st.data[st.top].i;
j = st.data[st.top].j;
di = st.data[st.top].di;
if(i == xe && j == ye)
{
printf("迷宫路径为:\n");
for(k = 0; k<=st.top; k++)
{
printf("\t(%d, %d)", st.data[k].i,st.data[k].j);
if((k+1)%5 == 0)
printf("\n");
}
printf("\n");
return true;
}
find = 0;
while(di<4 && find == 0)
{
di++;
switch(di)
{
case 0:i = st.data[st.top].i-1; j = st.data[st.top].j; break;
case 1:i = st.data[st.top].i; j = st.data[st.top].j+1; break;
case 2:i = st.data[st.top].i+1; j = st.data[st.top].j; break;
case 3:i = st.data[st.top].i; j = st.data[st.top].j-1; break;
}
if(mg[i][j] == 0)
find=1;
}
if(find == 1)
{
st.data[st.top].di = di;
st.top++;
st.data[st.top].i = i;
st.data[st.top].j = j;
st.data[st.top].di = -1;
mg[i][j] = -1;
}
else
{
mg[st.data[st.top].i][st.data[st.top].j] = 0;
st.top--;
}
}
return false;
}
int main()
{
if(!mgpath(1, 1, M, N))
printf("Null!");
}

qq%e6%88%aa%e5%9b%be20161017223908


点赞

发表评论

在发表评论前请确认您的言论中没有违反中国各项法律、法规和违背社会道德的内容。任何无意义的留言内容都会被直接删除。