用栈求解迷宫问题 ~~

#include <iostream>
#define INI_SIZE 1000
#define INCREATE_SIZE 7
using namespace std;


//typedef int SElemType;
//typedef char SElemType;


//*************************Maze
typedef struct 
{
int x;
int y;
}PosType;//the poistion of every point




typedef struct
{
int ord;//Channel block in the path of the "serial number"
PosType seat;
int di;//the next direction 0-3 means "east west south north"
}SElemTypeMaze;




typedef SElemTypeMaze SElemType;
//*************************Maze


struct SqStack
{
int stacksize;
SElemType *top;//point top point to the hightest's element' higher position;
SElemType *base;
};
//**************************************************************************************
int ini(SqStack &S)
{
if(!(S.base = (SElemType *)malloc(INI_SIZE*sizeof(SElemType))))
return -1;
S.top = S.base;
S.stacksize  =  INI_SIZE;
return 0;
}
void print_test()
{
cout<<"asdfasdf"<<endl;
}
int Destroy(SqStack &S)
{
free(S.base);
S.base = S.top = 0;
S.stacksize = 0;
return 0;
}
int ClearStack(SqStack &S)
{
S.top = S.base;//THIS FUNCTION IS used to get the two point equal and but can not take them to 0 as the point value is from the malloc the point is safe and can be used
return 0;
}
int IsEmpty(SqStack &S)
{
if(S.top == S.base)
return true;
else
return false;
}
int GetStackLength(SqStack S)
{
return S.stacksize;
}
int GetTop(SqStack S,SElemType &e)
{
if(S.top>S.base)
{
e = *(S.top-1);
   return 0;
}
else
return -1;
}
int push(SqStack & S,SElemType e)
{
if((S.top - S.base)>=S.stacksize)//when new space is needed
{
S.base = (SElemType *)realloc(S.base,(S.stacksize+INCREATE_SIZE)*sizeof(SElemType));
if(!S.base)
return -1;
S.top = S.base + S.stacksize;
}
    *(S.top++)=e;
return 0;
}
int Pop(SqStack & S,SElemType & e)
{
if(S.base == S.top)
return -1;
else
e = (*(--S.top));
}
int StackTrerse(SqStack & S,void (*visit)(SElemType e))
{
SElemType * temp;
temp=S.base;
while(temp!=S.top)
{
visit(*(temp++));
}
cout<<endl;
return 0;
}
/*int Conver(int i,int index)//将 i 转换成 index进制 并打印出来
{

    SqStack S;
ini(S);
SElemType get;
do
{
push(S,i%index);
}
while(i=i/index);
while(S.top != S.base)
{
Pop(S,get);
//putchar(get+'0');
//cout<<get;
//putchar((get<=10?(get+'0'):((get-10)+'A')));
if(get>=10)
putchar(get-10+'A');
else
printf("%d",get);
//cout<<endl;
}
return 0;
}*/
/*void Check()
{
SqStack S;
SElemType ch[80],*p,e;
cout<<"please insert parameters"<<endl;
gets(ch);
p=ch;
ini(S);
while(*p)//the '/0' will be added automatically at the end of the char[]
    {
switch(*p)
{
case '(':
case '[':
push(S,*p++);
break;
case ')':
case ']':
if(!IsEmpty(S))//when the stack is not empty there is some operator in the stack
{
Pop(S,e);
if(*p==')'&&e != '('|| *p==']'&&e != '[')//when the opreator is not matched
{
cout<<"can not match the operator"<<endl;
return ;
}//
else
{//配对的情况下 就这样经行 在这种情况下 就 POp在先了 移动p指针
++p;
break;
}
}
else//当一个站里面已经空了 并且
{
printf("do not have left operator");
//break;
return ;
}
default:
p++;
}
}
if(IsEmpty(S))//if the stack is empty when the string is over
{
cout<<"括号匹配"<<endl;
}
else//deal with this chance : (((((())
{
cout<<"be 缺少右括号"<<endl;
}
}//处理最容易处理的情况 把 不好处理的情况让他们自己去处理 放在else里进行处理*/
FILE * fp;
/*void  copy(SElemType C)
{
fputc(C,fp);
//return 0;
}*/
/*void LineEdit()
{
SqStack S;
char e;
ini(S);
cout<<"insert the code you want to input crtl+z is the ending"<<endl;
char ch;
ch = getchar();//初始化启动
while(ch!=EOF)
{
while(ch!=EOF&&ch!='\n')//inner loop get the char which is input
{
switch(ch)
{
  case '#': Pop(S,e);break;
  case '@': ClearStack(S);break;
  default : push(S,ch);
}
            ch = getchar();
}
StackTrerse(S,copy);//按照顺序弹出放出来 write them into the file which fp point to
fputc('\n',fp);
ClearStack(S);
if(ch!=EOF) ch = getchar();
}
Destroy(S);
}*/
//迷宫求解
typedef int Maze[25][25];//maze is the first add of 2-dimesions arrary
Maze m;//global instance;
//point struct 
int curstep = 1;//curent ppoistion
int Pass(PosType b)//if the block on the destination way we mark it with the curstep to 
{
if(m[b.x][b.y] == 1)
return 1;//if(m[1][3] == 1)then it can be reached 
else return 0;
}
void FootPrint(PosType a)
{
m[a.x][a.y] = curstep;
}
void NextPos(PosType *c,int di)/**/
{
PosType direct[4] = {{1,0},{0,1},{0,-1},{-1,0}};
c->x +=direct[di].x;/**/
c->y +=direct[di].y;
}
void MarkPrint(PosType b)// if the cur block is not on the right way then we mark this block as -1 
{
m[b.x][b.y] = -1;
}
int MazePath(PosType start,PosType end)
{
//1- initial the data structure
SqStack S;//stack
    ini(S);//stack;
PosType curpos = start;//receive the start
    SElemType e;
do//when the stack is not empty
{
if(Pass(curpos))//if the point can be reached
{
FootPrint(curpos);//put the point into the reaching road
//fill the info of the stack element
e.ord = curstep;
e.di = 0;
e.seat = curpos;
push(S,e);
++curstep;
if(curpos.x == end.x && curpos.y == end.y)
{
return 1;//if the point is the end point we return and end the function
}
//turn to the cur_point's west point 
NextPos(&curpos,e.di);//fill the new info into the curpos

}
else//in this condition the pint can not be reached
{
             Pop(S,e);
--curstep;
while(e.di == 3&&!IsEmpty(S))
{
Pop(S,e);
MarkPrint(e.seat);
--curstep;
}
if(e.di < 3 &&!IsEmpty(S))// in this condition the block has some directions has not been checked then we want to check other directions
{
++e.di;
push(S,e);// // as the e is popOut but the info is needed so we put it in again and the di is updated 
++curstep; /* 足迹加1 */
NextPos(&e.seat,e.di);//change the destination point to the next point Notice :: the e's info is changed 
curpos = e.seat;
}
}
}while(!IsEmpty(S));
return 0;
}
/*Test Function*/


