数据结构C语言迷宫

这是大一数据结构迷宫作业,用c实现

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 12
#define ERROR -1
#define OK   1
#define FALSE 0
#define TRUE  1
typedef enum{RIGHT,DOWN,LEFT,UP
			}diraction;
typedef enum{YES,NO
			}MarkTag;
typedef struct position   //迷宫中位置的坐标
	{   
		int x;
   		int y;
	}Position;
typedef struct
{  	
	int order;          //当前位置在路径中的序号
  	Position coordinate;       //当前位置在迷宫中的坐标
  	diraction orientation;       //从当前位置走到下一位置的方向
}SElemType;              //栈元素的类型
typedef struct
{ 
	SElemType *elem;
  	int top;
}Stack;
char maze[MAXSIZE][MAXSIZE]=
	 {
		 {'*','*','*','*','*','*','*','*','*','*','*'},
		 {'*','0','0','0','0','0','0','1','0','0','*'},		 
		 {'*','0','0','1','0','0','0','1','1','0','*'},		 
		 {'*','1','0','1','0','1','1','0','0','0','*'},		 
		 {'*','0','0','1','1','0','0','0','0','0','*'},		 
		 {'*','0','0','1','1','0','0','0','0','0','*'},		 
		 {'*','0','1','1','0','0','1','0','0','0','*'},		 
		 {'*','0','1','1','1','0','1','1','0','0','*'},		 
		 {'*','0','0','0','0','0','0','0','0','0','*'},		 
		 {'*','*','*','*','*','*','*','*','*','*','*'}
 	}; 	
int InitStack(Stack *S)             //创建一个空栈
{
	 S->elem=(SElemType*)malloc(MAXSIZE*MAXSIZE*sizeof(SElemType));
	 if(!S->elem)  
	 	return ERROR;
	 S->top=0;
	     return OK;
}  
int Push(Stack *S,SElemType e)     //元素e入栈
{   
	if(S->top>=MAXSIZE*MAXSIZE)  
	   return ERROR;
 	S->elem[S->top++]=e;
 		return OK;
} 
int Pop(Stack *S,SElemType *e) //栈顶元素出栈,由e带回栈顶元素
{   
	if(S->top<=0)
	     return ERROR;
  	*e=S->elem[--S->top];
  		 return OK;
}
int Empty(Stack S)    //判断栈是否为空
{  
	if(S.top==0)
	    return TRUE;
   	else   
   		return FALSE;
} 
int createMaze(Position *startpos,Position *endpos)
{ 
	Position start,end;
    printf("\n\n");
 	printf("请输入迷宫入口的位置:");
 	scanf("%d%d",&start.y,&start.x);
	printf("请输入迷宫出口的位置:");
 	scanf("%d%d",&end.y,&end.x);
     *startpos=start; *endpos=end;
 return OK;
}  //createMaze
int canPass(Position canpos)
{    
	if(maze[canpos.x][canpos.y]=='0')  
	   return TRUE;
     	return FALSE;
}   //canPass
void markPos(Position canpos,MarkTag tag)     //为已经探索过的位置加标记
{  
  	switch(tag)
 	{ 
	  case YES: maze[canpos.x][canpos.y]='R'; break;   //路径标记
	  case NO:  maze[canpos.x][canpos.y]='X'; break;   //死胡同标记
 	}
}  
//根据当前的位置坐标和下一步要探索的方向dir求下一步要走的位置坐标
Position nextPos(Position canpos,diraction dir)
{    
Position nextpos;
switch(dir)
  {  case RIGHT:nextpos.x=canpos.x ;nextpos.y =canpos.y +1; break;
     case DOWN :nextpos.x=canpos.x+1 ;nextpos.y =canpos.y; break;
     case LEFT :nextpos.x=canpos.x ;nextpos.y =canpos.y -1; break;
     case UP :nextpos.x=canpos.x-1 ;nextpos.y =canpos.y;  break;
  }
  return nextpos;
}
diraction nextDir(diraction dir)//回溯到上个节点的下个方向
{  switch(dir)
  {   case RIGHT: return DOWN;
      case DOWN : return LEFT;
      case LEFT: return UP;
  }
}
int Solve(Stack *S,Position start,Position end)
{//若迷宫中存在从入口start到出口end的通道,则求得一条存放在栈S中,并返回TRUE,若迷宫中不存在从入口start到出口end的通道,并返回FALSE
 Position canpos;
 SElemType e;
 int canstep=1;   //共用的步数
if(InitStack(S)==ERROR)  
	return FALSE;
 	canpos=start;
do{
 	if(canPass(canpos)){      //当前位置可以通过
	     markPos(canpos,YES);   //留下足迹
	     e.order=canstep;e.coordinate=canpos;e.orientation=RIGHT;
	     Push(S,e);             //当前位置加入路径
    if(canpos.x==end.x&&canpos.y==end.y)   //当前位置是出口
      	return TRUE;
     canpos=nextPos(canpos,RIGHT);
     canstep++;
  	}	
  	else{              //当前位置不能通过
		if(!Empty(*S))
			{
 			if(Pop(S,&e)==ERROR)
 	   			return FALSE;
     		while(e.orientation==UP&&!Empty(*S)){
     //四个方向都试探过,没有找到通路也不能继续探索,则回溯
      		canpos=e.coordinate;
      		markPos(canpos,NO);
		    if(Pop(S,&e)==ERROR)  
		    	return FALSE;
					}
	    	if(e.orientation!=UP){   //四个方向还没有试探完
		      e.orientation=nextDir(e.orientation);
		      Push(S,e);  //换下一个方向探索
		      canpos=nextPos(e.coordinate,e.orientation);
				}				
		 	}	
 		}	
 	}
while(!Empty(*S));
	return FALSE;
}
void main()
{   
	int i,j;
	printf("迷宫地图,0位路径,1为墙壁,X为不能通过,R为可以通过路径,*为墙壁\n"); 
    for(i=0;i<10;i++){
		 	for(j=0;j<11;j++)
	    	printf("%c ",maze[i][j]);	    	
	        printf("\n");
	        }
	Position startPos,endPos;
    Stack path; 
	SElemType e;
	if(createMaze(&startPos,&endPos)==ERROR) return ;
 		Solve(&path,startPos,endPos);
	while(!Empty(path)){    //输出出口到入口的路径
  	Pop(&path,&e);
  	printf("(%d,%d)",e.coordinate.x,e.coordinate.y);
 }
	printf("\n");//输出迷宫的图形
	    for(i=0;i<11;i++)
	     {
		 	for(j=0;j<10;j++)	 	
	    	printf("%c ",maze[i][j]);    	
	        printf("\n");
	        }
}
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值