| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1430 人关注过本帖
标题:求助!谁能帮解迷宫,给1000金币,来者皆有
取消只看楼主 加入收藏
梦想中国
Rank: 2
等 级:新手上路
威 望:5
帖 子:539
专家分:0
注 册:2006-2-26
收藏
 问题点数:0 回复次数:5 
求助!谁能帮解迷宫,给1000金币,来者皆有

下面的迷宫程序出现死循环,转圈问题,谁能解决给1000金币

#include<stdio.h>
#include<stdlib.h>
#define INIT_STACK_SIZE 30
#define STACKINCREMENT 10
#define N 7
typedef struct{
int *base;
int *top;
int stacksize;
}SqStack;

int a[N][N]={0,0,0,0,0,0,0, //0代表障碍,1代表路径
0,1,1,1,1,0,0,
0,1,1,0,1,1,0,
0,0,1,0,0,0,0,
0,1,1,1,1,1,0,
0,1,1,0,1,1,0,
0,0,0,0,0,0,0};

SqStack InitStack(SqStack S);
SqStack FoundPath(SqStack S,int a[N][N]);
int EmptyStack(SqStack S);
SqStack Pop(SqStack S,int *path);
SqStack Push(SqStack S,int path);

int main(void)
{
SqStack S;
int path;

S=InitStack(S);
S=FoundPath(S,a);
while(!EmptyStack(S))
{
S=Pop(S,&path);
printf("%d->",path);
}
printf("->null\n\n");
free(S.base);

return 0;
}

SqStack FoundPath(SqStack S,int a[N][N])
{
int i,j,path,row,column;

i=1;j=1; //a[1][1]为路径的开始位置
a[5][5]=55;
while(a[i][j]!=a[5][5]) //当前的值不等于迷宫出口值时
{
if(a[i][j]==1) //代表路径可通情况
{
row=i; //把能走通的路径赋给row,column压入栈
column=j;
path=10*row+column;
S=Push(S,path); //把可通路径压入栈
j++; //继续向东走
}
else if(a[i][j]==0) //当向东走不通的时候,首先向南走
{
i=row;
j=column;
i=i+1;
if(a[i][j]==1) //向南走通的情况
{
row=i;
column=j;
path=10*row+column; //将向南走通的路径压入栈
S=Push(S,path);
j++;
}
else if(a[i][j]==0) //想南走不通的情况,向西走
{
i=row;
j=column;
j--;
if(a[i][j]==1) //向西走通的时候
{
row=i;
column=j;
path=10*row+column;
S=Push(S,path);
j++;
}
else if(a[i][j]==0) //向西走不通的情况
{
i=row;
j=column;
i--;
if(a[i][j]==1) //向北走通的情况
{
row=i;
column=j;
path=10*row+column;
S=Push(S,path);
j++;
}
else //东南西北全部走不同的情况
printf("maze is not answer.\n");
}
}//else
}//else
}//while
if(a[i][j]==55) //把最后一个路径压入栈
{
row=i;
column=j;
path=10*row+column;
S=Push(S,path);
}

return S;
}

SqStack InitStack(SqStack S)
{
if((S.base=(int *)malloc(INIT_STACK_SIZE * sizeof(int)))==NULL)
{
exit(1);
}
S.top=S.base;
S.stacksize=INIT_STACK_SIZE;

return S;
}

int EmptyStack(SqStack S)
{
int flag=0;

if(S.top==S.base)
flag=1;

return flag;
}

SqStack Pop(SqStack S,int *path)
{
if(!EmptyStack(S))
*path=*(--S.top);

return S;
}

SqStack Push(SqStack S,int path)
{
*(S.top++)=path;
if(S.top-S.base >= S.stacksize)
{
if((S.base=(int *)realloc(S.base,(S.stacksize + STACKINCREMENT) * sizeof(int)))==NULL)
{
exit(1);
}
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}

return S;
}

[此贴子已经被作者于2006-3-26 14:18:31编辑过]

搜索更多相关主题的帖子: 解迷宫 来者 金币 int SqStack 
2006-03-26 11:07
梦想中国
Rank: 2
等 级:新手上路
威 望:5
帖 子:539
专家分:0
注 册:2006-2-26
收藏
得分:0 

怎么,没人帮我吗?


2006-03-26 12:01
梦想中国
Rank: 2
等 级:新手上路
威 望:5
帖 子:539
专家分:0
注 册:2006-2-26
收藏
得分:0 
没有题目,我自己想的迷宫

2006-03-26 12:12
梦想中国
Rank: 2
等 级:新手上路
威 望:5
帖 子:539
专家分:0
注 册:2006-2-26
收藏
得分:0 
晕,那是栈

2006-03-26 14:16
梦想中国
Rank: 2
等 级:新手上路
威 望:5
帖 子:539
专家分:0
注 册:2006-2-26
收藏
得分:0 
谢谢楼上,金币等一下,我试试程序

2006-03-26 15:01
梦想中国
Rank: 2
等 级:新手上路
威 望:5
帖 子:539
专家分:0
注 册:2006-2-26
收藏
得分:0 
谢谢楼上,不过这年头拼的就是实力,要的就是速度

2006-03-26 17:25
快速回复:求助!谁能帮解迷宫,给1000金币,来者皆有
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012188 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved