栈-迷宫求解

栈-迷宫求解

魔改栈

/*
*项目名: Stack_Maze
*文件名: Stack_Maze.h
*创建者: CaoPengCheng
*创建时间:2021-4-1
*描述: 迷宫求解魔改栈
*函数:
	Status Init_SqStack(&S); 初始化操作
	Status Push_SqStack(&S,e); 插入
	ElemType GetTop_Stack(&S,i); 取顶元素
	ElemType Pop_Stack(&S); 出栈
	Status Empty_Stack(&S); 判空
	int Length_Stack(&S); 栈长度
*/
#include "stdio.h"
#include "stdlib.h"
typedef int DirectiveType;        //下一个通道方向  

#define ElemType int
#define Status int 
#define OK 1
#define ERROE 0

#define RANGE 100                 //迷宫大小  
#define STACK_INIT_SIZE 100       //定义栈的初始大小
#define STACKINCREMENT    10      //定义栈的储存增量,在栈长度越界时 
#define RANGE 100                 //迷宫大小  
#define ROW 10                    //迷宫的行数
#define COL 10                    //迷宫的列数    

typedef struct        {
    int m, n;
    int arr[RANGE][RANGE];       //迷宫数组
}MazeType;                       //迷宫的类型

typedef struct{
    int row;                     //迷宫中的行
    int col;                     //迷宫中的列
}PosType;                        //坐标(row,col)

typedef struct{
    int step;                    //当前位置在路径上的"序号"
    PosType seat;                //当前的坐标位置
    DirectiveType di;            //往下一个坐标位置的方向
}SElemType;                      //栈的元素类型

typedef struct{
    SElemType *base;             //栈底
    SElemType *top;              //栈顶
    int stacksize;               //栈的大小
}MStack; 


//初始化
Status Init_SqStack(MStack *S){
	S->base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
	S->top=S->base;
	S->stacksize=STACK_INIT_SIZE;
	return OK;
}

//插入
Status Push_SqStack(MStack *S,SElemType e){
	//printf("入栈\n");
	//栈满,追加存储空间
	if(S->top-S->base >= S->stacksize){
		S->base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
		S->top=S->base+S->stacksize;
		S->stacksize+=STACKINCREMENT;
	}
	*(S->top++)=e;
	return OK;
}

//取顶元素
SElemType GetTop_Stack(MStack *S){
	SElemType p;
	if(S->top==S->base){	
		printf("栈空\n");
		return;
	}
	p=*(S->top-1);
	return p;
}

//出栈
SElemType Pop_Stack(MStack *S){
	//printf("出栈\n");
	if(S->top==S->base){	
		printf("栈空\n");
		return;
	}
	return *(--S->top);
}

//求栈长度
int Length_Stack(MStack *S){
	return S->top-S->base;
}

//判空
Status Empty_Stack(MStack *S){
	if(S->top == S->base)
		return OK;
	else
		return ERROE;
}

迷宫运算

/*
*项目名: Stack_Maze
*文件名: Maze.h
*创建者: CaoPengCheng
*创建时间:2021-4-1
*描述: 迷宫求解实现
*函数:
	Status MazePath(PosType start,PosType end); 探索主函数
	Status Pass(MStack *S,PosType curpos);判断此位置是否走过
	Status FootPrint(PosType curpos); 地图留下足迹
	Status Equls(PosType pos1,PosType pos2); 对比两个节点是否相同
	PosType Next(PosType pos); 下一个节点,当前位置顺时针旋转
	Status MarkPrint(PosType curpos); 地图留下不能通过足迹
	Status InitMaze(MazeType *maze, int a[ROW][COL], int row, int col);//初始化迷宫
*/
#include "Stack_Maze.h"

//初始化迷宫
Status InitMaze(MazeType *maze, int a[ROW][COL], int row, int col)  //初始化迷宫
{
    int i, j;                            //设置迷宫maze的初值,包括加上边缘一圈的值
    for (i = 1; i<row - 1;i++)         
    {
       for (j = 1; j<col - 1; j++)
       {
           maze->arr[i][j] = a[i][j];
       }
    }                                          
    for (j = 0; j<row; j++)                  
       maze->arr[0][j] = maze->arr[row-1][j] = 1;
    for (i = 0; i<col; i++)
       maze->arr[i][0] =maze->arr[i][col - 1] = 1;
    return OK;
}

