//头文件
stack.h
#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STACK_INIT_SIZE 30
#define STACK_INCR_SIZE 5
typedef struct
{
int x ;
int y ;
} PosType ;
typedef struct
{
PosType position ;
int direction ;
} ElemType ;
typedef struct Sqstack
{
ElemType* Sbase ;
ElemType* Stop ;
int StackSize ;
} Sqstack ;
int InitStack( Sqstack* s ) ;
int DestroyStack( Sqstack* s ) ;
int GetTop( Sqstack* s , ElemType* e ) ;
int GetLength( Sqstack* s ) ;
int Push( Sqstack* s , ElemType e ) ;
int Pop( Sqstack* s , ElemType* e ) ;
int IsEmpty( Sqstack* s ) ;
int ClearStack( Sqstack* s ) ;
int TraveserStack( Sqstack* s ) ;
#endif // STACK_H_INCLUDED
maze.h
#ifndef MAZE_H_INCLUDED
#define MAZE_H_INCLUDED
#include "stack.h"
int MazePath(Sqstack* s ) ;
ElemType NextPos( ElemType CurrPos ) ;
#endif // MAZE_H_INCLUDED
//函数实现
maze.c
#include "maze.h"
#define ROW 10
#define COL 10
int maze[ROW][COL]=
{
{ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } ,
{ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 } ,
{ 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 } ,
{ 0 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 1 , 0 } ,
{ 0 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 0 } ,
{ 0 , 1 , 1 , 1 , 0 , 1 , 1 , 1 , 1 , 0 } ,
{ 0 , 1 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 0 } ,
{ 0 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 , 0 } ,
{ 0 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 1 , 1 } ,
{ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }
} ;
PosType start = { 1 , 0 } ;//入口
PosType end = { 8 , 9 } ;//出口
int MazePath( Sqstack* s )
{
ElemType currpos ;//当前位置的信息
ElemType e ;//中间变量
currpos.position = start ;
currpos.direction = 0 ;
do
{
if( maze[currpos.position.x][currpos.position.y] == 1 )
{
maze[currpos.position.x][currpos.position.y] = 2 ;
e.position.x = currpos.position.x ;
e.position.y = currpos.position.y ;
e.direction = 0 ;
Push( s , e ) ;
// TraveserStack( s ) ;
if( ( currpos.position.x == end.x ) && ( currpos.position.y == end.y ) )
{
printf( "success to find the export!\n" ) ;
return 0 ;
}
else
{
currpos = NextPos( currpos ) ;
currpos.direction = 0 ;
}
}
else
{
if( !IsEmpty( s ) )
{
Pop( s , &e ) ;
//printf("[%d , %d ] , %d\n" , e.position.x , e.position.y , e.direction ) ;
while( ( e.direction == 3 ) && ( !IsEmpty( s ) ) )
{
maze[e.position.x][e.position.y] = -1 ;
Pop( s , &e ) ;
// printf("[%d , %d ] , %d\n" , e.position.x , e.position.y , e.direction ) ;
}
if( e.direction < 3 )
{
e.direction++ ;
Push( s , e ) ;
currpos = NextPos( e ) ;
currpos.direction = 0 ;
// printf("[%d , %d ]%d\n" ,currpos.position.x , currpos.position.y , e.direction ) ;
}
}
}
}
while( !IsEmpty( s ) ) ;
printf("fail to find the export!\n") ;
exit( 1 ) ;
}
ElemType NextPos( ElemType CurrPos )
{
ElemType nepos ;
if( CurrPos.direction == 0 )
{
nepos.position.x = CurrPos.position.x ;
nepos.position.y = CurrPos.position.y + 1 ;
}
else if( CurrPos.direction == 1 )
{
nepos.position.x = CurrPos.position.x + 1 ;
nepos.position.y = CurrPos.position.y ;
}
else if( CurrPos.direction == 2 )
{
nepos.position.x = CurrPos.position.x ;
nepos.position.y = CurrPos.position.y -1 ;
}
else if( CurrPos.direction == 3 )
{
nepos.position.x = CurrPos.position.x - 1 ;
nepos.position.y = CurrPos.position.y ;
}
return nepos ;
}
stack.c
#include "stack.h"
int InitStack( Sqstack* s )
{
s->Sbase = ( ElemType* )malloc( STACK_INIT_SIZE * sizeof( ElemType ) ) ;
if( !s->Sbase )
{
printf( " OVERFLOW !\n" ) ;
exit( 1 ) ;
}
memset( s->Sbase , 0 , STACK_INIT_SIZE * sizeof( ElemType ) ) ;
s->Stop = s->Sbase ;
s->StackSize = STACK_INIT_SIZE ;
return 0 ;
}
int DestroyStack( Sqstack* s )
{
ElemType* p = --s->Stop ;
while( s->Stop != s->Sbase )
{
s->Stop-- ;
free( p-- ) ;
}
free( s->Sbase ) ;
free( p ) ;
return 0 ;
}
int Push( Sqstack* s , ElemType e )
{
if( s->Stop - s->Sbase >= s->StackSize )
{
s->Sbase = ( ElemType* )realloc( s->Sbase , ( s->StackSize + STACK_INCR_SIZE ) * sizeof( ElemType ) ) ;
if( !s->Sbase )
{
printf( " OVERFLOW !\n" ) ;
exit( 1 ) ;
}
s->Stop = s->Sbase + STACK_INIT_SIZE ;
s->StackSize += STACK_INCR_SIZE ;
}
*s->Stop++ = e ;
return 0 ;
}
int GetLength( Sqstack* s )
{
return s->Stop - s->Sbase ;
}
int GetTop( Sqstack* s , ElemType* e )
{
if( s->Sbase == s->Stop )
{
printf( "ERROR!\n") ;
exit( 1 ) ;
}
*e = * ( s->Stop - 1 ) ;//为什么改成 e = --s->Stop ; 不行?
// printf( "(%d , %d)\t%d\n" , e->position.x , e->position.y , e->direction ) ;
return 0 ;
}
int Pop( Sqstack* s , ElemType* e )
{
if( s->Sbase == s->Stop )
{
printf( "the stack is empty !\n" ) ;
exit( 1 ) ;
}
*e = *--s->Stop ;
return 0 ;
}
int IsEmpty( Sqstack* s )
{
if( s->Sbase == s->Stop )
{
return 1 ;
}
return 0 ;
}
int ClearStack( Sqstack* s )
{
memset( s->Sbase , 0 , s->StackSize * sizeof( ElemType ) ) ;
s->Stop = s->Sbase ;
return 0 ;
}
int TraveserStack( Sqstack* s )
{
if( s->Stop == s->Sbase )
{
printf( "the stack is empty !\n" ) ;
exit( 1 ) ;
}
ElemType* p = s->Sbase ;
while( p != s->Stop )
{
printf( "( %d , %d ) %d\n" , p->position.x , p->position.y , p->direction ) ;
p++ ;
}
printf( "\n" ) ;
return 0 ;
}
主函数
#include "maze.h"
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
Sqstack s ;
InitStack( &s ) ;
MazePath( &s ) ;
TraveserStack( &s ) ;
return 0 ;
}