c 语言...求急看迷宫程序~ ~ ~
#define MAXNUM 100/*堆栈中元素的最大数量*/
#定义N 11 /*地图的第一维长度*/
# include & ltstdio.h & gt
# include & ltstdlib.h & gt
typedef结构{
int x;/*行下标*/
int y;/*列下标*/
int d;/*运动方向*/
}数据类型;
Struct SeqStack {/*序列堆栈类型定义*/
int t;/*表示堆栈顶部的位置*/
数据类型s[MAXNUM];
};
typedef struct seq stack * PSeqStack;/*顺序堆栈类型的指针类型*/
PSeqStack pastack/* pastack是指向顺序堆栈的指针变量*/
PSeqStack createEmptyStack _ seq(void){
PSeqStack pastack
pastack =(PSeqStack)malloc(sizeof(struct seq stack));
if (pastack == NULL)
printf("空间不足!!\ n ");
其他
pastack-& gt;t =-1;
返回pastack
}
int isEmptyStack _ seq(PSeqStack pastack){
返回pastack-& gt;t = =-1;
}
/*推一个元素x */
void push_seq( PSeqStack pastack,数据类型x ) {
if(pastack-& gt;t & gt= MAXNUM - 1)
printf("溢出!\ n ");
否则{
pastack-& gt;t++;
pastack-& gt;s[pastack-& gt;t]= x;
}
}
/*删除栈顶元素*/
void pop_seq( PSeqStack pastack ) {
if(pastack-& gt;t == -1)
printf("下溢!\ n ");
其他
pastack-& gt;t-;
}
/*当pastack指向的堆栈不为空时,查找堆栈顶部元素的值*/
数据类型top_seq( PSeqStack pastack ) {
return(pastack-& gt;s[pastack-& gt;t]);
}
void pushtostack(PSeqStack st,int x,int y,int d) {
数据类型元素;
element.x = x
element.y = y
element.d = d
push_seq(st,element);
}
void printpath(PSeqStack st) {
数据类型元素;
printf(" reverse path is:\ n ");/*打印路径上的每个点*/
而(!isEmptyStack_seq(st)) {
element = top _ seq(ST);
pop _ seq(ST);
printf("节点是:%d %d \n ",element.x,element . y);
}
}
/*在迷宫中找到从入口迷宫[x1][y1]到出口迷宫[x2][y2]的路径*/
/*其中1
void mazePath(int maze[][N],int direction[][2],int x1,int y1,int x2,int y2) {
int i,j,k,g,h;
PSeqStack st
数据类型元素;
ST = createEmptyStack _ seq();
迷宫[x 1][y 1]= 2;/*从入口进入并标记*/
pushtostack(st,x1,y1,-1);/*堆栈的入口点*/
而(!Isompty stack _ seq (ST)) {/*不行的时候一步一步退*/
element = top _ seq(ST);
pop _ seq(ST);
i = element.xj = element.y
for(k = element . d+1;k & lt= 3;K++) {/*依次测试每个方向*/
g = i +方向[k][0];h = j +方向[k][1];
if(g = = x2 & amp;& amph = = y2 & amp& amp迷宫[g][h] == 0) {/*转到出口点*/
print path(ST);/*打印路径*/
返回;
}
If (maze[g][h] == 0) {/*去你没走过的地方*/
迷宫[g][h]= 2;/*标记*/
pushtostack(st,I,j,k);/*放入堆栈*/
I = g;j = h;k =-1;/*下一点转换为当前点*/
}
}
}
printf("未找到路径。\ n ");/*堆栈回滚后找不到路径*/
}
int main(){
int direction[][2]={0,1,1,0,0,-1,-1,0 };
int maze[][N] = {
1,1,1,1,1,1,1,1,1,1,1,
1,0,1,0,0,1,1,1,0,0,1,
1,0,0,0,0,0,1,0,0,1,1,
1,0,1,1,1,0,0,0,1,1,1,
1,0,0,0,1,0,1,1,0,1,1,
1,1,0,0,1,0,1,1,0,0,1,
1,1,1,0,0,0,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1,1
};
mazePath(迷宫,方向,1,1,6,9);
getchar();
返回0;
}