static int x,y; /* 迷宫的行数,列数 */


 


void Data()
{
int i(0);
int j;
FILE *fp = fopen("awaraay.txt","r+");
for(i = 0;i<21;++i)
{
for(j = 0;j<21;++j)
{
fscanf(fp,"%d",&m[i][j]);
//fread(&a[i][j],sizeof(int),1,fp);
}
}
fclose(fp);
}
void Print()
{
int i;
int j;
for(i = 0;i<21;++i)
{
for(j = 0;j<21;++j)
{
switch(m[i][j])
{
case 0 :cout<<"■";break;
case 1 :cout<<"□";break;
default : cout<<m[i][j]<<" ";break;
}
       // cout<<m[i][j]<<"  ";
}
cout<<endl;
}
}


测试函数 :

#include "header.h"
 void main()
 {
  PosType begin,end;
  end.x = 18;
  end.y = 19;
  begin.x = 1;
  begin.y = 1;
   Data();
   if(MazePath(begin,end)) 
   {
     printf("此迷宫从入口到出口的一条路径如下:\n");
     Print(); 
   }
   else
     printf("此迷宫没有从入口到出口的路径\n");
 }




说明 :

本函数还有 一个 txt 迷宫文件 awaraay.txt::


0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 0 0 0 1 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 0
0 0 0 0 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0
0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 0
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值