董乘宇--迷宫程序1.10版

 

None.gif 程序目的:
None.gif输入一个任意大小的迷宫,用栈求出一条走出迷宫的路径,并
None.gif显示在屏幕上。
None.gif程序实现:
None.gif可以实现载入迷宫和保存迷宫,附带文件中有4个测试迷宫路径的
None.gif文件test1
~ 4 .dd。请将这些文件拷贝到TC当前目录下,或者在载
None.gif入时写明完全路径。由于屏幕大小的限制,当用户自己输入迷宫
None.gif时一定要注意:迷宫大小是有限制的,不小于4
* 3 ,不大于30 * 20
None.gif否则会出现错误信息。输入开始时全是墙,用上下左右键移动,
None.gif用Del键删除墙,形成通路,用Enter键添加墙。输入结束时可以
None.gif将迷宫保存下来,以dd为扩展名。输入完毕时用F9键来得到结果,
None.gif找到路径时,屏幕下方会出现Path found,否则出现Path not found。
None.gif程序经Turbo C 
2 .0编译调试成功。运行时不用添加任何运行库。
None.gif不可以在VC上编译。
None.gif下载DOS版和windows版的迷宫游戏全部代码
None.gif用户名:migong 
None.gif
----------------------------------------------------------------------------------
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*
InBlock.gifMazePath Demo BY Turbo C 2.0
InBlock.gifCopyright(c) RoverUnion. All right reserved.
InBlock.gifFilename: Maze.c
InBlock.gifAuthor Dongchengyu.
InBlock.gifVer 1.10
ExpandedBlockEnd.gif
*/

None.gif#include 
< stdio.h >
None.gif#include 
< stdlib.h >
None.gif#include 
< malloc.h >
None.gif#include 
< conio.h >
None.gif#include 
< dos.h >
None.gif
#define  OK 1
None.gif
#define  ERROR 0
None.gif
#define  TRUE 1
None.gif
#define  FALSE 0
None.gif
#define  F9 0x43
None.gif
#define  Esc 0x1b
None.gif
#define  Del 0x53
None.gif
#define  Home 0x47
None.gif
#define  End 0x4f
None.gif
#define  Space 0x20
None.gif
#define  Up 0x48
None.gif
#define  Down 0x50
None.gif
#define  Left 0x4b
None.gif
#define  Right 0x4d
None.gif
#define  Enter 0x0d
None.gif
#define  F2 0x3c
None.gif
#define  F3 0x3d
None.gif
#define  STACK_INIT_SIZE 200
None.gif
#define  STACKINCREMENT 10
None.giftypedef 
int  Boolean;
None.giftypedef 
int  Status;
ExpandedBlockStart.gifContractedBlock.giftypedef 
struct   dot.gif {
InBlock.gif
int x;
InBlock.gif
int y;
ExpandedBlockEnd.gif}
 PosType;
ExpandedBlockStart.gifContractedBlock.giftypedef 
struct   dot.gif {
InBlock.gif
int ord;
InBlock.gifPosType seat;
InBlock.gif
int di;
ExpandedBlockEnd.gif}
 SElemType;
ExpandedBlockStart.gifContractedBlock.giftypedef 
struct   dot.gif {
InBlock.gif
int td;
InBlock.gif
int foot;
InBlock.gif
int mark;
ExpandedBlockEnd.gif}
 MazeType;
ExpandedBlockStart.gifContractedBlock.giftypedef 
struct   dot.gif {
InBlock.gifSElemType 
*base;
InBlock.gifSElemType 
*top;
InBlock.gif
int stacksize;
ExpandedBlockEnd.gif}
 Stack;
