基于栈结构的迷宫搜索算法,数据结构

             原理在数据结构书本(严蔚敏)上面将的很清楚了,通过一个方向标识符来进行方向的搜索:

 

stackmaze.h

#include <stdio.h>
#define INIT_STACK_SIZE 200
#define INCREMENT_SIZE 100
#define MAXSTEP 80
#define N 10

typedef struct SEAT
{
     int x;
     int y;
     
}Seat,*pSeat;

typedef struct tag_POSITION
{
     Seat seat;
     int dir;
}Position,*pPosition;

typedef struct STACK
{
     Position *base;
     Position *top;
     int stacksize;
     int dataCount;
}Stack,*pStack;


 

stack.c  栈的基本操作

#include "stackmaze.h"

int InitStack(pStack* S)
{
	(*S) = (pStack)malloc(sizeof(Stack));

     (*S)->base=(Position*)malloc(INIT_STACK_SIZE*sizeof(Position));

     if( ! (*S)->base )
          return 0;

     (*S)->top=(*S)->base;
     (*S)->stacksize=INIT_STACK_SIZE;
     (*S)->dataCount =0;
     return 1;
}


void push(pStack* S,Position pos)
{
     int i=(*S)->stacksize;
     if( (*S)->top - (*S)->base >= (*S)->stacksize )
     {
          (*S)->base=(Position*)realloc((*S)->base,
               ( (*S)->stacksize+INCREMENT_SIZE )*sizeof(Seat) );
          if( (*S)->base )
          {
               printf("alloc memory error!\n");
               return;
          }
     }
     *((*S)->top)=pos;
     (*S)->top++;
     (*S)->dataCount ++;
}

void pop(pStack* S,Position* e)
{

     if( (*S)->top == (*S)->base )
     {
               printf("stack is empty,can't pop!\n");
               return;
     }
     (*S)->top--;
     *e=*((*S)->top);
     (*S)->dataCount --;
}

int StackEmpty(pStack S)
{
     if( S->top == S->base )
     {
          return 1;
     }
     else
          return 0;
}

int InStack(pStack S,Seat e)
{
	Position *  p=NULL;
	p=S->base;
	while(p!=S->top)
	{
		if( p->seat.x == e.x  && p->seat.y ==e.y)
			return 1;
		p++;
	}
	return 0;
}

 


 

stackmaze.c  主程序

#include "stackmaze.h"

int maze[N][N]={
                    {0,0,0,0,0,0,0,0,0,0},
                    {0,1,1,0,0,1,1,1,0,0},
                    {0,1,1,0,0,1,1,1,0,0},
                    {0,1,1,0,0,1,0,0,1,0},
                    {0,1,1,1,1,1,0,0,1,0},
                    {0,1,1,0,0,0,1,0,1,0},
                    {0,1,0,0,1,1,1,1,1,0},
                    {0,1,1,1,1,0,0,1,1,0},
                    {0,1,0,0,1,1,1,1,1,0},
                    {0,0,0,0,0,0,0,0,0,0}};

Position start={1,1,1};
Position end={8,8,0};    
Seat footprint[MAXSTEP]={0};
pStack S;

/*显示地图以及走过的路,0表示墙壁,1表示通道,2表示走过路,3表示正确的路径*/
void ShowPrint()
{
	int i ,j ;

	for(i = 0 ; i < N ; i++)
	{
		for(j = 0 ; j < N ; j++)
		{
			if( maze[i][j]  == 0)
				printf("▇");
			else if( maze[i][j]  == 1)
				printf("□");
			else if( maze[i][j]  == 2)
				printf("☆");
		}
		printf("\n");
	}
}
/*==走过的路要保存起来,以后都不能走了===*/
void MarkPrint(Seat S)
{
	int i =0;

	for(i=0;footprint[i].x!=0;i++);

	footprint[i].x=S.x;

	footprint[i].y=S.y;
}

/*===查看当前位置能否通过,如果不是墙壁,且没有走过就表示能走==*/ 
int Passit(pSeat pos)
{
	int i ;

	if( maze[pos->x][pos->y] == 1 )
	    return 1;

	if( maze[pos->x][pos->y] == 0 )
	    return 0;	

	for(i=0;footprint[i].x!=0;i++)
	{
		if( pos->x == footprint[i].x &&  pos->y == footprint[i].y)
			return 0;
	}

     return 1;
}
/*==根据传入的位置以及方向信息,得到下一个点===*/
Position GetNext(Position pos)
{
	Position NextPos;
	NextPos=pos;

	switch(NextPos.dir)
	{
	case 1:	NextPos.seat.y ++ ;
		      break;

	case 2:	NextPos.seat.x ++ ;
		      break;

	case 3:	NextPos.seat.y -- ;
		      break;

	case 4:	NextPos.seat.x -- ;
		      break;
	}
	NextPos.dir = 1;

	return NextPos;
}
/*=====显示最后的路径地图====*/
void Show()
{
	int j ,i;
	Position curpos = {0};

	while( !StackEmpty(S) )
	{
		pop(&S,&curpos);
		maze[curpos.seat.x][curpos.seat.y] = 3;

	}


	for(i = 0 ; i < N ; i++)
	{
		for(j = 0 ; j < N ; j++)
		{
			if( maze[i][j]  == 0)
				printf("▇");
			else if( maze[i][j]  == 1)
				printf("□");
			else if(maze[i][j]  == 3)
			{
				printf("●");
			}
			else if( maze[i][j]  == 2)
				printf("☆");

		}
		printf("\n");
	}
	
			
}
/*==迷宫搜索=*/
int MazePass()
{
	Position curpos= start;
	int stepCount = 0;
	InitStack(&S);

 
	while(1)
	{	
		if( curpos.seat.x == end.seat.x &&  curpos.seat.y == end.seat.y )
		{	
			MarkPrint(curpos.seat);
			ShowPrint();
			return 1;
		}

		if( Passit( &(curpos.seat)) )
		{

			MarkPrint(curpos.seat);
			push(&S,curpos);

			maze[ curpos.seat.x][ curpos.seat.y] = 2;

			curpos = GetNext(curpos);
			stepCount++;
			//ShowPrint();

		}
		else
		{
			if( StackEmpty(S) )
				return 0;
			pop(&S,&curpos);			
			while(curpos.dir >= 4 && !StackEmpty(S) )
			{
				pop(&S,&curpos);
			}

	

			
			if( curpos.dir < 4 )
			{
				curpos.dir++;

				push(&S,curpos);
				
				curpos = GetNext(curpos);
				
				stepCount++;
			}
		//	getch();
		}
	}
	
}


void main()
{
	printf("☆means the pos that searched,● means the right way\n");
	MazePass();
	printf("\n");
	Show();
}

 

运行结果:


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值