大家帮忙完成一下迷宫哈,谢谢了

浪流 2008-12-09 09:57:26
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define true 1
#define false 0
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define RANGE 12
#define STACK_INIT_SIZE 100
typedef int Status;
typedef int bool;
typedef struct
{
int x;
int y;
}PosType;
typedef struct
{
char arr[RANGE][RANGE];
}MazeType; //迷宫类型
typedef struct
{
int ord;
int di;
PosType seat;
}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(OVERFLOW);
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return OK;
}
bool Pass(MazeType *M,PosType pos) /*检查是否为空格*/
{
if(M->arr[pos.x][pos.y]==' ')
return true;
else
return false;
}
void FootPrint(MazeType *M,PosType pos) /*将该位置标记已走*/
{
M->arr[pos.x][pos.y]='*';
}
Status 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) exit(OVERFLOW);
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*S->top++=e;
return OK;
}
PosType NextPos(PosType pos,int i) /*求当前位置的下一个探索位置*/
{
switch(i)
{
case 1:
pos.y++;
break;
case 2:
pos.x++;
break;
case 3:
pos.y--;
break;
case 4:
pos.x--;
break;
}
return pos;
}
Status StackEmpty(SqStack *S)
{
if (S->top==-1)
return(1); //空栈则返回1
else
return(0);
}
void pop(SqStack *S)
{
if (S->top==-1)
printf("栈下溢出!\n");
else
S->top--;
}
void MarkPrint(MazeType *M,PosType pos) /*将该位置标记为死胡同*/
{
M->arr[pos.x][pos.y]='@';
}
Status MazePath(MazeType *maze,PosType start,PosType end)
{
SqStack S;
SElemType e;
PosType curpos;
int curstep=1;
InitStack(&S);
curpos=start;
e.ord=curstep;e.seat=curpos;e.di=1;
do
{
if(Pass(maze,curpos))
{
FootPrint(maze,curpos);
Push(S,&e);
if(curpos==end)
return true;
curpos=NextPos(curpos,1);
curstep++;
}
else(!StackEmpty(S))
{
Pop(&S,&e);
while(e.seat==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));
return (false);
}

void main()
{
PosType char arr[12][12]={
{'X','X','X','X','X','X','X','X','X','X','X','X'},
{'X','.','.','.','.','.','.','X','.','X','X','X'},
{'X','.','X','.','X','X','.','.','.','.','.','X'},
{'X','.','X','.','X','X','.','X','X','X','.','X'},
{'X','.','X','.','.','.','.','.','X','.','.','X'},
{'X','.','X','X','X','X','X','X','X','X','X','X'},
{'X','.','.','.','X','.','X','.','.','.','.','X'},
{'X','.','X','X','X','.','.','.','X','X','X','X'},
{'X','.','.','.','.','.','X','X','.','.','.','X'},
{'X','X','X','.','X','X','X','X','.','X','.','X'},
{'X','X','X','X','X','X','X','X','.','X','X','X'},
{'X','X','X','X','X','X','X','X','X','X','X','X'},
};
MazePath(

}
...全文
129 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
liberpc 2008-12-25
  • 打赏
  • 举报
回复
就上代码?什么类型的问题啊?
zhangyong369 2008-12-14
  • 打赏
  • 举报
回复
up to your climax
twtqe 2008-12-14
  • 打赏
  • 举报
回复
主要的代码 书上都有
浪流 2008-12-14
  • 打赏
  • 举报
回复
还没有写完,帮忙写完一下。
DRBYYW 2008-12-10
  • 打赏
  • 举报
回复
typedef int bool;
DDGG 2008-12-10
  • 打赏
  • 举报
回复
YXMG日常,来的M~~
xiangxuf 2008-12-10
  • 打赏
  • 举报
回复
lz就这样扔下走人了??
DRBYYW 2008-12-10
  • 打赏
  • 举报
回复
都没说问题啊
pingzi_1119 2008-12-10
  • 打赏
  • 举报
回复
up
toadzw 2008-12-10
  • 打赏
  • 举报
回复
up to your climax
toadzw 2008-12-10
  • 打赏
  • 举报
回复
up to your climax
lbjfeng 2008-12-10
  • 打赏
  • 举报
回复
对头~~
xqls_xqls 2008-12-09
  • 打赏
  • 举报
回复
说明白点,LZ
星羽 2008-12-09
  • 打赏
  • 举报
回复
你现在遇到的问题是什么

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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