C语言 迷宫求解 关于函数的执行
关于迷宫这一块的函数,能不能帮忙修改一下?谢谢各位大神!!!就是红色字体的那一块???
谢谢!!
#include<stdio.h>
#include<stdlib.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 2
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef struct
{
int x ;
int y ;
int di;
}PosType;//定义结构体(坐标x,坐标y,方向di)
typedef struct
{
PosType *base;
PosType *top;
int stacksize;
}SqStack;
int InitStack(SqStack *S)
{
S->base =(PosType *)malloc(STACK_INIT_SIZE * sizeof(PosType));
if(!S->base) exit(0);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return OK;
}
int DestroyStack(SqStack * S)
{
free(S->base);
S->base=NULL;
S->top=NULL;
S->stacksize=0;
return OK;
}
int ClearStack(SqStack *S)
{
S->top=S->base;
return OK;
}
int StackEmpty(SqStack *S)
{
if(S->top==S->base)return TRUE;
else return FALSE;
}
int GetTop(SqStack *S,PosType *e)
{
if(S->top==S->base)
return ERROR;
*e=*(S->top-1);
return OK;
}
int push(SqStack *S,PosType* e)
{
if(S->top - S->base >= S->stacksize)
{
S->base=(PosType *)realloc(S->base,(S->stacksize+STACKINCREMENT)* sizeof(PosType));
if(!S->base) exit(-1);
S->top=S->base+S->stacksize;
S->stacksize +=STACKINCREMENT;
}
*S->top++=*e;
return OK;
}
int pop (SqStack *S,PosType *e)
{
if(S->top==S->base)
return ERROR;
*e=*--S->top;
return OK;
}
void reverseprint(SqStack *S,PosType *end)//从栈的底部开始输出(坐标,坐标,方向)
{
while(S->base!=S->top)
{
printf("(%d,%d,%d)->",S->base->x,S->base->y,S->base->di);
S->base++;
}
printf("(%d,%d,%d)\n",end->x,end->y,end->di);
}
int Migong(int *p,SqStack *S,PosType *start,PosType *end,int e,int r)// 迷宫
{
static int i=1;//确定有几条路可以
PosType a;
a = *start;
do{
if( *(p +a.x*e+a.y)==0 )//判断坐标所在的位置是不是通路
{
push(S, &a);//存入栈
if(a.x == end->x && end->y == a.y )//判断是否到出口了
{
printf("第%d条路径:",i++);
reverseprint(S,end);
return TRUE;
}
a.y=a.y+1;//向右移动一格
a.di=1;//方向向右
}
else
{
if(!StackEmpty(S))
{
pop(S,&a);//后退一格
while(a.di == 4 && !StackEmpty(S))
{
pop(S,&a);//无路可走,后退一格
}
if(a.di<4)
{
a.di++;//方向转换:1为向右,2向下,3向左,4向右
push(S,&a);
switch(a.di)
{
case 2:a.x=a.x+1;break;//向下时,横坐标加1
case 3:a.y=a.y-1;break;
case 4:a.x=a.x-1;break;
}}
}
}
}while(!StackEmpty(S));
return FALSE;
}
void main()
{
int i,j,r1,r2;
int a,b,c,d;
int *p;
int mg[10][10];
SqStack *S;
PosType *m,*n;
S = (SqStack *)malloc(sizeof(SqStack));
InitStack(S);
printf("请输入迷宫的长和宽(a,b)\n");
scanf("%d,%d",&r1,&r2);
printf("请输入迷宫的图案(通:0 ;不通:1 )\n例如:\n1 0 0 \n0 1 1 \n0 0 1\n");
for(i=1;i<=r1;i++)
for(j=1;j<=r2;j++)
{
scanf("%d",&mg[i][j]);
}
for(i=0;i<=r1+1;i++)
{
mg[i][0]=1;
mg[i][r2+1]=1;
}
for(j=0;j<=r2+1;j++)
{
mg[0][j]=1;
mg[r1+1][j]=1;
}
p=&mg[0][0];
printf("请输入迷宫的入口((a,b))\n");
scanf("%d,%d",&a,&b);
printf("请输入迷宫的出口((a,b))\n");
scanf("%d,%d",&c,&d);
m = (PosType *)malloc(sizeof(PosType));
n = (PosType *)malloc(sizeof(PosType));
m->x=a;
m->y=b;
n->x=c;
n->y=d;
m->di=1;
if(!Migong(p,S,m,n,r1,r2))
printf("走不通\n");
[ 本帖最后由 樱花自在 于 2013-7-12 23:41 编辑 ]