C语言版迷宫问题

如题所述

我已经调试好啦,输出的是汉字不是英文哦。
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#include<time.h>
#define OK 1
#define FLASE 0
#define OVERFLOW -1
#define ERROR 0
#define TURE 1
#define MAX 100
typedef int Status;
typedef struct{
int x;
int y;
}PosType;
typedef struct
{ int ord; //路中次序
PosType seat; //迷宫中路的坐标
int direaction; //方向
}SElemType;
typedef struct
{ int data;
int pass;
int nopa;
}Maze;
Maze Type[MAX][MAX];

typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S)
{
S.base=(SElemType*)malloc(sizeof(SElemType)*MAX);
if(!S.base)exit(OVERFLOW);
S.top=S.base;
S.stacksize=100;
return OK;
}
Status Push(SqStack &S,SElemType e)
{
if(S.top-S.base>=S.stacksize)
{ S.base=(SElemType*)realloc(S.base,(S.stacksize+10)*sizeof(SElemType));
if(!S.base)exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=10;

}
*S.top++=e;
return OK;

}//push

Status Pop(SqStack &S,SElemType &e)
{
if(S.top=S.base)return ERROR;
e=*--S.top;
return OK;
}//Pop
Status StackEmpty(SqStack S)
{
if(S.top-S.base==0)
return OK;
else
return FLASE;
}

Status Pass (Maze maze,PosType curpos)
{
int x,y;
x=curpos.x;
y=curpos.y;
if(x>=0 && x<10 &&y>=0&&y<10)
{
if(Type[x][y].data==0 && Type[x][y].pass==0 && Type[x][y].nopa==0)
return OK;
else
return FLASE;
}
else
return FLASE;
}

Status MazePath(Maze Typ,SqStack &S,PosType start,PosType end)
{
SElemType e;
PosType curpos;
InitStack(S); //curpos=start;
curpos.x=start.x;
curpos.y=start.y;
//把开始位置赋给 curpos;
int curstept=1;
do{
if(Pass(Type[e.seat.x][e.seat.y],curpos))
{
printf("\ncurpos.x=%d,curpos.y=%d\n",curpos.x,curpos.y);
e.ord=curstept;
e.seat.x=curpos.x;
e.seat.y=curpos.y;
e.direaction=1;
Push(S,e);

Type[e.seat.x][e.seat.y].pass=1;
if(curpos.x==end.x && curpos.y==end.y)
{printf("\n*****找到路径*****\n");return TURE;
++curpos.y;//直向东面的位置
++curstept;
}//if
else
{
if(!StackEmpty(S))
{
Pop(S,e);
while(e.direaction==4 && !StackEmpty(S))
{

Type[e.seat.x][e.seat.y].pass=0;
Type[e.seat.x][e.seat.y].nopa=1;

Pop(S,e);
}//while
}
if(e.direaction<4)
{
++e.direaction;

Push(S,e);
int n;
n=e.direaction;
//printf("\n当前点的是(%d,%d)方向是%d",e.seat.x,e.seat.y,e.direaction);

switch(n)
{
case 2:
curpos.x=e.seat.x+1;curpos.y=e.seat.y;break;
case 3:
curpos.x=e.seat.x;curpos.y=e.seat.y-1;break;
case 4:
curpos.x=e.seat.x-1;curpos.y=e.seat.y;break;

}//swich
//printf("\n变向后点的是(%d,%d)",curpos.x,curpos,y);
}//if
}//if
}//else
}while (!StackEmpty(S));
{
printf("找不到路径");
return FLASE;
}
}

void MazePrint(Maze maze,int r,int c)
{
printf("0 1 2 3 4 5 6 7 8 9");
printf("\n");
for(int i=0;i<r;++i)
{
printf("%d ",i);
for (int j=0;j<c;++j)
if (Type[i][j].data==1)
printf ("1 ");
else
printf("0 ");
printf("\n");

}
}

void Printe(Maze maze ,int r,int c)
{
for(int i=0;i<r;++i)
{
for (int j=0;j<c;++j)
printf("%d",Type[i][j].data);
printf("\n");
}
}

void Print(Maze maze,SqStack S,int r,int c)
{
SElemType e;
while(!StackEmpty(S))
{
Pop(S,e);
int n;
n=e.direaction;
switch(n)
{
case 1:
Type[e.seat.x][e.seat.y].data=11;break;
case 2:
Type[e.seat.x][e.seat.y].data=12;break;
case 3:
Type[e.seat.x][e.seat.y].data=13;break;
case 4:
Type[e.seat.x][e.seat.y].data=14;break;
}//switch
}//while
printf("\n");

for(int i=0;i<r;++i)
{
for(int j=0;j<c;++j)
{
switch( Type[i][j].data)
{
case 1:
printf("1");break;
case 0:
printf("0");break;
case 11:
printf("->");break;
case 12:
printf("向下");break;
case 13:
printf("<-");break;
case 14:
printf("向上");break;

}//switch
}//for

printf("\n");
}//for
}
void main()
{
Maze maze;
maze.data=0;
maze.nopa=0;
maze.pass=0;
SqStack S;
srand((int)time(0));
int r,c;//行数和列数
printf("\n*****迷宫*****\n");
printf("请输入行数:");
scanf("%d",&r);
printf("请输入列数:");
scanf("%d",&c);

for(int i=0;i<r;++i)
for(int j=0;j<c;++j)
{
Type[i][j].data=rand()%2;
Type[i][j].pass=0;
Type[i][j].nopa=0;
}

MazePrint(maze,r,c);
PosType start,end;
int x,y,w,z;int sign=0;
do
{
printf("\n******\n");
printf("输入入口和出口的坐标(中间用空格隔开):");

scanf("%d",&x);
scanf("%d",&y);
scanf("%d",&w);
scanf("%d",&z);

start.x=x;start.y=y;end.x=w;end.y=z;
if(!Pass(maze,start))
{
sign=0;printf("入口是墙,请重新输入:");
}
else if(!Pass(maze,end))
{
sign=0;printf(" 出口是墙,请重新输入:");
}
else
{
sign=1;
printf("入口,出口正确。开始求解迷宫...\n");
}
}
while(sign==0);
{
time_t tm=time(0);
if(MazePath(maze,S,start,end))
{
Print(maze,S,r,c);
printf("求解用时大约是:%ld秒",tm);
}
}

}
温馨提示:答案为网友推荐,仅供参考
相似回答