该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
#include
#include
#include
#include
#define H 20
#define L 21
#define STACK_INIT_SIZE 400
#define STACKINCREMENT 10
int e;
#define OK 1
#define OVERFLOW 0
#define ERROR 0
typedef int SElemType;
typedef struct
{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
struct Position
{ int px;
int py;
}position;
void CreateStack(SqStack *S,SElemType e)
{
S->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S->base) printf("创建失败") ;
else
{
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
*(S->top)=e;
S->top++;
}
}
int 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) return OVERFLOW;
else{
S->stacksize+=STACKINCREMENT;
*(S->top)=e;
S->top++;
return OK;
}
}
else
{*(S->top)=e;
S->top++;
return OK;
}
}
int Pop(SqStack *S,SElemType e)
{
if(S->top==S->base) printf("ERROR");
else
{
e=*--S->top;
return OK;
}
}
FILE *p;
char maze[H][L];
const int dir1[4]={1,0,-1,0},
dir2[4]={0,1,0,-1};
int i,j,x,y;bool found =0;
main()
{void pr(void);
void dir(int i,int j);
if((p=fopen("h:\\maze.txt","r"))==NULL)
{
printf("打不开文件");
exit(0);
}
for(i=0;i
{
for(j=0;j
{
fscanf(p,"%c",&maze[i][j]);
if(maze[i][j]=='S')
{
x=i;
y=j;
}
}
}
dir(x,y);
return 0;
}
void pr(void)
{
for(i=0;i
{
for(j=0;j
{
printf("%c",maze[i][j]);
}
}
}
void dir(int x,int y)
{
int k;
SqStack stack,*S;
S=&stack;
position.px=x;
position.py=y;
CreateStack(S,position);
if(maze[i][j]!='E')
{
for(k=0;k<4;k++)
if(x +dir1[k]>=0 && y+dir1[k]=0 && y+dir2[k]
{maze[x+dir1[k]][y+dir2[k]]='@';
pr();
Sleep(100);
system("cls");
dir(x+dir1[k],y+dir2[k]);
}
}
}
目前程序就是把迷宫所有路径替换成@而已 没有真正实现一条路
想加入栈 让那种走不通的路不显示
但是老是提示不正确