数据结构学习记录-迷宫设计(最短路径)

这篇博客记录了使用数据结构和队列思想解决迷宫问题的过程,通过定义迷宫结构体和相关操作,实现了从入口到出口的最短路径搜索。主要涉及栈和队列的数据结构,包括初始化、压入、弹出等操作,并提供了主函数调用示例。作者强调了编程过程中不断调试、思考的重要性,以及对个人成长的价值。
摘要由CSDN通过智能技术生成

本程序主要是采用队列的思想,将4个方向的通路入队,然后依次先去,直到入口结束!!~~~~~~~~~~~~~~

 

定义头文件t11.h

 

#include"stdio.h"
#include"string.h"
#include"ctype.h"
#include"malloc.h"
#include"stdlib.h"  //atoi(),exit();
#include"io.h"      //eof()
#include"math.h"


#define  TRUE  1
#define  FALSE  0
#define  OK   1
#define  ERROR 0

typedef int Status;
typedef int Boolean;

 

定义队列的数据结构包含在m1.h头文件里

 

#define INIT_STACK_SIZE 40
#define STACK_ADD 10
#define INIT_QUEUE 100
#define INIT_ADD 40
typedef struct
{
 int road;
 int foot;
 int direct;
}migong,*mi;

typedef struct
{
 mi elem;      //  定义能够接受迷宫结构体指针的变量
 int size;     //  记录迷宫初始化大小
}ji,*hui;

typedef struct
{
 int i;           // 行
 int j;           // 列
}cun;

typedef struct     //  把位子放入栈中 线性表操作
{
 cun *top;
 cun *bottom;
 int stacksize;
} Sqstack;

typedef struct sq
{
 int i,j;               //存放前面的下标位置
 int i1,j1;           //  存放当前位置  
}sq;


typedef struct
{
 sq*front;
 sq*rear;
 int size;
}dui;

 

定义的实现函数代码(一些实现函数)定义在文件migong.cpp里

 

void initdui(dui &L)                   //  初始化队列   
{
    L.front=(sq*)malloc(INIT_QUEUE*sizeof(sq));
 L.rear=L.front;
 if(!L.front)
 {
  printf("内存分配失败!!");
  exit(1);
 }
 L.size=INIT_QUEUE;
 printf("初始化成功!! \n");
}

void enqueue(dui &L,int i,int j,int i1,int j1)            //   此队列针对迷宫先全部进完后再出,而设的顺序队列
{
 if(L.rear-L.front>L.size)  // 队列已满,追加空间
    {
  L.front=(sq*)realloc(L.front,(L.size+INIT_ADD)*sizeof(sq));
 if(!L.front)
 {
  printf("追加空间失败!!");
  exit(1);
 }
 }
 L.rear->i=i;
 L.rear->j=j;
 L.rear->i1=i1;
 (L.rear++)->j1=j1;


}


void initstack(Sqstack &L)            //  初始化栈操作
{
 L.bottom=(cun*)malloc(INIT_STACK_SIZE*sizeof(cun));
 if(!L.bottom)
 {
  printf("内存分配失败!!");
  exit(-1);
 }
 L.top=L.bottom;
    L.stacksize=INIT_STACK_SIZE;
}

Status push(Sqstack &L,int q,int p)          // 压入位置 q表示行,p表示列
{
 if(L.top-L.bottom >= L.stacksize)
 L.bottom=(cun*)realloc(L.bottom,(L.stacksize+STACK_ADD)*si

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值