用栈的迷宫问题

kinger425 2009-09-22 11:23:32
#include <stdio.h>
#include <stdlib.h>
#define MAXNUM 40
#define M 10
#define N 10
typedef struct
{
int x,y,d;
}guide;
typedef struct
{
guide data[MAXNUM];
int top;
}STACK;
typedef struct
{
int x,y;
}item;
item move[8]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
int maze[M][N]={1,1,1,1,1,1,1,1,1,1,
1,0,1,1,0,0,1,0,0,1,
1,1,0,0,1,1,0,1,0,1,
1,1,1,0,1,1,0,1,1,1,
1,1,0,1,1,1,1,0,0,1,
1,0,1,1,0,0,1,1,0,1,
1,0,1,0,1,1,0,0,0,1,
1,0,1,1,0,1,1,1,1,1,
1,1,0,1,1,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1};
STACK *Init_stack()
{
STACK *s;
s=(STACK *)malloc(sizeof(STACK));
s->top=-1;
return s;
}
int empty_stack(STACK *s)
{
if(s->top==-1) return 1;
else return 0;
}
void Push_stack(STACK *s,guide temp)
{
if(s->top==MAXNUM-1) puts("no space");
else
{
s->top++;
s->data[s->top].x=temp.x;
s->data[s->top].y=temp.y;
s->data[s->top].d=temp.d;
}
}
void Pop_stack(STACK *s,guide temp)
{
if(empty_stack(s)) printf("empty stack\n");
else
{

temp.x=s->data[s->top].x;
temp.y=s->data[s->top].y;
temp.d=s->data[s->top].d;
s->top--;
}
}

main()
{
STACK *s;
guide temp;
int x,y,i,j,d;
s=Init_stack();
temp.x=1;
temp.d=-1;
temp.y=1;
Push_stack(s,temp);

while(!empty_stack(s))
{
printf("%d,%d,%d\n",s->data[s->top].x,s->data[s->top].y,s->data[s->top].d);
Pop_stack(s,temp);
x=temp.x;
y=temp.y;
d=temp.d+1;
while(d<8)
{
i=x+move[d].x;
j=y+move[d].y;
if(maze[i][j]==0)
{
printf("%d--%d\n",i,j);
temp.d=d;
temp.x=x;
temp.y=y;
Push_stack(s,temp);
printf("%d,%d,%d\n",s->data[s->top].x,s->data[s->top].y,s->data[s->top].d);
x=i;y=j;maze[x][y]=69;
if(x==8&&y==x)
{
printf("out!\n");
exit(0);
}
else d=0;

}
else d++;
}

}
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
printf("%d\t",maze[i][j]);
printf("\n");
}
}
我已经是看了又看,对了又对书本。可就是得不出正确结果。高手帮忙啊
...全文
231 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
kouwenlong 2009-09-22
  • 打赏
  • 举报
回复
1是代表墙,0代表通道,对吧?
怎么看你的迷宫数组好像有问题吧?
kinger425 2009-09-22
  • 打赏
  • 举报
回复
-_-!

迷宫啊
就是那个 出口放个奶酪
把小老鼠放在入口让自己找出口

我中间加了点输出是为了找错
好像那个回溯没起作用
kouwenlong 2009-09-22
  • 打赏
  • 举报
回复
你的程序是什么功能?
kouwenlong 2009-09-22
  • 打赏
  • 举报
回复
没错,我正在给你看呢。
kinger425 2009-09-22
  • 打赏
  • 举报
回复
move[] 是个结构体数组
这样写应该没问题吧
kouwenlong 2009-09-22
  • 打赏
  • 举报
回复
item move[8]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}};
写错了.move[8][2]
kinger425 2009-09-22
  • 打赏
  • 举报
回复
前面是栈的一些基本函数
迷宫,数据构造,方向的准备工作。
MAIN()里是探路的

不麻烦的。帮帮忙!!!!
hrlhrl0 2009-09-22
  • 打赏
  • 举报