None.gif
int  Maze[ 20 ][ 30 ];
None.gifMazeType maze[
20 ][ 30 ];
None.gifPosType StartPlace;
None.gifPosType EndPlace;
None.gif
int  count;
None.gif
int  m,n;
None.gifBoolean b_start
= FALSE,b_end = FALSE;
None.gif
void  CreatMaze( void );
None.gifStatus SaveMaze(
char   * filename);
None.gifStatus LoadMaze(
char   * filename);
None.gif
void  Error( char   * message);
None.gifStatus InitStack(Stack 
* s);
None.gifStatus DestroyStack(Stack 
* s);
None.gifStatus ClearStack(Stack 
* s);
None.gifBoolean StackEmpty(Stack 
* s);
None.gif
int  StackLength(Stack  * s);
None.gifStatus Push(Stack 
* s,SElemType e);
None.gifSElemType Pop(Stack 
* s,SElemType e);
None.gifStatus GetTop(Stack 
* s,SElemType  * e);
None.gifStatus StackTraverse(Stack 
* s,Status ( *  visit)(SElemType  * se));
None.gifBoolean Pass(PosType curpos);
None.gif
void  MarkPrint(PosType seat);
None.gif
void  FootPrint(PosType curpos);
None.gifPosType NextPos(PosType seat,
int  di);
None.gifStatus MazePath(PosType start,PosType end);
None.gif
void  CreatMaze( void )
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* Form the maze. */
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
void Error(char *message);
InBlock.gifStatus SaveMaze(
char *filename);
InBlock.gifStatus LoadMaze(
char *filename);
InBlock.gif
int i,j;
InBlock.gif
int x,y;
InBlock.gif
char c;
InBlock.gif
char savename[12],loadname[12];
InBlock.gifBoolean flag
=FALSE,load=FALSE;
InBlock.gifclrscr();
InBlock.gifprintf(
"Menu:\n\n");
InBlock.gifprintf(
"1.Load Mazefile:(*.dd)\n\n");
InBlock.gifprintf(
"2.Input Maze:\n\n");
InBlock.gifprintf(
"Input your choice: ");
InBlock.gif
do
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifc
=getch();
InBlock.gif
switch(c)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
case ''''''''''''''''''''''''''''''''1'''''''''''''''''''''''''''''''': putch(''''''''''''''''''''''''''''''''1''''''''''''''''''''''''''''''''); break;
InBlock.gif
case ''''''''''''''''''''''''''''''''2'''''''''''''''''''''''''''''''': putch(''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''); break;
InBlock.gif
case Esc: sleep(1); exit(1);
InBlock.gif
defaultbreak;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
while(c!=''''''''''''''''''''''''''''''''1''''''''''''''''''''''''''''''''&&c!=''''''''''''''''''''''''''''''''2'''''''''''''''''''''''''''''''') ;
InBlock.gif
if(c==''''''''''''''''''''''''''''''''1'''''''''''''''''''''''''''''''')
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifprintf(
"\n\nLoadName: ");
InBlock.gifscanf(
"%s",loadname);
InBlock.gif
if(LoadMaze(loadname))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifsleep(
1); load=TRUE;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
else dot.gif{ gotoxy(1,9); printf("Load fail! "); }
ExpandedSubBlockEnd.gif}

