迷宫

来源:互联网 发布:双色球凤尾点位算法 编辑:程序博客网 时间:2024/04/28 20:03
写了个小程序 关于迷宫问题 

#include <iostream>
#include <string>
#include "maze.h"
using namespace std;
/*class Array          //定义描述迷宫中当前位置的结构类型
{
public:
 int horiz;         //代表当前位置的行坐标
 int vert;          //代表当前位置的列坐标
 int dir;           //前进的方向
};
class LinkNode           //链表结点
{
 friend class Stack;
public:
 Array data;
 LinkNode *next;
};
class Stack
{
private:
 LinkNode *top;          //指向第一个结点的栈顶指针
public:
 Stack();                //构造函数,置空栈
 ~Stack();               //析构函数
 void push(Array element);   //把元素data压入栈中
 Array pop();                 //使栈顶元素出栈
 Array getPop();              //取出栈顶元素
 bool empty();                //判断栈是否为空,如果为空则返回1,否则返回0
 void clear();                //把栈清空
};*/
/*Stack::Stack()          //构造函数,置空栈
{
 top=NULL;
}
Stack::~Stack()         //析构函数
{
}
void Stack::push(Array element)          //把元素x压入栈中
{
 LinkNode *p;
 p=new LinkNode;
 p->data=element;
 p->next=top;
 top=p;
}
Array Stack::pop()                 //使栈顶元素出栈
{
 Array temp;
 LinkNode *p;
 p=top;
 top=top->next;
 temp=p->data;
 delete p;
 return temp;
}
Array Stack::getPop()               //取出栈顶元素
{
 return top->data;
}
void Stack::clear()                    //把栈清空
{
 top=NULL;
}
bool Stack::empty()        //判断栈是否为空,如果为空则返回1,否则返回0
{
 if(top==NULL)
  return 1;
 else return 0;
}*/
int move[4][2]={{0,1},{1,0},{0,-1},{-1,0}};   //定义当前位置移动的4个方向
bool mazePathDepth (int length,int width);
bool mazePathBreadth(int length,int width);
     //寻找迷宫maze中从(1,1)到(length, width)的路径
     //到则返回true,否则返回false
void printPath(Stack p);        //输出迷宫的路径
                               
int myMaze[8][11] =
{
 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
 {1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1},
 {1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1},
 {1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1},
 {1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1},
 {1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1},
 {1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1},
 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} 
};
void display() {
 for(int i=0; i<8; i++) {
  for(int j=0; j<11; j++) {
   if(myMaze[i][j]==-1)         //恢复探索过位置,即把-1恢复为0
   {myMaze[i][j]=0;}
   cout << myMaze[i][j];
  }
  cout << endl;
 }
}
int main()
{
 int length=8, width=11;       //定义迷宫的长和宽
 string s, depth, breadth;
 cout<<"排序前的迷宫"<<endl;
 display();
 cout <<"请输入搜索模式:深度或者广度:depth or breadth"<<endl;
 if(cin>>s){
 if("depth" == s)
 {
  if(mazePathDepth(length, width))
  { cout<<"迷宫路径探索成功!  " << "  排序后的迷宫深度搜索"<<endl;}
  else cout<<"路径不存在!\n";
 }
 else if("breadth" == s)
 {
  if(mazePathBreadth(length, width))
  { cout<<"迷宫路径探索成功!  " << "  排序后的迷宫广度搜索"<<endl;}
  else cout<<"路径不存在!\n";
 }
 else
  cout <<"error"<<endl;
 }
 return 0;
}


