迷宫结构如下:
定义三个结构体,
#define _size 100 //对该迷宫,100个够用了
#define add_size 10 //每次要增加的大小
#define endFlag NULL //出错时返回的标志
#define row 8 //迷宫的行的大小,对应i
#define col 8 //迷宫的列的大小,对应j
typedef struct _list{
int data;
struct _list *next;
}list,*LinkList;
//一个节点的结构
typedef struct{
int x; //当前位置的x
int y; //当前位置的y
int status; //0表示没遍历过,1表示已经遍历过,-1表示不能通过
LinkList drit; //周围可以遍历的方向,1,2,3,4分别表示东南西北,0表示全部遍历过
}*node,_Node;
typedef struct{
node *base;
node *top;
int size;
}*stack,st;
int m[row][col] //迷宫布局,全局变量
= {
0,0,1,0,0,0,1,0,
0,0,1,0,0,0,1,0,
0,0,0,0,1,1,0,0,
0,1,1,1,0,0,0,0,
0,0,0,1,0,0,0,0,
0,1,0,0,0,1,0,0,
0,1,1,1,0,1,1,0,
1,0,0,0,0,0,0,0
};
全部代码如下:
#include "stdio.h"
#include "stdlib.h"
/*
* 用栈解决迷宫问题
*/
#define _size 100 //对该迷宫,100个够用了
#define add_size 10 //每次要增加的大小
#define endFlag NULL //出错时返回的标志
#define row 8 //迷宫的行的大小,对应i
#define col 8 //迷宫的列的大小,对应j
typedef struct _list{
int data;
struct _list *next;
}list,*LinkList;
//一个节点的结构
typedef struct{
int x; //当前位置的x
int y; //当前位置的y
int status; //0表示没遍历过,1表示已经遍历过,-1表示不能通过
LinkList drit; //周围可以遍历的方向,1,2,3,4分别表示东南西北,0表示全部遍历过
}*node,_Node;
typedef struct{
node *base;
node *top;
int size;
}*stack,st;
int m[row][col] //迷宫布局,全局变量
= {
0,0,1,0,0,0,1,0,
0,0,1,0,0,0,1,0,
0,0,0,0,1,1,0,0,
0,1,1,1,0,0,0,0,
0,0,0,1,0,0,0,0,
0,1,0,0,0,1,0,0,
0,1,1,1,0,1,1,0,
1,0,0,0,0,0,0,0
};
void InitList(LinkList *L);
void addList(LinkList L,int value);
void delList(LinkList *L);
void createNode(_Node _p[][col],int i,int j);//创立一个节点
void InitStack(stack *S); //初始化栈
void DestroyStack(stack *S);//销毁栈
void push(stack st,node *e);//插入元素
node* pop(stack st); //弹出栈顶元素
node* getTop(stack st); //获得栈顶元素,并不删除
int length(stack st); //返回栈的元素个数
int isEmpty(stack st); //是否为空,c语言中没有bool类型
void InitList(LinkList *L){
(*L) = (LinkList)malloc(sizeof(list));
if(!*L){
printf("LinkList malloc failed!");
return;
}
(*L)->data = 0;
(*L)->next = NULL;
}
void addList(LinkList L,int value){
LinkList p;
while(1){
if(L->next == 0){
p = (LinkList)malloc(sizeof(list));
p->data = 0;
p->next = NULL;
L->data = value;
L->next = p;
return;
}
L = L->next;
}
}
//删除链表的头节点
void delList(LinkList *L){
LinkList p = (*L)->next;
free(*L);
*L = p;
}
void createNode(_Node _p[][col],int i,int j){
node p = *( _p + i) + j;
if(!p){
printf("node malloc failed!");
return;
}
p->x = i;
p->y = j;
p->status = 0;
InitList(&p->drit);
if( j < 7 && m[i][j+1] == 0)
addList(p->drit,1);
if( i < 7 && m[i+1][j] == 0)
addList(p->drit,2);
if( j > 0 && m[i][j-1] == 0)
addList(p->drit,3);
if( i > 0 && m[i-1][j] == 0)
addList(p->drit,4);
}
void InitStack(stack *S){
(*S) = (stack)malloc(sizeof(st)); //先为栈的结构体分配空间
(*S)->base = (node *)malloc(_size * sizeof(_Node) ); //再为栈里面的数组分配空间
if(!*(*S)->base){
printf("Stack malloc failed!");
return;
}
(*S)->top = (*S)->base;
(*S)->size = _size;
}
void DestroyStack(stack *S){
node* temp;
while((*S)->top > (*S)->base){
temp = (*S)->top - 1;
free((*S)->top);
(*S)->top = temp;
}
free((*S)->base);
free(*S);
}
void push(stack st,node *e){
if( (st->top - st->base) >= st->size){
st->base = (node *)realloc(st->base,(st->size + add_size)*sizeof(_Node));
st->size += add_size;
}
if(!st->base){
printf("Remalloc failed!");
return;
}
st->top++;
*st->top = *e;
}
node* pop(stack st){
node* e;
if(st->top == st->base)
return endFlag; //Error
e = st->top--; //equels to e= st->top;st->top--;
return e;
}
node* getTop(stack st){
if(st->top == st->base)
return endFlag; //Error
return st->top;
}
int isEmpty(stack st){
if(length(st) == 0)
return 1;
return 0;
}
int length(stack st){
return st->top - st->base;
}
int main(){
stack st;
node p;
_Node maze[row][col]; //创建结构体数组
int i,j;
for(i=0;i<row;i++) //初始化结构体
for(j=0;j<col;j++)
createNode(maze,i,j);
InitStack(&st);
p = &maze[0][0];
push(st,&p);
(*st->top)->status = 1;
while(!isEmpty(st)){
if((*st->top)->x == row -1 && (*st->top)->y == col -1)
break;
switch((*st->top)->drit->data){
case 1: //右
delList(&(*st->top)->drit); //将当前的右方向删掉
p = &maze[(*st->top)->x][(*st->top)->y + 1];
if(p->status == 0){ //表示下一个点没有在路径里面,或者不能走
push(st,&p);
(*st->top)->status = 1;
}
break;
case 2: //下
delList(&(*st->top)->drit);
p = &maze[(*st->top)->x + 1][(*st->top)->y];
if(p->status == 0){
push(st,&p);
(*st->top)->status = 1;
}
break;
case 3: //左
delList(&(*st->top)->drit);
p = &maze[(*st->top)->x][(*st->top)->y - 1];
if(p->status == 0){
push(st,&p);
(*st->top)->status = 1;
}
break;
case 4: //上
delList(&(*st->top)->drit);
p = &maze[(*st->top)->x - 1][(*st->top)->y];
if(p->status == 0){
push(st,&p);
(*st->top)->status = 1;
}
break;
default: //没有可以遍历的方向
(*st->top)->status = -1; //表示当前点不能走
pop(st);
}
}
if(isEmpty(st))
{
printf("没有找到路径\n");
return 0;
}
i =1,j=5;
while(!isEmpty(st)){
printf("(%d,%d) <- ",(*st->top)->x,(*st->top)->y);
pop(st);
if(i++%j == 0)
printf("\n");
}
printf("enter\n");
return 0;
}
程序输出的路径如下: