迷宫程序设计是第三个实验,思想是不停的试探,然后标记判断出路的过程
一般自己习惯把头文件全部放在一个文件里名为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
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;
再就是自己定义的实现函数了 我自己定义为migong.cpp
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,Sqstack &T)
{
int m,n,i=1,j=1;
printf("开始执行!!");
while(m!=L.size-2 || n!=L.size-2) // 循环条件是不是最后一个位置
{
shituan(L,i,j,m,n);
(L.elem+L.size*i+j)->direct++;
if((L.elem+L.size*m+n)->road != 0 && (L.elem+L.size*m+n)->foot != 1) // 该位置不是墙也不是已经走过的路径
{
push(T,i,j); // 压入位置
(L.elem+L.size*i+j)->foot=1; // 留下足迹
(L.elem+L.size*i+j)->road=2; // 将通路为置为2,表示路径
i=m;
j=n;
}
else if((L.elem+L.size*i+j)->direct == 5) // 4个方向都没有路了
{
if(emptystack(T)) // 当前栈里面是否有元素
{
printf("无通路!!!~~~~~~~~~~~~~\n试探路径为:\n");
print(L);
exit(1); // 退出程序
}
(L.elem+L.size*i+j)->road=-1; // 将走过的路,不通的留下记号,标记为-1
(L.elem+L.size*i+j)->foot=1; // 留下足迹
pop(T,i,j); // 不通的路径出栈
}
}
if(m == L.size-2 && n == L.size-2) // 将最后一个位置为留下2
(L.elem+L.size*m+n)->road=2;
print(L);
}
最后就是自己定义的主函数了名为main_5.cpp
#include"t11.h"
#include"m1.h"
#include"migong.cpp"
void main()
{
ji S;
Sqstack T;
initstack(T);
init(S);
shuru(S);
chuli(S,T);
}
这是自己学数据结构的第三个实验,小弟深知编程不但可以考验一个人的逻辑思维能力,更是对自己毅力的体现,希望自己以后能够变得更出色!留住最真的于2012.03.10 20:18写~~~~~~~~~~~~~~~~~~