InBlock.gif
if(!load)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifprintf(
"\nInput the maze''''''''''''''''''''''''''''''''s size:\n");
InBlock.gifprintf(
"\nInput Length :\n");
InBlock.gifscanf(
"%d",&m);
InBlock.gifprintf(
"\nInput Width :\n");
InBlock.gifscanf(
"%d",&n);
InBlock.gif
if(m<4||n<4) Error("Input");
InBlock.gif
if(m>30||n>20) Error("Maze too large");
InBlock.gif
for(i=0;i<30;i++)
InBlock.gif
for(j=0;j<20;j++)
InBlock.gifMaze[j][i]
=2;
InBlock.gifStartPlace.x
=0;
InBlock.gifStartPlace.y
=0;
InBlock.gifEndPlace.x
=0;
InBlock.gifEndPlace.y
=0;
InBlock.gifclrscr();
InBlock.gifprintf(
"\n");
InBlock.gif
for(i=1;i<=n;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
for(j=1;j<=m;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifprintf(
" #");
InBlock.gifMaze[i
-1][j-1]=0;
ExpandedSubBlockEnd.gif}

InBlock.gifprintf(
"\n");
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gifgotoxy(
65,5);
InBlock.gifprintf(
"''''''''''''''''''''''''''''''''#'''''''''''''''''''''''''''''''':Wall");
InBlock.gifgotoxy(
65,7);
InBlock.gifprintf(
"Start:Home");
InBlock.gifgotoxy(
65,9);
InBlock.gifprintf(
"End:End");
InBlock.gifgotoxy(
65,11);
InBlock.gifprintf(
"Delete Wall:Del");
InBlock.gifgotoxy(
65,13);
InBlock.gifprintf(
"Enter Wall:Enter");
InBlock.gifgotoxy(
65,15);
InBlock.gifprintf(
"Save Maze:F2");
InBlock.gifgotoxy(
65,17);
InBlock.gifprintf(
"Complete:F9");
InBlock.gifgotoxy(
65,19);
InBlock.gifprintf(
"Exit:Esc");
InBlock.gifgotoxy(
4,3);
InBlock.gifx
=4;y=3;
InBlock.gif
do
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifc
=getch();
InBlock.gif
switch(c)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
case Up: if(y>3dot.gif{ y--; gotoxy(x,y); }
InBlock.gif
break;
ExpandedSubBlockStart.gifContractedSubBlock.gif
case Down: if(y<n) dot.gif{ y++; gotoxy(x,y); }
InBlock.gif
break;
ExpandedSubBlockStart.gifContractedSubBlock.gif
case Left: if(x>4dot.gif{ x-=2; gotoxy(x,y); }
InBlock.gif
break;
ExpandedSubBlockStart.gifContractedSubBlock.gif
case Right: if(x<2*m-2dot.gif{ x+=2; gotoxy(x,y); }
InBlock.gif
break;
InBlock.gif
case Del: if(y-2==StartPlace.y&&x/2-1==StartPlace.x) b_start=FALSE;
InBlock.gif
if(y-2==EndPlace.y&&x/2-1==EndPlace.x) b_end=FALSE;
InBlock.gifputch(
'''''''''''''''''''''''''''''''' ''''''''''''''''''''''''''''''''); Maze[y-2][x/2-1]=1; gotoxy(x,y);
InBlock.gif
break;
InBlock.gif
case Enter: if(y-2==StartPlace.y&&x/2-1==StartPlace.x) break;
InBlock.gif
if(y-2==EndPlace.y&&x/2-1==EndPlace.x) break;
InBlock.gifputch(
''''''''''''''''''''''''''''''''#''''''''''''''''''''''''''''''''); Maze[y-2][x/2-1]=0; gotoxy(x,y);
InBlock.gif
break;
InBlock.gif
case Home: if(Maze[y-2][x/2-1]&&!b_start)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifStartPlace.x
=x/2-1;
InBlock.gifStartPlace.y
=y-2;
InBlock.gifputch(
''''''''''''''''''''''''''''''''S'''''''''''''''''''''''''''''''');
InBlock.gifgotoxy(x,y);
InBlock.gifb_start
=TRUE;
ExpandedSubBlockEnd.gif}

InBlock.gif
break;
InBlock.gif
case End: if(Maze[y-2][x/2-1]&&!b_end)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifEndPlace.x
=x/2-1;
InBlock.gifEndPlace.y
=y-2;
InBlock.gifputch(
''''''''''''''''''''''''''''''''E'''''''''''''''''''''''''''''''');
InBlock.gifgotoxy(x,y);
InBlock.gifb_end
=TRUE;
ExpandedSubBlockEnd.gif}

InBlock.gif
break;
InBlock.gif
case Esc: gotoxy(2,22); printf("exit"); sleep(1); exit(1);
InBlock.gif
case F9: if(b_start&&b_end) flag=TRUE; break;
InBlock.gif
case F2: gotoxy(2,22);
InBlock.gifprintf(
"Savename:");
InBlock.gifscanf(
"%s",savename);
InBlock.gifgotoxy(
2,22);
InBlock.gif
if(SaveMaze(savename)) printf("Save OK! ");
InBlock.gif
else printf("Save fail! ");
InBlock.gifsleep(
1);
InBlock.gifgotoxy(
2,22);
InBlock.gifprintf(
" ");
InBlock.gifgotoxy(x,y);
InBlock.gif
break;
InBlock.gif
defaultbreak;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
while(!flag);
InBlock.gif
for(i=0;i<30;i++)
InBlock.gif
for(j=0;j<20;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifmaze[j][i].td
=Maze[j][i];
InBlock.gifmaze[j][i].mark
=0;
InBlock.gifmaze[j][i].foot
=0;
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gifStatus LoadMaze(
char   * file)
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* The maze has been loaded. */
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifFILE 
*fp;
InBlock.gif
char *buffer;
InBlock.gif
char ch;
InBlock.gif
int i=0,j,k;
InBlock.gifBoolean len
=FALSE,wid=FALSE;
InBlock.gif
if((fp=fopen(file,"r"))==NULL)
InBlock.gif
return ERROR;
InBlock.gifbuffer
=(char *)malloc(600*sizeof(char));
InBlock.gifch
=fgetc(fp);
InBlock.gif
while(ch!=EOF)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifbuffer[i]
=ch;
InBlock.gifi
++;
InBlock.gifch
=fgetc(fp);
ExpandedSubBlockEnd.gif}

InBlock.gifm
=30;n=20;
InBlock.gif
for(i=0;i<600;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifj
=i/30; k=i%30;
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(buffer[i]==''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''&&!len)dot.gif{ m=i; len=TRUE; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(k==0&&buffer[i]==''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''&&!wid)dot.gif{ n=j; wid=TRUE; }
InBlock.gif
switch(buffer[i])
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
case ''''''''''''''''''''''''''''''''0'''''''''''''''''''''''''''''''': Maze[j][k]=0break;
InBlock.gif
case ''''''''''''''''''''''''''''''''1'''''''''''''''''''''''''''''''': Maze[j][k]=1break;
InBlock.gif
case ''''''''''''''''''''''''''''''''2'''''''''''''''''''''''''''''''': Maze[j][k]=2break;
InBlock.gif
case ''''''''''''''''''''''''''''''''3'''''''''''''''''''''''''''''''': Maze[j][k]=1;
InBlock.gifStartPlace.x
=k;
InBlock.gifStartPlace.y
=j;
InBlock.gifb_start
=TRUE;
InBlock.gif
break;
InBlock.gif
case ''''''''''''''''''''''''''''''''4'''''''''''''''''''''''''''''''': Maze[j][k]=1;
InBlock.gifEndPlace.x
=k;
InBlock.gifEndPlace.y
=j;
InBlock.gifb_end
=TRUE;
InBlock.gif
break;
InBlock.gif
default : break;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.giffclose(fp);
InBlock.gifclrscr();
InBlock.gif
for(i=0;i<30;i++)
InBlock.gif
for(j=0;j<20;j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifmaze[j][i].td
=Maze[j][i];
InBlock.gifmaze[j][i].foot
=0;
InBlock.gifmaze[j][i].mark
=0;
InBlock.gif
if(Maze[j][i]==0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifgotoxy(
2*i+2,j+2);
InBlock.gifputch(
''''''''''''''''''''''''''''''''#'''''''''''''''''''''''''''''''');
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gifgotoxy(
2*StartPlace.x+2,StartPlace.y+2);
InBlock.gifputch(
''''''''''''''''''''''''''''''''S'''''''''''''''''''''''''''''''');
InBlock.gifgotoxy(
2*EndPlace.x+2,EndPlace.y+2);
InBlock.gifputch(
''''''''''''''''''''''''''''''''E'''''''''''''''''''''''''''''''');
InBlock.gif
return OK;
ExpandedBlockEnd.gif}

None.gifStatus SaveMaze(
char   * filename)
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* The maze has been saved. */
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifFILE 
*fp;
InBlock.gif
char *buffer;
InBlock.gif
int i,j,k;
InBlock.giffp
=fopen(filename,"wb");
InBlock.gifbuffer
=(char *)malloc(600*sizeof(char));
InBlock.gif
for(i=0;i<600;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifj
=i/30; k=i%30;
InBlock.gif
switch(Maze[j][k])
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
case 0: buffer[i]=''''''''''''''''''''''''''''''''0''''''''''''''''''''''''''''''''break;
InBlock.gif
case 1: buffer[i]=''''''''''''''''''''''''''''''''1''''''''''''''''''''''''''''''''break;
InBlock.gif
case 2: buffer[i]=''''''''''''''''''''''''''''''''2''''''''''''''''''''''''''''''''break;
InBlock.gif
default : Error("Write"); break;
ExpandedSubBlockEnd.gif}

InBlock.gif
if(k==StartPlace.x&&j==StartPlace.y) buffer[i]=''''''''''''''''''''''''''''''''3'''''''''''''''''''''''''''''''';
InBlock.gif
if(k==EndPlace.x&&j==EndPlace.y) buffer[i]=''''''''''''''''''''''''''''''''4'''''''''''''''''''''''''''''''';
ExpandedSubBlockEnd.gif}

InBlock.giffwrite(buffer,
600,1,fp);
InBlock.giffree(buffer);
InBlock.giffclose(fp);
InBlock.gif
return OK;
ExpandedBlockEnd.gif}

None.gif
void  Error( char   * message)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifclrscr();
InBlock.giffprintf(stderr,
"Error:%s\n",message);
InBlock.gifexit(
1);
ExpandedBlockStart.gifContractedBlock.gif}
  /**/ /* Error */
None.gif
None.gifStatus InitStack(Stack 
* s)
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* The stack s has been created and is initialized to be empty. */
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifs
->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
InBlock.gif
if(!s->base) Error("Overflow");
InBlock.gifs
->top=s->base;
InBlock.gifs
->stacksize=STACK_INIT_SIZE;
InBlock.gif
return OK;
ExpandedBlockStart.gifContractedBlock.gif}
  /**/ /* InitStack */
None.gifStatus DestroyStack(Stack 
* s)
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* The stack s has been destroyed. */
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifs
->top=NULL;
InBlock.gifs
->stacksize=0;
InBlock.giffree(s
->base);
InBlock.gifs
->base=NULL;
InBlock.gif
return OK;
ExpandedBlockStart.gifContractedBlock.gif}
  /**/ /* DestroyStack */
None.gifStatus ClearStack(Stack 
* s)
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* The stack has been clear to be maximum. */
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifs
->top=s->base;
InBlock.gifs
->stacksize=STACK_INIT_SIZE;
InBlock.gif
return OK;
ExpandedBlockStart.gifContractedBlock.gif}
  /**/ /* ClearStack */
None.gifBoolean StackEmpty(Stack 
* s)
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* Check if the stack s is empty. */
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(s->top==s->basereturn TRUE;
InBlock.gif
else return FALSE;
ExpandedBlockStart.gifContractedBlock.gif}
  /**/ /* StackEmpty */
None.gif
int  StackLength(Stack  * s)
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* Gain the length of the stack s. */
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(s->top>s->basereturn (int)(s->top-s->base);
InBlock.gif
else return 0;
ExpandedBlockStart.gifContractedBlock.gif}
  /**/ /* StackLength */
None.gifStatus Push(Stack 
* s,SElemType e)
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* The element e has been pushed into the stack s. */
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(s->top-s->base>=s->stacksize)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifs
->base=(SElemType *)realloc(s->base,
InBlock.gif(s
->stacksize+STACKINCREMENT)*sizeof(SElemType));
InBlock.gif
if(!s->base) Error("Overflow");
InBlock.gifs
->top=s->base+s->stacksize;
InBlock.gifs
->stacksize+=STACKINCREMENT;
ExpandedSubBlockEnd.gif}

