c++ 迷宫求解

typedef struct
{
int point[100][100];
int i,j;
}Map;
void InitMap(Map &m);
void PrintMap(Map &m);

typedef struct
{
int r,c;//迷宫中r行c列的位置
}PosType;

#include"stdio.h"
#include"PosType.h"
#include<iostream>
using namespace std;
typedef struct
{
int m,n;
char arr[11][11];
}MazeType;
void InitMaze(MazeType &maze,int a[][9],int row,int col);//按照用户输入的row行和col列的二维数组(元素值为1或0)设置迷宫maze的初值,包括加上边缘一圈的值
bool MazePath(MazeType &maze,PosType start,PosType end);//求解迷宫maze中,从入口start到出口end的一条路径,若存在,则返回TURE;否则返回FALSE
void PrintMaze(MazeType maze);//将迷宫以字符型方阵的形式输出到标准输出文件上
int Pass(MazeType maze,PosType pos);//判断当前位置是否可以通过,即当前位置是否未曾走过
void FootPrint(MazeType &maze,PosType pos);//标记当前位置
PosType NextPos(PosType pos,int di);//根据所给方向di返回下一位置的坐标
void MarkPrint(MazeType &e,PosType pos);//留下不能通过的标记

#include"MazeType.h"
typedef struct
{
int ord;
PosType seat;
int di;
}SElemType;
typedef struct NodeType
{
SElemType data;
NodeType *next;
}NodeType,*LinkType;
typedef struct
{
LinkType top;
int size;
}Stack;
void InitSElemType(SElemType &e);//初始化SElemType
void InitStack(Stack &s);//初始化栈s
int MakeNode(LinkType &p,SElemType e);//为结点分配空间,若分配成功返回1,否则返回0
int StackEmpty(Stack s);//判断栈s是否为空,若为空返回1,否则返回0
int Push(Stack &s,SElemType e);//若分配空间成功,则在s的栈顶插入新的元素e,并返回1,否则返回0
int Pop(Stack &s,SElemType &e);//若栈不为空,则删除s的栈顶元素并以e带回其值,返回1;否则返回0
void Set(SElemType &e,int ord,PosType pos,int di);//设置e中数据
void PrintStack(Stack s);//打印栈s中数据

#include"Map.h"
#define MAPSIZE_X 10
#define MAPSIZE_Y 10
void InitMap(Map &m)
{
m.point=[MAPSIZE_X][MAPSIZE_Y];
m.i=0;
m.j=0;
}

#include"Stack.h"
int Pass(MazeType maze,PosType pos)
{
if(maze.arr[pos.c][pos.r]==' ')
   return 1;
else return 0;
}
void FootPrint(MazeType &maze,PosType pos)
{
maze.arr[pos.c][pos.r]='*';
}
PosType NextPos(PosType pos,int di)//根据所给方向di返回下一位置的坐标
{
PosType newpos;
newpos.c=0;
newpos.r=0;
if(di==1)
{
   newpos.c=pos.c;
   newpos.r=pos.r+1;
}
if(di==2)
{
   newpos.c=pos.c+1;
   newpos.r=pos.r;
}
if(di==3)
{
   newpos.c=pos.c;
   newpos.r=pos.r-1;
}
if(di==4)
{
   newpos.c=pos.c-1;
   newpos.r=pos.r;
}
return newpos;
}
void MarkPrint(MazeType &e,PosType pos)//留下不能通过的标记
{
e.arr[pos.c][pos.r]='@';
}
void InitMaze(MazeType &maze,int a[][9],int row,int col)//按照用户输入的row行和col列的二维数组(元素值为1或0)设置迷宫maze的初值,包括加上边缘一圈的值
{
maze.m=row+2;
maze.n=col+2;
for(int i=0;i<maze.m;i++)
{
   for(int j=0;j<maze.n;j++)
   {
    maze.arr[i][j]=' ';
   }
}
for(int i=1;i<=row;i++)
{
   for(int j=1;j<=col;j++)
   {
    if(a[i-1][j-1]==1)
     maze.arr[i][j]='#';
    else
       maze.arr[i][j]=' ';

   }
}
for(int r=0;r<maze.n;r++)
{
   maze.arr[0][r]='#';
   maze.arr[row+1][r]='#';
}
for(int c=0;c<maze.m;c++)
{
   maze.arr[c][0]='#';
   maze.arr[c][col+1]='#';
}
}
bool MazePath(MazeType &maze,PosType start,PosType end)//求解迷宫maze中,从入口start到出口end的一条路径,若存在,则返回TURE;否则返回FALSE
{
Stack s;
InitStack(s);
PosType curpos=start;
    SElemType e;
int curstep=1;
do
{
   if(Pass(maze,curpos))//当前位置可以通过,即是未曾走到过的通道块
   {
    FootPrint(maze,curpos);//留下足迹
      Set(e,curstep,curpos,1);
    Push(s,e);
    if(curpos.c==end.c&&curpos.r==end.r)
    {
     PrintStack(s);
     return 1;
    }
    curpos=NextPos(curpos,1);//下一位置是当前位置的东邻
    curstep++;//探索下一步
   }
   else//当前位置不能通过
   {
    if(!StackEmpty(s))
    {
     Pop(s,e);
     while(e.di==4&&!StackEmpty(s))
     {
      MarkPrint(maze,e.seat);//留下不能通过的标记,并退回一步
      Pop(s,e);
      curstep--;
     }//while
     if(e.di<4)
     {
      e.di++;//换下一个方向探索
      Push(s,e);
      curpos=NextPos(e.seat,e.di);//设置当前位置是该新位置上的相邻块
     }//if
    }//if
   }//else
}while(!StackEmpty(s));
return 0;
}

