数据结构的应用------------迷宫求解

 
/*------------------------------------------------------------------------
	程序说明:迷宫求解

	用栈实现迷宫寻找一条可行的路径


	---------------------------------------------------------------------*/









/*----------------------------------------------------------------------
	文件:useMaze.c
	功能:提供测试代码

	------------------------------------------------------------------*/

#include<stdio.h>
#include"maze.h"



int main(void)
{
	MazeType Maze[MAX][MAX] ;
	PosType start ,end ;



	InitMaze(Maze) ;       //初始化迷宫
	

	InitStartEnd(&start,&end) ;     //初始化入口出口

	printf("\n\n\n\n") ;

	if(MazePath(Maze,start,end))
	{
		puts("能找到一条路径到出口") ;
	}
	else
	{
		puts("不能找到一条路径到出口") ;
	}
	

	return 0 ;
}





/*----------------------------------------------------------
	文件:maze.h
	功能:提供函数声明以及一些常量标识符、类型定义


	--------------------------------------------------------*/




#ifndef MAZE_H_
#define MAZE_H_



/*--------------------------------------------------------*/
/*路径的一些标识*/

#define PASS 0
#define USE 1
#define BLOCK 2
#define DIE 3
#define MAX 5

#define ISPASS(Pos)	 (Pos.x >= 0 && Pos.x < MAX && Pos.y>= 0 && Pos.y < MAX && Maze[Pos.x][Pos.y] == PASS )

/*-----------------------------------------------------*/




/*--------------------------------------------------------*/
/*函数的一些返回值定义符*/
#define OK 1
#define FALSE 0
#define TRUE 1
#define OVERFLOW -1
#define ERROR 0

typedef int Status ;

/*-----------------------------------------------------*/






/*-----------------------------------------------------*/
/*一些类型的定义*/

typedef int MazeType ;  //迷宫类型,二维整型数组实现

typedef struct { int x; int y;}	PosType ;   // 路径的坐标

typedef struct                           //关于每一块路径的信息
{
	int ord ;
	PosType seat ;
	int di ;
} SElemType ;


typedef struct                       //栈
{
	SElemType *base ;
	SElemType *top ;
	int stacksize ;
}	MazeStack ;



#define STACK_ININ_SIZE  100
#define STACKINCREMENT 10

/*-----------------------------------------------------*/




/*-----------------------------------------------------*/
//fuction defined

void InitStartEnd(PosType *start,PosType *end) ;//InitStartEnd 

Status DestoryStack(MazeStack *S) ; //DestoryStack

Status Push(MazeStack *S,SElemType e) ;  //Push

Status Pop(MazeStack *S, SElemType *e) ;//Pop

Status StackEmpty(MazeStack S) ;//StackEmpty

void MarkPrint(PosType P , MazeType (*Maze)[MAX]); //MarkPrint

PosType NextPos(PosType *P, int d) ;//NextPos 

void FootPrint(const PosType Pos,MazeType (*Maze)[MAX])  ;//FootPrint

Status Pass(const PosType Pos,MazeType (*Maze)[MAX]); //Pass

Status InitStack(MazeStack *S) ;//InitStack

void PrintMaze(MazeType (*Maze)[MAX]); //PrintMaze

void InitMaze(MazeType (*Maze)[MAX]); //InitMaze

Status MazePath(MazeType (*Maze)[MAX],PosType start,PosType end) ;//MazePath

/*-----------------------------------------------------*/




#endif






/*------------------------------------------------
	文件:Maze.c
	功能:提供接口的实现


	----------------------------------------------*/






#include<stdio.h>
#include<stdlib.h>    //提供realloc
#include"maze.h"





void InitStartEnd(PosType *start,PosType *end)
{
	printf("请输入入口的横坐标: ") ;
	scanf("%d",&(start->x )) ;

	printf("请输入入口的纵坐标:") ;
	scanf("%d",&(start->y )) ;

	printf("请输入出口的横坐标: ") ;
	scanf("%d",&(end->x )) ;

	printf("请输入出口的纵坐标:") ;
	scanf("%d",&(end->y )) ;

}//InitStartEnd 


Status DestoryStack(MazeStack *S)
{
	free(S->base) ;
	S->base = S->top = NULL ;

	return OK ;
}//DestoryStack