InBlock.gif
*s->top++=e;
InBlock.gif
return OK;
ExpandedBlockStart.gifContractedBlock.gif}
  /**/ /* Push */
None.gifSElemType Pop(Stack 
* s,SElemType e)
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* The element e has been removed from the stack s. */
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(s->top==s->base) Error("Pop");
InBlock.gife
=*--s->top;
InBlock.gif
return e;
ExpandedBlockStart.gifContractedBlock.gif}
  /**/ /* Pop */
None.gifStatus GetTop(Stack 
* s,SElemType  * e)
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* The element e has got to the top of the stack s.*/
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(s->top==s->base) Error("GetTop");
InBlock.gif
*e=*(s->top-1);
InBlock.gif
return OK;
ExpandedBlockStart.gifContractedBlock.gif}
  /**/ /* GetTop */
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* Traverse the stack s using ''''''''''''''''''''''''''''''''visiting'''''''''''''''''''''''''''''''' function. */
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* Status StackTraverse(Stack *s,Status (* visit)(SElemType *se))
InBlock.gif{
InBlock.gifSElemType p;
InBlock.gifint result;
InBlock.gifif(s->top==s->base) return ERROR;
InBlock.gifp=s->base;
InBlock.gifwhile(!(p==s->top))
InBlock.gif{
InBlock.gifresult=(*visit)(p);
InBlock.gifp++;
InBlock.gif}
InBlock.gifreturn OK;
ExpandedBlockEnd.gif
*/

None.gifBoolean Pass(PosType curpos)
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* Check if the current position can be passed. */
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
if(maze[curpos.x][curpos.y].td==1&&
InBlock.gifmaze[curpos.x][curpos.y].foot
==0&&maze[curpos.x][curpos.y].mark==0)
InBlock.gif
return TRUE;
InBlock.gif
else return FALSE;
ExpandedBlockStart.gifContractedBlock.gif}
  /**/ /* Pass */
