#include<stdio.h> #include<errno.h> #include<malloc.h> #define PUZZLESIZE 10 //使用枚举定义四个方向 enum DIRECTION { EAST , SOUTH , WEST , NORTH }; //自己初始化的迷宫,用数组表示,1代表围墙,0代表路 int puzzle_arry[ PUZZLESIZE ][ PUZZLESIZE ] = { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 1 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 }; //定义坐标结构 typedef struct { int x; int y; }Coordinate; typedef struct { // int ord; //position in stack Coordinate cor; //Coordinate int dir; //0 is east , 1 is south , 2 is west , 3 north int flag; //0 is not steped , 1 is steped }SMaze; //将迷宫数据和栈关联起来 typedef SMaze SElemType; #define STACK_INIT_SIZE 100 #define STACK_INCRESE_SIZE 10 typedef struct { SElemType *base; SElemType *top; int stacksize; }Stack_t; int InitStack( Stack_t * s_t ) { s_t->base = ( SElemType * )malloc( STACK_INIT_SIZE * sizeof( SElemType ) ); if( s_t->base == NULL ) { perror("InitStack()"); return 1; } s_t->top = s_t->base; s_t->stacksize = STACK_INIT_SIZE; return 0; } int destroy( Stack_t * s_t ) { if( s_t->base == s_t->top ) { return 0; } else { free( s_t->base ); return 0; } return 1; } int ClearStack( Stack_t * s_t ) { s_t->top = s_t->base; return 0; } int StackEmpty( Stack_t s ) { if( s.top == s.base ) return 0; else return 1; } int GetTop( Stack_t s , SElemType * val ) { if( s.top == s.base ) return 1; else *val = *--s.top; return 0; } int Push( Stack_t * s_t , SElemType val ) { if( s_t->top - s_t->base >= s_t->stacksize ) { printf("fuck\n"); s_t->base = ( SElemType * )realloc( s_t->base , ( s_t->stacksize + STACK_INCRESE_SIZE ) * sizeof( SElemType ) ); if( s_t->base == NULL ) { perror( "Push()" ); return 1; } s_t->top = s_t->base + s_t->stacksize; s_t->stacksize += STACK_INCRESE_SIZE; } *s_t->top++ = val; return 0; } int Pop( Stack_t * s_t , SElemType *val ) { if( s_t->top == s_t->base ) return 1; else *val = *--s_t->top; return 0; } //根据传入的方向返回下一步应该走的坐标 Coordinate MoveNext(Coordinate cor , int dir ) { switch( dir ) { case EAST: cor.y++; return cor; break; case SOUTH: cor.x++; return cor; break; case WEST: cor.y--; return cor; break; case NORTH: cor.x--; return cor; break; } return cor; } //判断给出的坐标是否有路,返回追0表示有,1表示无 int Pass( int mase[ PUZZLESIZE ][ PUZZLESIZE ] , Coordinate cor ) { if( !mase[ cor.x ][ cor.y ] ) return 0; else return 1; } void PrintStack( int maze[ PUZZLESIZE ][ PUZZLESIZE ] ) { int i = 0 , j = 0; for( i = 0 ; i < PUZZLESIZE ; i++ ) { for( j = 0 ; j < PUZZLESIZE ; j++ ) { switch( maze[ i ][ j ] ) { case 0: printf(" ");//没走过,但是通路 break; case 2: printf("* ");//走过且走得通 break; case 3: printf("@ ");//走过但走不通 break; case 1: printf("# ");//障碍 break; } } printf("\n"); } } //这个可以打印出栈中的信息 void PrintDetail( Stack_t s ) { SMaze *top , *base; base = s.base; top = s.top; while( base != top ) { printf("x:%d,y:%d,dir:%d\n" , base->cor.x , base->cor.y , base->dir ); base++; } getchar(); } //思路:判断一个点是否能通过,如果能则将这个点存储入栈,并将下一步默认为东 //如果不能通过则判断栈是否为空,不为空且这个点还有其余的方向未走那么就先把这个元素出栈再改变方向入栈, //然后进入下一轮的循环 int MathPath( int mase[ PUZZLESIZE ][ PUZZLESIZE ] , Coordinate start , Coordinate end ) { // int curstep = 1; int dir = 0; Coordinate curcor; //目前所在的位置的坐标 SMaze sm; //目前所在的位置及方向 Stack_t s; //用来存储的栈 InitStack( &s ); curcor = start; do { if( !Pass( mase , curcor ) ) { sm.cor.x = curcor.x; sm.cor.y = curcor.y; mase[ sm.cor.x ][ sm.cor.y ] = 2; sm.dir = dir; // sm.flag = 1; Push( &s , sm ); if( curcor.x == end.x && curcor.y == end.y ) { printf("find it!\n"); PrintStack( mase ); destroy( &s ); return 0; } curcor = MoveNext( curcor , EAST ); } else { if( StackEmpty( s ) ) { Pop( &s , &sm ); //之所以使用while是因为存在连续几步都是最后一个方向的情况 while( sm.dir == 3 && StackEmpty( s ) ) { mase[ sm.cor.x ][ sm.cor.y ] = 3; Pop( &s , &sm ); } if( sm.dir < 3 ) { sm.dir++; Push( &s , sm ); curcor.x = sm.cor.x; curcor.y = sm.cor.y; curcor = MoveNext( curcor , sm.dir ); } } } // PrintDetail( s ); }while( StackEmpty( s ) ); destroy( &s ); return 1; } int main() { Coordinate start = { 1 , 1 }; Coordinate end = { 8 , 8 }; if( MathPath( puzzle_arry , start , end ) ) printf("Not find it!\n"); return 0; }