Status Push(MazeStack *S,SElemType e)
{
	if(S->top -S->base  >= S->stacksize )
	{
		S->base = (SElemType *)realloc(S->base ,(S->stacksize +STACKINCREMENT) * sizeof(SElemType)) ;
		if(NULL == S->base )
		{
			puts("ERROR") ;
			exit(OVERFLOW) ;
		}

		S->top = S->base + S->stacksize ;
		S->stacksize += STACKINCREMENT ;
	}

	*(S->top )++ = e ;
	return OK ;

}//Push


Status Pop(MazeStack *S, SElemType *e)
{
	if(S->top == S->base )
		return ERROR ;
	
	*e = * --S->top ;

	return OK ;
}//Pop



Status StackEmpty(MazeStack S)
{

	return (S.base == S.top ) ;

}// StackEmpty


void MarkPrint(PosType P , MazeType (*Maze)[MAX])     //注意二维数组的地址是如何传递给函数的
{	
	Maze[P.x][P.y] = DIE ;
}//MarkPrint




PosType NextPos(PosType *P, int d)
{
	switch(d)
	{
	case 1 : P->y++ ; break ;
	case 2 : P->x++ ; break ;
	case 3 : P->y-- ; break ;
	case 4 : P->x-- ; break ;
	}
	
	return *P ;

} //NextPos


void FootPrint(const PosType Pos,MazeType (*Maze)[MAX])
{
//调试使用	printf(" x = %d,y = %d\n",Pos.x,Pos.y ) ; 
	Maze[Pos.x][Pos.y] = USE ;	
}//FootPrint



Status Pass(const PosType Pos,MazeType (*Maze)[MAX])
{
	return ISPASS(Pos) ;
}//Pass


Status InitStack(MazeStack *S)
{
	S->base = (SElemType *) malloc(STACK_ININ_SIZE * sizeof(SElemType) ) ;
	
	if(NULL == S->base)
	{
		puts("ERROR") ;
		exit(OVERFLOW) ;
	}

	S->top = S->base  ;
	S->stacksize = STACK_ININ_SIZE ;

	return OK ;

}//InitStack


void PrintMaze(MazeType (*Maze)[MAX])
{
	int i = 0 ;
	int j = 0 ;

	printf("\n迷宫现状:\n") ;
	printf("\n0代表通路、1代表走过、2代表墙壁、3代表死胡同\n") ;

	for(i = 0 ; i < MAX ; i++)
	{
		for(j = 0 ; j < MAX ; j++)
		{
			printf("%2d",Maze[i][j]) ;
		}
		printf("\n") ;
	}
}//PrintMaze


void InitMaze(MazeType (*Maze)[MAX])
{
	int i = 0 ;
	int j = 0 ;

	printf("\n0代表通路、2代表墙壁\n") ;

	for(i = 0 ; i < MAX ; i++)
	{
		for(j = 0 ; j < MAX ; j++)
			Maze[i][j] = 2 ;
	}

	PrintMaze(Maze) ;

	printf("\n0代表通路、2代表墙壁\n") ;
	printf("请初始化迷宫:\n") ;

	for(i = 0 ; i < MAX ; i++)
	{
		printf("请初始化第%d行: \n",i+1) ;
		for(j = 0 ; j < MAX ; j++)
			scanf("%d",&Maze[i][j]) ;
	}


	PrintMaze(Maze) ;

}//InitMaze