回复
要不我给你个我的
kinger425 2009-09-22
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#define MAXNUM 40
#define M 10
#define N 10
typedef struct
{
int x,y,d;
}guide;
typedef struct
{
guide data[MAXNUM];
int top;
}STACK;
typedef struct
{
int x,y;
}item;
item move[8]={{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1}}; //方向初始化
int maze[M][N]={1,1,1,1,1,1,1,1,1,1,
1,0,1,1,0,0,1,0,0,1,
1,1,0,0,1,1,0,1,0,1,
1,1,1,0,1,1,0,1,1,1,
1,1,0,1,1,1,1,0,0,1,
1,0,1,1,0,0,1,1,0,1,
1,0,1,0,1,1,0,0,0,1,
1,0,1,1,0,1,1,1,1,1,
1,1,0,1,1,0,0,0,0,1,
1,1,1,1,1,1,1,1,1,1};
STACK *Init_stack()
{
STACK *s;
s=(STACK *)malloc(sizeof(STACK));
s->top=-1;
return s;
}
int empty_stack(STACK *s)
{
if(s->top==-1) return 1;
else return 0;
}
void Push_stack(STACK *s,guide temp)
{
if(s->top==MAXNUM-1) puts("no space");
else
{
s->top++;
s->data[s->top].x=temp.x;
s->data[s->top].y=temp.y;
s->data[s->top].d=temp.d;
}
}
void Pop_stack(STACK *s,guide temp)
{
if(empty_stack(s)) printf("empty stack\n");
else
{

temp.x=s->data[s->top].x;
temp.y=s->data[s->top].y;
temp.d=s->data[s->top].d;
s->top--;
}
}

main()
{
STACK *s;
guide temp;
int x,y,i,j,d;
s=Init_stack(); //初始化栈
temp.x=1;
temp.d=-1;
temp.y=1;
Push_stack(s,temp); //入口压入栈

while(!empty_stack(s))// 开始出发
{
printf("%d,%d,%d\n",s->data[s->top].x,s->data[s->top].y,s->data[s->top].d);
Pop_stack(s,temp);
x=temp.x;
y=temp.y;
d=temp.d+1; //回溯时下一个方向开始
while(d<8)// 探路
{
i=x+move[d].x;
j=y+move[d].y;
if(maze[i][j]==0) //发现新路点
{
printf("%d--%d\n",i,j);
temp.d=d;
temp.x=x;
temp.y=y;
Push_stack(s,temp);
printf("%d,%d,%d\n",s->data[s->top].x,s->data[s->top].y,s->data[s->top].d);
x=i;y=j;maze[x][y]=69;//路点压入栈并标记
if(x==8&&y==x)
{
printf("out!\n");
goto II; //出口,停止探路
}
else d=0; //新路点从零方向

}
else d++; //不是路点下一方向
}

}
puts("no way");
II:
for(i=0;i<10;i++) //输出迷宫和探索过的路
{
for(j=0;j<10;j++)
printf("%d\t",maze[i][j]);
printf("\n");
}
while(s->top>-1) //输出通路
{
printf("%d,%d,%d\n",s->data[s->top].x,s->data[s->top].y,s->data[s->top].d);
s->top--;
}
}
HELP!!! 就是找不出错误,就是得不到答案。
我做了注释。 希望前辈们认真看看啊。(探路中的输出是我为了跟踪探路过程找错,可以忽略)
anybbs 2009-09-22
  • 打赏
  • 举报
回复
太长了,谁有这个闲工夫看啊?

说具体点比较好


----------------
kouwenlong 2009-09-22
  • 打赏
  • 举报
回复
还得有方向吧,你的好像没有吧?
再看看你的代码吧.弄懂了再问吧,我现在也看不出来什么.
hrlhrl0 2009-09-22
  • 打赏
  • 举报
回复
我写好了一个,蛮好的

69,336

社区成员

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

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