| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1430 人关注过本帖
标题:求助!谁能帮解迷宫,给1000金币,来者皆有
只看楼主 加入收藏
梦想中国
Rank: 2
等 级:新手上路
威 望:5
帖 子:539
专家分:0
注 册:2006-2-26
收藏
 问题点数:0 回复次数:18 
求助!谁能帮解迷宫,给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: 16Rank: 16Rank: 16Rank: 16
等 级:版主
帖 子:622
专家分:318
注 册:2004-5-4
收藏
得分:0 
有点难得说
2006-03-26 12:04
仁者无敌
Rank: 1
等 级:新手上路
帖 子:199
专家分:0
注 册:2006-3-5
收藏
得分:0 
函数名字太长,我英语又太差,所以比较麻烦,为何不把原题目说出来呢

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

2006-03-26 12:12
withoutme_hw
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2006-3-19
收藏
得分:0 
搂主的程序中很多变量的意义都没给注释啊 难理解
SqStack Push(SqStack S,int path)
中的
*(S.top++)=path;
是什么意思 ?

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

2006-03-26 14:16
success
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-3-24
收藏
得分:0 

我看了看,程序写的还可以,只是有小问题,我按你的思路写了一个,我测试了两遍,没什么问题。
给金币吧,1000个,全是我的。
#include<stdio.h>
#include<stdlib.h>
#define N 7
#define INIT_STACK_SIZE 20
#define STACKINCREMENT 10
typedef struct{
int *base;
int *top;
int stacksize;
}SqStack;

int maze[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 maze[N][N]);
SqStack Push(SqStack S,int path);
SqStack Pop(SqStack S,int *path);
int EmptyStack(SqStack S);

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

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

return 0;
}


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;
}

SqStack FoundPath(SqStack S,int maze[N][N])
{
int i,j,path;

i=1; j=1; maze[5][5]=55;
while(maze[i][j]!=maze[5][5])
{
if(maze[i][j]==1)
{
path=10*i+j; //把当前可通路径压入栈
S=Push(S,path);
maze[i][j]=0;
j++; //继续向东走
}
else if(maze[i][j]==0) //当前位置不可通,向南走
{
if(!EmptyStack(S)) //当前栈不空
path=*(S.top-1);
i=path/10;
j=path%10;
i++; //向南走
if(maze[i][j]==1)
{
path=10*i+j;
S=Push(S,path);
maze[i][j]=0;
j++;
}
else if(maze[i][j]==0) //向南走不通,向西走
{
if(!EmptyStack(S))
path=*(S.top-1);
i=path/10;
j=path%10;
j--;
if(maze[i][j]==1) //向西走通
{
path=10*i+j;
S=Push(S,path);
maze[i][j]=0;
j++;
}
else if(maze[i][j]==0) //向西走不通
{
if(!EmptyStack(S))
path=*(S.top-1);
i=path/10;
j=path%10;
i--;
if(maze[i][j]==1) //向北走通
{
path=10*i+j;
S=Push(S,path);
maze[i][j]=0;
j++;
}
else //都走不通时
{
if(!EmptyStack(S))
S=Pop(S,&path);
i=path/10;
j=path%10;
}
}//else
}//else
}//else
}//while
if(maze[i][j]==maze[5][5])
{
path=10*i+j;
S=Push(S,path);
}

return S;
}

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

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

return flag;
}

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;
}

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

return S;
}

请指教


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

2006-03-26 15:01
控制0and1
Rank: 1
等 级:新手上路
帖 子:22
专家分:0
注 册:2006-1-12
收藏
得分:0 
楼主你好:
  我近一段有点忙事,不知你是否着急要答案,如果不着急,我有空帮你看看,如果着急,我也没办法了!不过不管怎样,我想它不会难的,会有人帮你解决的,不要灰心!
     等你回复!我把它剪到邮箱可以吗?

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



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

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