向高手请教~~~
这个是迷宫问题的C代码,在VC6.0平台中编译没有错误,但运行不了~
调试好象是动态存储有问题,但我一直找不到问题的根结~~
请各位大侠帮忙~~
# include <stdio.h>
# include <stdlib.h>
# define MAX 100
# define STACKINIT_SIZE 100
# define STACKINCREMENT 10
typedef struct{
int r,c;
} PosType;
typedef struct{
int step;
PosType seat;
int dir;
int pdir;
}ElemType;
typedef struct {
ElemType * top;
ElemType * base;
int size;
}Stack;
static Stack * MazePath(PosType,PosType ,int ,Stack *);
static void print(Stack, int, int c);
static ElemType Direct(ElemType, int );
static void InitStack(Stack *s)
{
s->top = s->base;
s->size= STACKINIT_SIZE;
}
static ElemType Pop(Stack * s)
{
ElemType e;
e = * (--s->top);
return e;
}
static void Push(Stack * s, ElemType e)
{
if(s->top-s->base>=s->size)
{
s->base=(ElemType*)realloc(s->base,(s->size+STACKINCREMENT)*sizeof(ElemType));
s->top=s->base+s->size;
s->size += STACKINCREMENT;
}
* (s->top++)=e;
}
static ElemType Direct(ElemType ccurpos, int p[][MAX])
{
int row, col, dir;
row=ccurpos.seat.r;
col=ccurpos.seat.c;
dir=ccurpos.dir;
switch (dir)
{
case 1 : if(p[row][col+1]==0)
{ccurpos.seat.c++;
ccurpos.step=0;
ccurpos.pdir=1;
}
else ccurpos.dir++;
break;
case 2 : if(p[row+1][col]==0)
{ccurpos.seat.r++;
ccurpos.step=0;
ccurpos.pdir=2;
}
else ccurpos.dir++;
break;
case 3 : if(p[row-1][col]==0)
{
ccurpos.seat.r--;
ccurpos.step=0;
ccurpos.pdir=3;
}
else ccurpos.dir++;
break;
case 4 : if(p[row][col-1]==0)
{
ccurpos.seat.c--;
ccurpos.step=0;
ccurpos.pdir=4;
}
break;
}
return ccurpos;
}
static Stack * MazePath(PosType start1,PosType end1,int array[][MAX],Stack * sq)
{
ElemType curpos1;
curpos1.seat.r=start1.r;
curpos1.seat.c=start1.c;
curpos1.step=1;
if (curpos1.seat.r==end1.r&&curpos1.seat.c==end1.c)
{Push( sq,curpos1);
return sq;
}
else {
do //做第一个格子
{ curpos1=Direct(curpos1,array);
}
while(curpos1.step!=0);
Push(sq, curpos1);
curpos1.pdir=curpos1.dir;
do
{
do
{
if(curpos1.dir==5-curpos1.pdir)
curpos1.dir++;
curpos1=Direct(curpos1,array);
}
while(curpos1.step!=0 && curpos1.dir<4);
if(curpos1.step==0)
{
Push( sq,curpos1);
curpos1.pdir=1;
}
else(curpos1.step==1);
{
curpos1.dir=Pop (sq).dir;
curpos1.pdir=Pop (sq).pdir;
curpos1.seat=Pop (sq).seat;
curpos1.step=Pop (sq).step;
}
}
while(curpos1.seat.r!=end1.r || curpos1.seat.c!=end1.c);
return sq;
}
}
static void print(Stack * sq, int r, int c)
{
char maze[MAX][MAX];
ElemType curpos;
int i, j;
do
{curpos=Pop(sq);
maze[curpos.seat.r][curpos.seat.c]='@';
}
while(sq->base!=sq->top);
for (i=0;i<r;i++)
for(j=0;j<c;j++)
{
if(maze[curpos.seat.r][curpos.seat.c]=='@')
continue;
else maze[i][j]='#';
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("\t%c ",maze[i][j]);
printf("\n");
}
}
void main()
{
int maze[MAX][MAX], row, col, i, j;
PosType start,end;
Stack * sq, * sqq;
sq->base=(ElemType *)malloc(STACKINIT_SIZE * sizeof(ElemType));
InitStack (sq);
scanf("%d",& row);
scanf("%d",& col);
for (i=0; i<row; i++)
for(j=0; j<col; j++)
{
printf("\n第%d行第%d列位置",i,j);
scanf("%d",&maze[i][j]);
}
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
printf("\t%d ",maze[i][j]);
printf("\n");
}
sqq->top = MazePath (start, end, maze, sq)->top;
sqq->base = MazePath (start, end, maze, sq)->base;
sqq->size = MazePath (start, end, maze, sq)->size;
print(sqq,row,col);
}