bool mazePathDepth(int length,int width)//寻找迷宫maze中从(0,0)到(outLen, outWid)的路径
           //到则返回true,否则返回false
{
 Stack q,p;       //定义栈p、q,分别存探索迷宫的过程和存储路径
 Array Temp1,Temp2;  
 int outLen=6, outWid=9;
 int x,y,loop;
 Temp1.horiz=1;
 Temp1.vert=1;
 q.push(Temp1);           //将入口位置入栈
 p.push(Temp1);
 myMaze[1][1]=-1;          //标志入口位置已到达过
 while(!q.empty())      //栈q非空,则反复探索
 {
  Temp2=q.getPop();      //获取栈顶元素
  if(!(p.getPop().horiz==q.getPop().horiz&&p.getPop().vert==q.getPop().vert))
   p.push(Temp2);        
     //如果有新位置入栈,则把上一个探索的位置存入栈p
  for(loop=0;loop<4;loop++)   //探索当前位置的4个相邻位置
  {
   x=Temp2.horiz+move[loop][0];     //计算出新位置x位置值
   y=Temp2.vert+move[loop][1];      //计算出新位置y位置值
   if(myMaze[x][y]==0)         //判断新位置是否可达
   {
    Temp1.horiz=x;
    Temp1.vert=y;
    myMaze[x][y]=-1;          //标志新位置已到达过
    q.push(Temp1);         //新位置入栈
   }
   if((x==(outLen))&&(y==(outWid)))    //成功到达出口
   {
    Temp1.horiz=outLen;
    Temp1.vert=outWid;
    Temp1.dir=outWid;
    p.push(Temp1);       //把最后一个位置入栈
    printPath(p);        //输出路径
    return 1;            //表示成功找到路径
   }
  }
  if(p.getPop().horiz==q.getPop().horiz&&p.getPop().vert==q.getPop().vert)
        //如果没有新位置入栈,则返回到上一个位置
  {
   p.pop();
   q.pop();
  }
 }
 return 0;           //表示查找失败,即迷宫无路经
}

bool mazePathBreadth(int length,int width)//寻找迷宫maze中从(0,0)到(outLen, outWid)的路径     广度搜索
           //到则返回true,否则返回false
{
 Stack q,p;       //定义栈p、q,分别存探索迷宫的过程和存储路径
 Array Temp1,Temp2;  
 int outLen=6, outWid=9;
 int x,y,loop;
 Temp1.horiz=1;
 Temp1.vert=1;
 q.push(Temp1);           //将入口位置入栈
 p.push(Temp1);
 myMaze[1][1]=-1;          //标志入口位置已到达过
 while(!q.empty())      //栈q非空,则反复探索
 {
  Temp2=q.getPop();      //获取栈顶元素
  if(!(p.getPop().horiz==q.getPop().horiz&&p.getPop().vert==q.getPop().vert))
   p.push(Temp2);        
     //如果有新位置入栈,则把上一个探索的位置存入栈p
  for(loop=0;loop<4;loop++)   //探索当前位置的4个相邻位置
  {
   x=Temp2.horiz+move[loop][1];     //计算出新位置x位置值
   y=Temp2.vert+move[loop][0];      //计算出新位置y位置值
   if(myMaze[x][y]==0)         //判断新位置是否可达
   {
    Temp1.horiz=x;
    Temp1.vert=y;
    myMaze[x][y]=-1;          //标志新位置已到达过
    q.push(Temp1);         //新位置入栈
   }
   if((x==(outLen))&&(y==(outWid)))    //成功到达出口
   {
    Temp1.horiz=outLen;
    Temp1.vert=outWid;
    Temp1.dir=outWid;
    p.push(Temp1);       //把最后一个位置入栈
    printPath(p);        //输出路径
    return 1;            //表示成功找到路径
   }
  }
  if(p.getPop().horiz==q.getPop().horiz&&p.getPop().vert==q.getPop().vert)
        //如果没有新位置入栈,则返回到上一个位置
  {
   p.pop();
   q.pop();
  }
 }
 return 0;           //表示查找失败,即迷宫无路经
}

void printPath(Stack p)        //输出路径
{
 cout<<"迷宫的路径为\n";
 Array data;
 while(!p.empty())         //栈非空,继续输出
 {
  data=p.pop();
  myMaze[data.horiz][data.vert]=2;
 }
 display();
}