主题:迷宫问题?不能运行啊!怎么解决?
bai
[专家分:70] 发布于 2005-09-23 16:59:00
#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(!feop(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;<=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;
}
}
回复列表 (共9个回复)
沙发
bai [专家分:70] 发布于 2005-09-23 17:00:00
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);
}
}
板凳
qq9199333 [专家分:60] 发布于 2005-09-23 17:13:00
网络资源下载中心http://www.nrdc.cn/index.asp大量编程书免费售 不收钱不要邮费 如果本店没有你要的书请到这里给我留言 http://www.nrdc.cn/gbindex.asp 你们开发了东西没有网上空间可放可以到先放我这里来 http;//www.nrd.cn
3 楼
qq9199333 [专家分:60] 发布于 2005-09-23 19:12:00
如果解决不了这个问题可以到网络资源下载中心查一查,不想查看看笑话也好啊.
做正经生意,在我这里创业不需花钱就是辛苦,挣不了大钱一个月挣几百元还是没有一点问题的。
可以加一个变量。用地址进行判断。或直接加一个querystring 变量
我的域名价更低 专业国际国内域名超低限量价:45元
网络资源下载中心http://www.nrdc.cn/index.asp大量编程书免费售 不收钱不要邮费 如果本店没有你要的书请到这里给我留言 http://www.nrdc.cn/gbindex.asp 你们开发了东西没有网上空间可放可以到先放我这里来 http;//www.nrd.cn
4 楼
said [专家分:60] 发布于 2005-09-27 20:16:00
呜呜,没注释的,好辛苦............
5 楼
smallworker [专家分:50] 发布于 2005-10-02 12:12:00
#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(!feop(fp)) /***********!feof(fp)*/
{
fscanf(fp,"%d%d",&rows,&cols);
fscanf(fp,"%d%d",&start.x,&start.y);
fscanf(fp,"%d%d",end.x,&end.y); /********&end.x*/
}
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;<=rows+1;i++) maze[i][j]='1'; /********i<rows+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;
}
}
6 楼
smallworker [专家分:50] 发布于 2005-10-02 12:13:00
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)) /*********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); /*******startPos*/
while(!Empty(path))
{
Pop(&path,&e);
printf("(%d,%d")\n",e.seat.x,e.seat.y); /*******"(%d,%d)\n"*/
}
}
7 楼
bai [专家分:70] 发布于 2005-10-05 11:23:00
还是不行啊!
8 楼
smallworker [专家分:50] 发布于 2005-10-06 22:22:00
我怎么可以运行呀,你的迷宫in.txt按要求作了没?
9 楼
asp5858 [专家分:0] 发布于 2006-04-29 08:52:00
我也有问题
我来回复