Status MazePath(MazeType (*Maze)[MAX],PosType start,PosType end)
{
	SElemType	e ;
	MazeStack S ;
	PosType curpos ;

	int curstep ;

	InitStack(&S) ;     //初始化栈

	curpos = start ;  // start.x= start.y = 0

	curstep = 1 ;

	do
	{
		if(Pass(curpos,Maze))
		{
			FootPrint(curpos,Maze) ;
			e.di = 1 ;
			e.seat = curpos ;
			e.ord = curstep ;

			Push(&S,e) ;

			if(curpos.x == end.x && curpos.y == end.y )
			{
				PrintMaze(Maze) ;
				return TRUE ;
			}  //if

			curpos = NextPos(&curpos,1) ;
			curstep++ ;
			
		} //if

		else
		{
			if(!StackEmpty(S))
			{
				Pop(&S,&e) ;

				while(e.di == 4 && !StackEmpty(S))
				{
					MarkPrint(e.seat,Maze) ;
					Pop(&S,&e) ;
				}  //while

				if(e.di <4)
				{
					e.di++ ;
					Push(&S,e) ;
					curpos = NextPos(&(e.seat) ,e.di) ;
				} //if
			} //if
		}  //else
	}while(!StackEmpty(S)) ;   //while


	PrintMaze(Maze) ;

	return FALSE ;

} // MazePath

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
/* ****迷宫算法求解************* */ /**计目标:教学演示**************/ #include<stdio.h> #define rows 10 #define cols 10 typedef struct {int row; int col; }PosType;/* //坐标点结构 */ typedef struct {int ord;/*//通道块在路径上的“序号” */ PosType seat;/*//通道块在迷宫中的“坐标位置”*/ int di;/*//从此通道快走向下一通道块的“方向” */ }SElemType;/*//栈的元素类型 */ typedef struct {SElemType *base; SElemType *top; int stacksize; }SqStack;/*//堆栈结构 */ void InitStack(SqStack &s)/*//初始化堆栈 */ { s.base=(SElemType *)malloc(100*sizeof(SElemType)); if(!s.base) return NULL; s.top=s.base; s.stacksize=100; } int StackEmpty(SqStack s)/* //栈空判别*/ {return(s.top==s.base); } void Pop(SqStack &s ,SelemType &e)/*//弹栈 */ {e=*--s.top); } void Push(SqStack &s,SElemType e)/*//将元素压入堆栈*/ { *s.top++=e; } /*static int maze[rows][cols]= {{0,0,0,0,0,0,0,0,0,0}, {0,1,1,0,1,1,1,0,1,0}, {0,1,1,0,1,0,1,0,1,0}, {0,1,1,0,1,0,0,1,1,0}, {0,1,1,0,0,1,1,1,1,0}, {0,1,1,1,0,1,1,1,1,0}, {0,1,0,1,1,1,0,1,1,0}, {0,1,0,0,0,1,0,0,1,0}, {0,0,1,1,1,1,1,1,1,0}, {0,0,0,0,0,0,0,0,0,0}, }; */ /* //初始迷宫数据(1-通,0-不通)*/ static int maze[rows][cols]= {{0,0,0,0,0,0,0,0,0,0}, {0,1,1,0,1,1,1,0,1,0}, {0,1,1,0,1,0,1,1,1,0}, {0,1,1,1,0,0,0,0,1,0}, {0,1,0,0,0,1,1,1,1,0}, {0,1,0,1,0,1,0,0,0,0}, {0,1,0,1,1,1,0,1,1,0}, {0,1,0,1,0,0,0,0,1,0}, {0,0,1,1,1,1,1,1,1,0}, {0,0,0,0,0,0,0,0,0,0}, }; /* //初始迷宫数据(1-通,0-不通)*/ static int foot[10][10]={0};/*//标记某点是否走过(1-走过,0-未走过)*/ void printpath(SqStack &s)/*//打印迷宫通路*/ {int i,j; SElemType e; while(!StackEmpty(s)) { Pop(s,e); foot[e.seat.row][e.seat.col]=1; } for(i=0;i<10;i++) {printf("\n"); for(j=0;j<10;j++) if(foot[i][j]) printf(" # "); else printf(" . "); } } int Pass(PosType pos)/*//判断当前的通道块是否可通*/ { return(maze[pos.row][pos.col]); }; void FootPrint(PosType pos) { maze[pos.row][pos.col]=0; } PosType NextPos(PosType curpos,int dir)/*//取当前通道块的下一个通道块*/ { switch(dir) {case 1: curpos.row++; break; case 2: curpos.col++; break; case 3: curpos.row--; break; case 4: curpos.col--; } return curpos;/*//将下一个通道块变为当前通道块*/ } int END(PosType curpos,PosType end) {return(curpos.row==end.row && curpos.col==end.col); } void MazePath(SqStack &s,PosType start,PosType end) {PosType curpos,nextpos; int curstep; SElemType e; SqStack *s; s=InitStack(); curpos=start; curstep=1; do{ if(Pass(curpos)) {FootPrint(curpos); e.ord=curstep;e.seat=curpos;e.di=1; Push(s,e); if(END(curpos,end)) return s; curpos=NextPos(curpos,1); curstep++; }/* end of if */ else { if(!StackEmpty(s)) { e=Pop(s); while(e.di==4 && !StackEmpty(s)) {FootPrint(e.seat);/* The same fuction as MarkPrint ? */ e=Pop(s); }/* end of while */ if(e.di<4) {e.di++;Push(s,e); curpos=NextPos(e.seat,e.di); } /* end of if */ } /* end of if */ } /* end of else */ }while(!StackEmpty(s)); curstep=0; return NULL; } void main() {SqStack *s; static PosType start={1,1},end={8,8}; s=MazePath(start,end); if(s) printpath(s); else printf("\n NO find the path!"); }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值