void PrintMaze(MazeType maze)//将迷宫以字符型方阵的形式输出到标准输出文件上
{

printf(" ");
for(int i=0;i<maze.n;i++) printf(" %d",i);
printf("/n");
for(int j=0;j<maze.m;j++)
{
   if(j<=9)
   printf(" %d",j);
   else
    printf("%d",j);
   for(int k=0;k<maze.n;k++)
   {
    printf(" %c",maze.arr[j][k]);
   }
        printf("/n");
}
}
int main(){
int a[9][9]={
           {0,0,1,0,0,0,1,0,1},
              {0,0,1,0,0,0,1,0,1},
              {0,0,0,0,1,1,0,0,1},
              {0,1,1,1,0,0,0,0,1},
                 {0,0,0,1,0,0,0,0,1},
              {0,1,0,0,0,1,0,0,1},
              {0,1,1,1,0,1,1,0,1},
              {1,1,0,0,0,0,0,0,1},
              {1,1,0,0,0,0,0,0,1}
             };
MazeType maze;
InitMaze(maze,a,8,8);
PrintMaze(maze);
PosType start,end;
start.c=1;
start.r=8;
end.c=7;
end.r=1;
if(MazePath(maze,start,end))
{

PrintMaze(maze);
}
else
   printf("没有通路");
system("PAUSE");
return 0;
}

#include"Stack.h"
#include"malloc.h"
#include"stdio.h"
void InitSElemType(SElemType &e)//初始化SElemType
{
e.di=1;
e.ord=0;
e.seat.c=0;
e.seat.r=0;
}
void InitStack(Stack &s)//初始化栈s
{
s.top=NULL;
s.size=0;
}
int MakeNode(LinkType &p,SElemType e)//为结点分配空间,若分配成功返回1,否则返回0
{
p=(LinkType)malloc(sizeof(NodeType));
if(!p)
   return 0;
else
{
   p->data=e;
   p->next=NULL;
   return 1;
}
}
int StackEmpty(Stack s)//判断栈s是否为空,若为空返回1,否则返回0
{
if(s.top==NULL)
   return 1;
else
   return 0;
}
int Push(Stack &s,SElemType e)//若分配空间成功,则在s的栈顶插入新的元素e,并返回1,否则返回0
{
LinkType p;
if(MakeNode(p,e))
{
   p->next=s.top;
   s.top=p;
   s.size++;
   return 1;
}
else
   return 0;
}
int Pop(Stack &s,SElemType &e)//若栈不为空,则删除s的栈顶元素并以e带回其值,返回1;否则返回0
{
if(StackEmpty(s)) return 0;
else
{
   LinkType p=s.top;
   s.top=s.top->next;
   e=p->data;
   s.size--;
   return 1;
}
}
void Set(SElemType &e,int ord,PosType pos,int di)//设置e中数据
{
e.ord=ord;
e.seat=pos;
e.di=di;
}
void PrintStack(Stack s)//逆序打印s中元素
{
Stack t;
InitStack(t);
SElemType e;
InitSElemType(e);
LinkType p=s.top;
    while(p->next!=NULL)
{
   Pop(s,e);
   Push(t,e);
   p=p->next;
}
Pop(s,e);
Push(t,e);
p=t.top;
while(p->next!=NULL)
{
   if(p->data.ord<10)
    printf(" %d,(%d,%d),%d/n",p->data.ord,p->data.seat.c,p->data.seat.r,p->data.di);
   else
        printf("%d,(%d,%d),%d/n",p->data.ord,p->data.seat.c,p->data.seat.r,p->data.di);
   p=p->next;
}
printf("%d,(%d,%d),%d/n",p->data.ord,p->data.seat.c,p->data.seat.r,p->data.di);
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值