数据结构迷宫问题的C代码编译一直有错,请高手赐教~~

sosboy923 2004-11-20 08:43:35
# 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();

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)
{
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,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=Pop (sq);
}
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");
}
MazePath(start, end, maze, sq);
sqq = MazePath;
print(sqq,row,col);
}

...全文
147 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ff44023154 2004-11-26
  • 打赏
  • 举报
回复
这个是我编的迷宫程序,感觉基本上和你差不多,我已经调试通过了,整个迷宫放在maze.txt里面!
你可以去看看!或许对你有帮助!
#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
#define ok 1
#define true 1
#define false 0
#define m 4
#define n 4
#define stack_init_size 100
#define stackincrement 10

typedef struct{
int x;
int y;
}postype;
typedef struct{
int ord;
postype seat;
int di;
} selemtype,*stype;
typedef struct{
int content;
int footprint;
int markprint;
} valuetype;
typedef struct{
selemtype *base;
selemtype *top;
int stacksize;
} sqstack;

void initstack(sqstack *s)
{
s->base=(selemtype *)malloc(stack_init_size * sizeof(selemtype));
if(s->base==NULL) {printf("initstack overfow!");exit(0);}
s->top=s->base;
s->stacksize=stack_init_size;
}

void push(sqstack *s,selemtype e)
{
if(((s->top)-(s->base))>=s->stacksize)
{
s->base=(selemtype *)realloc(s->base,(s->stacksize+stackincrement)*sizeof(selemtype));
if((s->base)==NULL){printf("increasement overlow!");exit(0);}
s->top=s->base+s->stacksize;
s->stacksize+=stackincrement;
}
*(s->top)=e;
s->top=(s->top)+1;
}

selemtype pop(sqstack *s)
{
selemtype e;
if(s->top==s->base) printf(" the stack is empty!");
s->top=(s->top)-1;
e=*(s->top);
return e;
}

int stackempty(sqstack *s)
{
if(s->top==s->base)
return true;
else return false;
}

postype nextpos(postype curpos,int pi)
{
switch(pi)
{
case 1: curpos.y=curpos.y+1;break;
case 2: curpos.x=curpos.x+1;break;
case 3: curpos.y=curpos.y-1;break;
case 4: curpos.x=curpos.x-1;break;
default: printf("nextpos error!");
}
return curpos;
}

void footprint(postype curpos,valuetype maze_content[m][n])
{
int i,j;
i=curpos.x;
j=curpos.y;
maze_content[i][j].footprint=1;
}

void markprint(postype curpos,valuetype maze_content[m][n])
{
int i,j;
i=curpos.x;
j=curpos.y;
maze_content[i][j].markprint=1;
}

int pass(postype curpos,valuetype maze_content[m][n])
{
int i,j;
i=curpos.x;
j=curpos.y;
if((0<=curpos.x<m)&&(0<=curpos.y<n)&&maze_content[i][j].footprint==0&&maze_content[i][j].markprint==0&&maze_content[i][j].content==0)return true;
else return false;
}

int mazepath(valuetype maze_content[m][n],postype start,postype end,sqstack *s)
{
selemtype e;
postype curpos,position;
int curstep;
curpos.x=start.x;
curpos.y=start.y;
curstep=1;
do{
if(pass(curpos,maze_content))
{
footprint(curpos,maze_content);
e.ord=curstep;
e.seat.x=curpos.x;
e.seat.y=curpos.y;
e.di=1;
push(s,e);
if(curpos.x==end.x&&curpos.y==end.y){return true;exit(0); }
curpos=nextpos(curpos,1);
curstep++;
}
else
{
if(stackempty(s)!=true)
{
pop(s);
while(e.di==4&&stackempty(s)==false)
{
position.x=e.seat.x;
position.y=e.seat.y;
markprint(position,maze_content);
pop(s);
}

if(e.di<4){e.di++;push(s,e); position.x=e.seat.x;position.y=e.seat.y; curpos=nextpos(position ,e.di);}
}
}
}while(stackempty(s)==false);
return false;
}

void value(valuetype maze_content[m][n])
{
FILE *fp;
int i,j,k;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
maze_content[i][j].footprint=0;
maze_content[i][j].markprint=0;
}
if((fp=fopen("e:/turboc2/maze.txt","r"))==NULL)
{printf("can not open the file!\n");exit(0);}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{fscanf(fp,"%d",&k);
maze_content[i][j].content=k;
}
fclose(fp);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf(" %d ",maze_content[i][j].content);
if(j==3)printf("\n");
}
}

main()
{
int i,j;
sqstack *s;
valuetype maze_content[m][n];
selemtype f;
postype start,end;
clrscr();
initstack(s);
start.x=0;
start.y=0;
end.x=3;
end.y=3;
value(maze_content);
if(mazepath(maze_content,start,end,s)==false)
printf("no way to get out!");
else
{printf("end->");
while(stackempty(s)!=true)
{f=pop(s);
printf("(%d,%d)->",(f.seat.x),(f.seat.y));
}
printf("start");
}
printf("\n");
}























yyh21m 2004-11-22
  • 打赏
  • 举报
回复
不知道,帮顶&蹭分
picoolo1124 2004-11-22
  • 打赏
  • 举报
回复
132:else(curpos1.step==1)
=====> else if(curpos1.step==1)

167: int maze[MAX][MAX], row, col, i, j;
=====> int maze[MAX][MAX], row, col, i, j,*a=maze[0];
186: MazePath(start, end, maze, sq);
=====> MazePath(start, end, &a, sq);

187: sqq = MazePath;
=====> sqq =(Stack *) MazePath;

15,440

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