有三个主要的问题:1,num=(800*(i+j)+1500)%327;为什么要选用这样的数字.
2,其中的if(i!=s->top)
printf("-->");这是什么意思.
3,就是标题的问题,不能运行.
在这里麻烦大侠们了
#define M2 12
#define N2 11
#define Null 0
#define MAXLEN M2
#define True 1
#define False 0
#include <stdio.h>
int M=M2-2,N=N2-2;
typedef struct
{
int x,y,dir;
}elemtype;
typedef struct
{
elemtype stack[MAXLEN];
int top;
}sqstktp;
struct moved
{
int dx,dy;
};
void inimaze(int maze[][N2])
{
int i,j,num;
for(i=1;i<=M;i++)
{
for(j=1;j<=N;j++)
{
num=(800*(i+j)+1500)%327;
if((num<150)&&(i!=M||j!=N))
maze[i][j]=1;
else
maze[i][j]=0;
printf("%3d",maze[i][j]);
}
printf("\n");
}
printf("\n");
for(i=0,j=0;i<=M+1;i++)
maze[i][j]=1;
for(i=0,j=N+1;i<=M+1;j++)
maze[i][j]=1;
for(i=0,j=0;j<=N+1;j++)
maze[i][j]=1;
for(i=M+1,j=0;j<=N+1;j++)
maze[i][j]=1;
}
void inimove(struct moved move[])
{
move[0].dx=0;move[0].dy=1;
move[1].dx=1;move[1].dy=1;
move[2].dx=1;move[2].dy=-1;
move[3].dx=0;move[3].dy=-1;
move[4].dx=0;move[4].dy=-1;
move[5].dx=-1;move[5].dy=-1;
move[6].dx=-1;move[6].dy=0;
move[7].dx=-1;move[7].dy=1;
}
void inistack(sqstktp *s)
{
s->top=-1;
}
int push(sqstktp *s,elemtype x)
{
if(s->top==MAXLEN-1)
return(False);
else
{
s->stack[++s->top]=x;
return(True);
}
}
elemtype pop(sqstktp *s)
{
elemtype elem;
if(s->top<0)
{
elem.x=Null;
elem.y=Null;
elem.dir=Null;
return(elem);
}
else
{
s->top--;
return(s->stack[s->top+1]);
}
}
void path(int maze[][N2],struct moved move[],sqstktp *s)
{
int i,j,dir,x,y,f;
elemtype elem;
i=1;j=1;dir=0;
maze[1][1]=-1;
do
{
x=i+move[dir].dx;
y=j+move[dir].dy;
if(maze[x][y]==0)
{
elem.x=i;
elem.y=j;
elem.dir=dir;
f=push(s,elem);
if(f==False)
printf("the stack is too short!\n");
i=x;
j=y;
dir=0;
maze[x][y]=-1;
}
else
if(dir<7)
dir++;
else
{
elem=pop(s);
if(elem.x!=Null)
{
i=elem.x;
j=elem.y;
dir=elem.dir+1;
}
}
}while(!((s->top==-1)&&(dir>7)||(x==M)&&(y==N)&&(maze[x][y]==-1)));
if(s->top==-1)
printf("the road have not\n");
else
{
elem.x=x;
elem.y=y;
elem.dir=dir;
f=push(s,elem);
i=0;
while(i<=s->top)
{
printf("%d,%d",s->stack[i].x,s->stack[i].y);
if(i!=s->top)
printf("-->");
if((i+1)%4==0)
printf("\n");
i++;
}
printf("\n");
}
}
void draw(int maze[][N2],sqstktp *s)
{
int i,j;
elemtype elem;
for(i=1;i<=M;i++)
for(j=1;j<=N;j++)
if(maze[i][j]==-1)
maze[i][j]=0;
while(s->top>-1)
{
elem=pop(s);
i=elem.x;
j=elem.y;
maze[i][j]=0;
}
for(i=1;i<=M;i++)
{
for(j=1;j<=N;j++)
printf("%3d",maze[i][j]);
printf("\n");
}
printf("\n");
}
void main()
{
sqstktp *s;
int maze[M2][N2];
struct moved move[8];
inimaze(maze);
s=(sqstktp *)malloc(sizeof(sqstktp));
inistack(s);
inimove(move);
path(maze,move,s);
draw(maze,s);
}