关于软件设计师教程中P447的迷宫问题

hellolp425 2008-02-22 03:22:37
问题是求解迷宫中从入口到出口的所有路径
附源代码如下:
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 50
#define ERROR -1
#define OK 0
#define FALSE 0
#define TRUE 1
typedef enum{RIGHT,DOWN,LEFT,UP}Direction;
typedef enum{YES,NO}MarkTag;
typedef struct position{
int x;int y;
}Position;
typedef struct{
int order;
Position seat;
Direction di;
}SElemType;
typedef struct{
SElemType *elem;
int top;
}Stack;
char maze[MAXSIZE+2][MAXSIZE+2];
int InitStack(Stack *S){
S->elem=(SElemType*)malloc(MAXSIZE*MAXSIZE*sizeof(SElemType));
if(!S->elem)
return ERROR;
S->top=0;
return OK;

}
int Push(Stack *S,SElemType e){
if(S->top>=MAXSIZE*MAXSIZE)
return ERROR;
S->elem[S->top++]=e;
return OK;
}
int Pop(Stack *S,SElemType *e){
if(S->top<=0)
return ERROR;
*e=S->elem[--S->top];
return OK;
}
int Empty(Stack S){
if(S.top==0)
return TRUE;
return FALSE;
}
int createMaze(char *filename,Position *startpos,Position *endpos){
FILE *fp;
int i,j,rows,cols,temp;
Position start,end;
fp=fopen(filename,"r");
if(!fp){
printf("open file %s error!\n",filename);
return ERROR;
}
if(!feof(fp)){
fscanf(fp,"%d,%d",&rows,&cols);
fscanf(fp,"%d,%d",&start.x,&start.y);
fscanf(fp,"%d,%d",&end.x,&end.y);
}
for(i=1;i<=rows;i++){
for(j=1;j<=cols;j++){
fscanf(fp,"%d",&temp);
maze[i][j]=48+temp;
}
}
fclose(fp);
for(i=0,j=0;i<=rows+1;i++)
maze[i][j]='1';
for(i=0,j=cols+1;i<=rows+1;i++)
maze[i][j]='1';
for(i=0,j=0;j<=cols+1;j++)
maze[i][j]='1';
for(i=rows+1,j=0;j<=cols+1;j++)
maze[i][j]='1';
*startpos=start;
*endpos=end;
return OK;
}
int canPass(Position curpos){
if(maze[curpos.x][curpos.y]=='0')
return TRUE;
return FALSE;
}
void markPos(Position curpos,MarkTag tag){
switch(tag){
case YES:
maze[curpos.x][curpos.y]='.';
break;
case NO:
maze[curpos.x][curpos.y]='#';
break;
}
}
Position nextPos(Position curpos,Direction dir){
Position nextpos;
switch(dir){
case RIGHT:
nextpos.x=curpos.x;
nextpos.y=curpos.y+1;
break;
case DOWN:
nextpos.x=curpos.x+1;
nextpos.y=curpos.y;
break;
case LEFT:
nextpos.x=curpos.x;
nextpos.y=curpos.y-1;
break;
case UP:
nextpos.x=curpos.x-1;
nextpos.y=curpos.y;
break;
}
return nextpos;
}
Direction nextDir(Direction dir){
switch(dir){
case RIGHT:
return DOWN;
case DOWN:
return LEFT;
case LEFT:
return UP;
}
}
int Solve(Stack *S,Position start,Position end){
Position curpos;
SElemType e;
int curstep=1;
if(InitStack(S)==ERROR)
return FALSE;
curpos=start;
do{
if(canPass(curpos)){
markPos(curpos,YES);
e.order=curstep;
e.seat=curpos;
e.di=RIGHT;
Push(S,e);
if(curpos.x==end.x&&curpos.y==end.y)
return TRUE;
curpos=nextPos(curpos,RIGHT);
curstep++;
}
else{
if(!Empty(*S)){
if(Pop(S,&e)==ERROR)
return FALSE;
while(e.di==UP&&!Empty(*S)){
curpos=e.seat;
markPos(curpos,NO);
if(Pop(S,&e)==ERROR)
return FALSE;
}
if(e.di!=UP){
e.di=nextDir(e.di);
Push(S,e);
curpos=nextPos(e.seat,e.di);
}
}
}
}while(!Empty(*S));
return FALSE;
}
void main(){
Position startPos,endPos;
Stack path;
SElemType e;
char*fname="in.txt";
if(createMaze(fname,&startPos,&endPos)==ERROR)
return;
Solve(&path,startPos,endPos);
while(!Empty(path)){
Pop(&path,&e);
printf("(%d,%d)\n",e.seat.x,e.seat.y);
}
}
输入为:88
1188
00100010
00100010
00001100
01110000
00010000
01000100
01110110
10000000
一切都是照书上做的,但是无法运行。不知是什么原因。看算法应该是没有错误吧?请大家指点
...全文
71 点赞 收藏 1
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
fubin_biao 2008-03-04
我觉得int Solve(Stack *S,Position start,Position end)这个方法有问题,仔细分析了一下发现是一个死循环,不知道楼主看出来了没有!
回复
相关推荐
发帖
软件水平考试
创建于2007-08-27

2791

社区成员

就计算机等级考试、软件初、中、高级不同级别资格考试相关话题交流经验,共享资源。
申请成为版主
帖子事件
创建了帖子
2008-02-22 03:22
社区公告
暂无公告