None.gif
void  MarkPrint(PosType seat)
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* Mark the position seat. */
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifmaze[seat.x][seat.y].mark
=-1;
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//* Marking ''''''''''''''''''''''''''''''''-1'''''''''''''''''''''''''''''''' symbolize the current position cannot be put. */
ExpandedBlockStart.gifContractedBlock.gif}
  /**/ /* MarkPrint */
None.gif
void  FootPrint(PosType curpos)
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* The foot of the curpos of the maze has been set ''''''''''''''''''''''''''''''''true''''''''''''''''''''''''''''''''. */
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifmaze[curpos.x][curpos.y].foot
=1;
ExpandedBlockStart.gifContractedBlock.gif}
  /**/ /* FootPrint */
None.gifPosType NextPos(PosType seat,
int  di)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
switch(di)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
case 1: seat.y++return seat; /**//* Eastward */
ExpandedSubBlockStart.gifContractedSubBlock.gif
case 2: seat.x++return seat; /**//* Southward */
ExpandedSubBlockStart.gifContractedSubBlock.gif
case 3: seat.y--return seat; /**//* Westward */
ExpandedSubBlockStart.gifContractedSubBlock.gif
case 4: seat.x--return seat; /**//* Northward */
InBlock.gif
default: seat.x=0; seat.y=0return seat;
ExpandedSubBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif}
  /**/ /* NextPos */
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* The key to the program. */
ExpandedBlockStart.gifContractedBlock.gif
/**/ /* Pre: The maze array & the startplace & the endplace.
InBlock.gifPost: Find the one traverse of the maze and perform the mazepath.
InBlock.gifUses: The ADT stack class.
ExpandedBlockEnd.gif
*/

