本程序主要是采用队列的思想,将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)*sizeof(cun));
if(!L.bottom)
{
printf("内存分配失败!!");
exit(-1);
}
L.top=L.bottom+L.stacksize;
L.stacksize+=STACK_ADD;
}
L.top->i=q;
(L.top++)->j=p;
return OK;
}
Status pop(Sqstack &L,int &k,int &l) // 弹出 将栈顶的元素带回
{
if(L.top == L.bottom)
return ERROR;
k=(--L.top)->i; // 这点自己犯的问题 不能--L.top->i 这样就表示值减1 没意义,出错
l=L.top->j;
return OK;
}
Status emptystack(Sqstack L) // 判断栈为空
{
if(L.top == L.bottom)
return OK;
else
return ERROR;
}
void init(ji &L) // 初始化数据
{
printf("输入迷宫的大小:");
scanf("%d",&L.size);
L.size+=2;
L.elem=(mi)malloc(L.size*L.size*sizeof(migong));
if(!L.elem)
{
printf("分配内存失败!!");
exit(1);
}
printf("初始化成功!\n");
}
void print(ji L)
{
int m;
printf("打印输出!!\n\n");
m=L.size*L.size;
for(int i=0;i<m;i++)
{
if((i+1)%L.size != 1 && (i+1)%L.size != 0 && i>L.size-1 && i<m-L.size+1)
printf("%-4d",(L.elem+i)->road);
if((i+1)%L.size == 0 && i>L.size && i<m-L.size-1)
printf("\n\n");
}
printf("\n");
}
void shuru(ji &L)
{
int m=L.size*L.size;
for(int i=0;i<m;i++)
{
(L.elem+i)->direct=1;
(L.elem+i)->foot=0;
if((i+1)%L.size == 1 || (i+1)%L.size == 0 || i<=L.size-1 || i>=m-L.size+1)
(L.elem+i)->road=0;
else
(L.elem+i)->road=1; // 表示通路,初始化全部为
}
print(L);
printf("设置墙:\n");
printf("输入迷宫的行和列:");
int k,j;
char ch='y';
while('y' == ch || 'Y' == ch)
{
scanf("%d%d",&k,&j);
(L.elem+k*L.size+j)->road=0;
printf("是否继续(Y/y):");
getchar();
ch=getchar();
}
print(L);
}
void shituan(ji &L,int z,int x,int &m,int &n) // 判断方向
{
switch((L.elem+L.size*z+x)->direct) // 线性表中位置的转换
{
case 1:m=z,n=x+1;break;
case 2:m=z+1,n=x;break;
case 3:m=z,n=x-1;break;
case 4:m=z-1,n=x;break;
}
}
void chuli(ji &L,dui &T)
{
int m=1,n=1,i=1,j=1;
int q=0,p,t;
sq*w=T.front-1,*r;
printf("开始执行!!");
enqueue(T,1,1,1,1);
(L.elem+L.size+1)->foot=1;
t=1;
while(m != L.size-2 || n != L.size-2) // 循环条件是不是最后一个位置
{
// printf("进入循环!!");
// getchar();
r=w;
q=0;
for(p=1;p<=t;p++)
{
w++; // 查询入队元素的位置,不停的更新
i=w->i; // 取得已经入队元素的当前位置坐标
j=w->j;
while((L.elem+L.size*i+j)->direct <= 4) // 寻找4个方位 满足条件的位置
{
// printf("进入内部循环!!");
shituan(L,i,j,m,n);
if((L.elem+L.size*m+n)->road != 0 && (L.elem+L.size*m+n)->foot != 1) // 该位置不是墙也不是已经走过的路径
{
enqueue(T,m,n,i,j); // 压入位置
q++; // 入队元素个数记录