//这个程序是将一个迷宫的程序打印出来,具体的操作我会在下面的程序中具体的
//给出来,其中分为两一个小的程序,一个是这个文件,另一个是一个以head.cpp文件
//为main函数的文件,好了,程序开始了
#include "iostream.h"
#include "malloc.h"
#define ERROR 0
#define OK 1
#define NULL 0
typedef int ElemType ;
typedef int status;
#define A 4 //定义一个数组的长度(6*6的数组)
int array[A][A];
typedef struct LNode{
int length;
int width;
int direct;//这个是为了判断方向的。其中0代表向上,1代表向右,2代表向下,3代表向左
struct LNode *next;
}map,*mapth;
mapth coutmap=0;//全局变量
status push(mapth &L,mapth p)
{//将一个元素推带堆栈中
if(L==NULL)
{L=(mapth)malloc(sizeof(map));
L->length=0;}
mapth q;
q=(mapth)malloc(sizeof(map));
q->direct=p->direct;
q->length=p->length;
q->next=p->next;
q->width=p->width;
q->next=L->next;
L->next=q;
L->length++;
return OK;
}
status pull(mapth &L,mapth &q)
{//这个函数的功能是将一个线性表拉出来一个元素,并且用e返回,在开始的时候还可以对文件
//判断一下
if((L==0)||(L->next==0))
return ERROR;
else if(L->length==1)
{
q=L->next;
L->next=0;
L->length--;
return OK;
}
else
{
q=L->next;
L->next=q->next;L->length--;
return OK;
}
}
status empty(int length,int width)
{//判断是否越界,0代表越界,1代表没有
if(length>=A||width>=A||length<0||width<0)
return ERROR;
else if((array[length][width]!=0)) return ERROR;
else return OK;
}
status coutnode(mapth &L,mapth &first)
{//在执行的时候判断一下,成功就返回1,不成功就返回0
if((first->length==(A-1))&&(first->width==(A-1)))
{
while(L&&(L->next!=0))
{
pull(L,first);push(coutmap,first);
}
return OK;
}
else return ERROR;
}
status del(mapth &L,mapth &first)
{
if(!L||L->next==0) return ERROR;
first->direct++;
if(first->direct==4) first->direct=0;
if((L->next->direct==1&&first->direct==3)||(L->next->direct==3&&first->direct==1)||(L->next->direct==0&&first->direct==2)||(L->next->direct==2&&first->direct==0))
first->direct++;
if(first->direct==4) first->direct=0;
if(L->next->direct==first->direct)
{
pull(L,first);
first->direct++;
if(first->direct==4) first->direct=0;
if(!L||L->next==0)
return ERROR;
}
return OK;
}
status migong(mapth &coutmap)
{
int i,j;
cout<<"please input "<<A*A<<" number !"<<endl;
for(i=0;i<A;i++)
for(j=0;j<A;j++)
cin>>array[i][j];
cout<<"The computer is begin to find the end! please waite for a few minute!"<<endl;
mapth L=0;
L=(mapth)malloc(sizeof(map));
L->next=0;
L->length=0;
L->width=0;
mapth first;
first=(mapth)malloc(sizeof(map));
mapth second;
second=(mapth)malloc(sizeof(map));
first->length=0;
first->width=0;
first->direct=0;//方向为向上,以下的东西都是循环
first->next=0;
second=first;
push(L,first);
first=(mapth)malloc(sizeof(map));
first->direct=second->direct;
first->length=second->direct;
first->next=second->next;
first->width=second->width;
free(second);
while((first->length<(A-1))||(first->width<(A-1)))//当L不是空栈的时候,就一直循环
{
if(first->direct==0)
{first->width--;
if(empty(first->length,first->width)) //当没有越界的时候执行
{
push(L,first);
if(coutnode(L,first)) return 0;
}
else
{first->width++;
if(!del(L,first)) return ERROR;
}
}
if(first->direct==1)
{
first->length++;
if(empty(first->length,first->width)) //当没有越界的时候执行
{
push(L,first);
if(coutnode(L,first)) return 0;
}
else
{ first->length--;
if(!del(L,first)) return ERROR;
}
}
if(first->direct==2)
{
first->width++;
if(empty(first->length,first->width)) //当没有越界的时候执行
{
push(L,first);
if(coutnode(L,first)) return 0;
}
else
{ first->width--;
if(!del(L,first)) return ERROR;
}
}
if(first->direct==3)
{
first->length--;
if(empty(first->length,first->width)) //当没有越界的时候执行
{
push(L,first);
if(coutnode(L,first)) return 0;
}
else
{//当方向是向右的时候就有点不对了,应该做相应的修改
first->length++;
if(!del(L,first)) return ERROR;
}
}
}
while(L&&(L->next!=0))
{
pull(L,first);push(coutmap,first);
}
return OK;
}
int main()
{
migong(coutmap);
mapth first=0;
while(coutmap&&(coutmap->next!=0))
{
pull(coutmap,first);
cout<<"A["<<first->length<<"]["<<first->width<<"]"<<endl;
}
return 0;
}