None.gifStatus MazePath(PosType start,PosType end)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifPosType curpos;
InBlock.gif
int curstep;
InBlock.gifSElemType e;
InBlock.gifStack 
*s,stack;
InBlock.gifstack.
base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
InBlock.gif
if(!stack.base) Error("Overflow");
InBlock.gifstack.top
=stack.base;
InBlock.gifstack.stacksize
=STACK_INIT_SIZE;
InBlock.gifs
=&stack;
InBlock.gifcurpos
=start;
InBlock.gifcurstep
=1;
InBlock.gif
do
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(Pass(curpos))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifFootPrint(curpos);
InBlock.gife.ord
=curstep; e.seat=curpos; e.di=1;
InBlock.gifgotoxy((curpos.y
+1)*2,curpos.x+2);
InBlock.gifputch(
''''''''''''''''''''''''''''''''@'''''''''''''''''''''''''''''''');
ExpandedSubBlockStart.gifContractedSubBlock.gifdelay(
8000); /**//* pospone time. */
InBlock.gifPush(s,e);
ExpandedSubBlockStart.gifContractedSubBlock.gif
if(curpos.x==end.x&&curpos.y==end.y) /**//* Proceed recursively. */
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifDestroyStack(s);
InBlock.gif
return TRUE;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gifcurpos
=NextPos(curpos,1); /**//* Try next position. */
InBlock.gifcurstep
++;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(!StackEmpty(s))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gife
=Pop(s,e); /**//* Removed e from s. */
ExpandedSubBlockStart.gifContractedSubBlock.gif
while(e.di==4&&!StackEmpty(s)) /**//* Four directions have been checked
ExpandedSubBlockEnd.gifand s is not empty. 
*/

ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifMarkPrint(e.seat);
InBlock.gifgotoxy((e.seat.y
+1)*2,e.seat.x+2);
InBlock.gifputch(
''''''''''''''''''''''''''''''''@'''''''''''''''''''''''''''''''');
ExpandedSubBlockStart.gifContractedSubBlock.gifdelay(
8000); /**//* Pospone time. */
InBlock.gifgotoxy((e.seat.y
+1)*2,e.seat.x+2);
InBlock.gifputch(
'''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''');
ExpandedSubBlockStart.gifContractedSubBlock.gife
=Pop(s,e); /**//* Remove e from s. */
InBlock.gifcurstep
--;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockStart.gifContractedSubBlock.gif
if(e.di<4/**//* The current position hasnot been checked. */
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gife.di
++;
ExpandedSubBlockStart.gifContractedSubBlock.gifPush(s,e); 
/**//* Insert e into s. */
ExpandedSubBlockStart.gifContractedSubBlock.gifcurpos
=NextPos(e.seat,e.di); /**//* Try next position. */
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
while(!StackEmpty(s));
InBlock.gifDestroyStack(s);
InBlock.gif
return FALSE;
ExpandedBlockStart.gifContractedBlock.gif}
  /**/ /* MazePath */
None.gif
void  main()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifPosType start,end;
InBlock.gifCreatMaze();
InBlock.gifstart.x
=StartPlace.y;
InBlock.gifstart.y
=StartPlace.x;
InBlock.gifend.x
=EndPlace.y;
InBlock.gifend.y
=EndPlace.x;
InBlock.gif
if(MazePath(start,end))
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifgotoxy(
2,22);
InBlock.gifprintf(
"Path found\n");
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifgotoxy(
2,22);
InBlock.gifprintf(
"Path not found\n");
ExpandedSubBlockEnd.gif}

InBlock.gifgetch();
InBlock.gifclrscr();
ExpandedBlockEnd.gif}
 
None.gif
None.gif
posted on 2006-03-02 21:50 Aween's Blog 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/aween/archive/2006/03/02/341538.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值