主题:迷宫和显示的问题!
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLLOW 0
#define INFEASIBLE -1
#define STACK_INIT_SIZE 500
#define STACKINCREMENT 10
#define MAXSTRLEN 255
#define N 10
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int status;
typedef char ELEMTYPE;
typedef char mazetype;
typedef struct{
int c;
int r;
}postype;
typedef struct{
int ord;
postype seat;
int di;
}SELEMTYPE;
typedef struct{
SELEMTYPE *base;
SELEMTYPE *top;
int stacksize;
}*sqstack;
status initstack(sqstack *s)
{(*s)->base=(SELEMTYPE*)malloc(STACK_INIT_SIZE*sizeof(SELEMTYPE));
if(!(*s)->base)exit(OVERFLLOW);
(*s)->top=(*s)->base;
(*s)->stacksize=STACK_INIT_SIZE;
return(OK);
}
status push(sqstack s,SELEMTYPE e)
{while(s->top-s->base>=s->stacksize)
{s->base=(SELEMTYPE*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(SELEMTYPE));
if(!s->base)exit(OVERFLLOW);
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*(s->top)=e;
s->top++;
return(OK);
}
status pop(sqstack s,SELEMTYPE *e)
{if(s->top==s->base)return(ERROR);
else {
--s->top;
*e=*(s->top);
return(OK);
}
}
status stackempty(sqstack s)
{if(s->top==s->base)return(OK);
else return(ERROR);
}
postype nextpos(postype q, int n)
{
postype p;
switch (n) {
case 1:
p.r=q.r;
p.c=q.c+1;
break;
case 2:
p.r=q.r+1;
p.c=q.c;
break;
case 3:
p.r=q.r;
p.c=q.c-1;
break;
case 4:
p.r=q.r-1;
p.c=q.c;
break;
}
return p;
}
status pass(mazetype maze[N][N],postype curpos)
{
if (maze[curpos.r][curpos.c]==' ')
return 1;
else return 0;
}
void footprint(mazetype maze[N][N],postype curpos)
{
maze[curpos.r][curpos.c]='1';
}
void markprint(mazetype maze[N][N],postype curpos)
{
maze[curpos.r][curpos.c]='0';
}
void scanmaze(mazetype maze[N][N])
{int i;
for(i=0;i<N;i++)
gets(maze[i]);
printf("\n");
}
void printmaze(mazetype maze[N][N])
{int i,j;
for(i=0;i<N;i++)
{for(j=0;j<N;j++)
if(maze[i][j]=='0')
printf("%c",' ');
else
printf("%c",maze[i][j]);
printf("\n");
}
}
void mazepath(mazetype maze[N][N],postype start,postype end)
{
sqstack S;
postype curpos;
int curstep=1;
SELEMTYPE e;
initstack(&S);
curpos=start;
do {
if(pass(maze,curpos))
{
footprint(maze,curpos);
e.di=1;
e.ord=curstep;
e.seat=curpos;
push(S,e);
if (curpos.r==end.r&&curpos.c==end.c)
{printf("迷宫的走法如下:\n");
printmaze(maze);
exit(0);
}
curpos=nextpos(curpos,1);
curstep++;
}
else
{if(!stackempty(S))
{pop(S,&e);
while(e.di==4&&!stackempty(S))
{markprint(maze,e.seat);
pop(S,&e);
}
if(e.di<4){e.di++;
push(S,e);
curpos=nextpos(e.seat,e.di);
}
}
}
} while(!stackempty(S));
printf("此迷宫走不通!\n");
}
void main()
{postype start,end;
mazetype maze[N][N]={"##########",
"# # # #",
"# # # #",
"# ## #",
"# ### #",
"# # #",
"# # # #",
"# ### ## #",
"## #",
"##########",};
printf("迷宫如下:\n");/*为什么在执行程序时不能显示这一句话啊?*/
printmaze(maze);
start.r=1;
start.c=1;
end.r=N-2;
end.c=N-2;
mazepath(maze,start,end);
}
为什么在主函数中不能显示printf中的语句啊?
谢谢高手指点!
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLLOW 0
#define INFEASIBLE -1
#define STACK_INIT_SIZE 500
#define STACKINCREMENT 10
#define MAXSTRLEN 255
#define N 10
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int status;
typedef char ELEMTYPE;
typedef char mazetype;
typedef struct{
int c;
int r;
}postype;
typedef struct{
int ord;
postype seat;
int di;
}SELEMTYPE;
typedef struct{
SELEMTYPE *base;
SELEMTYPE *top;
int stacksize;
}*sqstack;
status initstack(sqstack *s)
{(*s)->base=(SELEMTYPE*)malloc(STACK_INIT_SIZE*sizeof(SELEMTYPE));
if(!(*s)->base)exit(OVERFLLOW);
(*s)->top=(*s)->base;
(*s)->stacksize=STACK_INIT_SIZE;
return(OK);
}
status push(sqstack s,SELEMTYPE e)
{while(s->top-s->base>=s->stacksize)
{s->base=(SELEMTYPE*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(SELEMTYPE));
if(!s->base)exit(OVERFLLOW);
s->top=s->base+s->stacksize;
s->stacksize+=STACKINCREMENT;
}
*(s->top)=e;
s->top++;
return(OK);
}
status pop(sqstack s,SELEMTYPE *e)
{if(s->top==s->base)return(ERROR);
else {
--s->top;
*e=*(s->top);
return(OK);
}
}
status stackempty(sqstack s)
{if(s->top==s->base)return(OK);
else return(ERROR);
}
postype nextpos(postype q, int n)
{
postype p;
switch (n) {
case 1:
p.r=q.r;
p.c=q.c+1;
break;
case 2:
p.r=q.r+1;
p.c=q.c;
break;
case 3:
p.r=q.r;
p.c=q.c-1;
break;
case 4:
p.r=q.r-1;
p.c=q.c;
break;
}
return p;
}
status pass(mazetype maze[N][N],postype curpos)
{
if (maze[curpos.r][curpos.c]==' ')
return 1;
else return 0;
}
void footprint(mazetype maze[N][N],postype curpos)
{
maze[curpos.r][curpos.c]='1';
}
void markprint(mazetype maze[N][N],postype curpos)
{
maze[curpos.r][curpos.c]='0';
}
void scanmaze(mazetype maze[N][N])
{int i;
for(i=0;i<N;i++)
gets(maze[i]);
printf("\n");
}
void printmaze(mazetype maze[N][N])
{int i,j;
for(i=0;i<N;i++)
{for(j=0;j<N;j++)
if(maze[i][j]=='0')
printf("%c",' ');
else
printf("%c",maze[i][j]);
printf("\n");
}
}
void mazepath(mazetype maze[N][N],postype start,postype end)
{
sqstack S;
postype curpos;
int curstep=1;
SELEMTYPE e;
initstack(&S);
curpos=start;
do {
if(pass(maze,curpos))
{
footprint(maze,curpos);
e.di=1;
e.ord=curstep;
e.seat=curpos;
push(S,e);
if (curpos.r==end.r&&curpos.c==end.c)
{printf("迷宫的走法如下:\n");
printmaze(maze);
exit(0);
}
curpos=nextpos(curpos,1);
curstep++;
}
else
{if(!stackempty(S))
{pop(S,&e);
while(e.di==4&&!stackempty(S))
{markprint(maze,e.seat);
pop(S,&e);
}
if(e.di<4){e.di++;
push(S,e);
curpos=nextpos(e.seat,e.di);
}
}
}
} while(!stackempty(S));
printf("此迷宫走不通!\n");
}
void main()
{postype start,end;
mazetype maze[N][N]={"##########",
"# # # #",
"# # # #",
"# ## #",
"# ### #",
"# # #",
"# # # #",
"# ### ## #",
"## #",
"##########",};
printf("迷宫如下:\n");/*为什么在执行程序时不能显示这一句话啊?*/
printmaze(maze);
start.r=1;
start.c=1;
end.r=N-2;
end.c=N-2;
mazepath(maze,start,end);
}
为什么在主函数中不能显示printf中的语句啊?
谢谢高手指点!