Status Pass(MazeType *maze, PosType curpos){                                                 
    if (maze->arr[curpos.row][curpos.col]== 0)     
       return OK;
    else
		return ERROE;
}

Status FootPrint(MazeType *maze, PosType curpos){                                                  
    maze->arr[curpos.row][curpos.col]= 6;           
    return OK;
}

Status MarkPrint(MazeType *maze, PosType curpos){                                                  
    maze->arr[curpos.row][curpos.col]= 4;          
    return OK;
}

Status PosEqual(PosType pos1, PosType pos2){                                               
    if (pos1.row == pos2.row && pos1.col == pos2.col)    
       return OK;                            
    else
		return ERROE;
}

PosType NextPos(PosType curpos, DirectiveType di)  {                                                  
    PosType pos = curpos;
    switch (di){
		case 1:pos.col++;break;//右
		case 2:pos.row++;break;//下
		case 3:pos.col--;break;//左
		case 4:pos.row--;break;//上
    }
    return pos;
}

SElemType CreateSElem(int step, PosType pos, DirectiveType di){ 
    SElemType e;
    e.step = step;
    e.seat = pos;
    e.di = di;
    return e;
}

Status MazePath(MazeType *maze, PosType start, PosType end){                              
    MStack *s,S;                  
    SElemType e;                 
    PosType curpos = start;             
	int curstep = 1;   
	s=&S;
	Init_SqStack(s); 
                                
    do {
       if (Pass(maze, curpos))
       {    
           FootPrint(maze,curpos);              
           e = CreateSElem(curstep,curpos, 1);   
           Push_SqStack(s, e);                            
           if (PosEqual(curpos,end))             
              return OK;                        
           curpos = NextPos(curpos,1);            
           curstep++;                             
       }
       else
       {                                         
           if (!Empty_Stack(s))
           {
              e=Pop_Stack(s);
              while (e.di == 4&& !Empty_Stack(s)) 
			  {
                  MarkPrint(maze,e.seat);
                  e=Pop_Stack(s);                      
              }
              if (e.di<4)
              {
                  e.di++;                       
                  Push_SqStack(s, e);
                  curpos =NextPos(e.seat, e.di); 
              }
		   }
       }
    } while (!Empty_Stack(s));
	return ERROE;
}




测试

int Text_Maze_Use2(){
    int i, j;                
    PosType start, end;      //开始,终点坐标
    MazeType MAZE,*maze;           
    int a[ROW][COL] = {                 //原始迷宫,其中'1'表示墙,'0'表示通道
       { 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 }
    };
	maze=&MAZE;

    printf("\n-------------------------------------------------\n");
    printf("\n原始迷宫如下:\n");
    printf("(其中'1'表示墙,'0'表示通道)\n");
    for (i = 0; i<10; i++)           //双重循环输出原始迷宫
    {
       for (j = 0; j<10; j++){
           printf("%d ",a[i][j]);
       }
       printf("\n");
    }
    InitMaze(maze, a, ROW, COL);                 //初始化迷宫
    start.row = 1;                               //给定迷宫起点坐标
    start.col = 1;                               //(1,1)
    end.row = 8;                                 //给定迷宫终点坐标   
    end.col = 8;                                 //(8,8)
    if (MazePath(maze, start,end))              //如果找到一条路径
    {
       printf("\n-------------------------------------------------\n");
       printf("\n解迷宫路径如下:\n");
       printf("(其中'6'表示求解路径,'4'表示死胡同)\n");
		 for (i = 0; i<10; i++)           //双重循环输出迷宫
		{
		   for (j = 0; j<10; j++){
			   printf("%d ",maze->arr[i][j]);
		   }
		   printf("\n");
		}
    }
    else                                         //否则,没有通路
       printf("\n---------从入口到出口没有通路!-----\n");
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CaoPengCheng&

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值