迷宫(C++)

#include<iostream>
#include<ctime>
#include<stack>
#include<windows.h>
using namespace std;

int NUM1= 20;
int NUM2= 20;
const int East  = 0;
const int South = 1;
const int West  = 2;
const int North = 3;
const int pause = 50; //间隔时间
static int count=1;
class Maze
{
public:
    Maze(int h, int w);
    ~Maze();
    void display();
    bool hasCrossOver();
 void WalkMaze();
private:
    int row;
    int col;
    char **p;
    void Init();
};

Maze::Maze(int h,int w):row(h),col(w)
{
    p=new char *[row];
    for(int i=0;i<row;++i)
        p[i]=new char[col];
    Init();
}

Maze::~Maze()
{
    for(int i=0;i<row;i++)
        delete []p[i];
    delete []p;
}


void Maze::display()
{
    for(int i=0;i<row;++i)
    {
        for(int j=0;j<col;++j)
            cout<<p[i][j];
        cout<<endl;
    }
}

void Maze::Init() //随机生成迷宫
{
    srand(time(0)); 
    for(int i=0;i<row;++i)
    {
        for(int j=0;j<col;++j)
        {
            if((i==0)||(j==0)||(i==row-1)||(j==col-1))//加上墙壁
            {
                p[i][j]='*';
                continue;
            }
            if(rand()%100<80)
                p[i][j]='0';
            else
                p[i][j]='1';
        }
    }
    p[1][1]='m';
}
void Maze::WalkMaze()
{
 int num=1;
 for(int i=0;i<NUM1;i++)
 {
  for(int j=0;j<NUM2;j++)
  {
   if(p[i][j]=='m')
   {
    cout<<"第"<<num<<"步走过的点是: ("<<i<<","<<j<<")"<<endl;
    num++;
   }
  }
  cout<<endl;
 }
}
bool Maze::hasCrossOver()
{
    int   flag, i=1,j=1;
    stack<int> s;
    while(true)
    {
  //判断是否可以向东(右)
        if((j+1<col-1)&&(p[i][j+1]=='0'))
        {
            p[i][++j] = 'm';
            s.push(East);
        }
        //判断是否可以向南(下)
        else if((i+1 < row-1) && (p[i+1][j] == '0'))
   {
    p[++i][j] = 'm';
    s.push(South);
   }
   //判断是否可以向西(左)
   else if((j-1 > 0) && (p[i][j-1] == '0'))
    {
     p[i][--j] = 'm';
     s.push(West);
    }
    //判断是否可以向北(上)
    else if((i-1 > 0) && (p[i-1][j] == '0'))
     {
      p[--i][j] = 'm';
      s.push(North);
     }
     else
     {
      if(s.empty())
       return false;
      flag = s.top();
      s.pop();
      switch (flag)
      {
       case East:
        --j; break;
       case South:
        --i; break;
       case West:
        ++j; break;
       case North:
        ++i; break;
      }
     }


        if(i==row-2 && j==col-2)
        {
            int m, n;
            stack<int> newStack;
            while(!s.empty())
            {
                newStack.push(s.top());
                s.pop();
            }
   
            for(m=0;m<row;++m)
            {
                for(n=0;n<col;++n)
    {
                    if(p[m][n] == 'm')
                        p[m][n] = '0';
    }
   
   }
  
   m = 1;
   n = 1;
   p[m][n]='m';
   while(!newStack.empty())
   {
    flag=newStack.top();
    switch(flag)
    {
                    case East:
                        ++n; break;
                    case South:
                        ++m; break;
                    case West:
                        --n; break;
                    case North:
                        --m; break;
    }
    p[m][n] = 'm';
    Sleep(pause);  //windows.h
    ++count;
    system("cls");
    display();
    newStack.pop();
   }
   return true;
  }//end if;
  
 }//end while
}

void main()
{
    Maze gMaze(NUM1,NUM2);
    gMaze.display();
    system("pause");//等待
    if (gMaze.hasCrossOver())
 {
  cout<<"总共走了"<<count<<"步"<<endl;
        cout << "经过的下标节点是:"<< endl;
  gMaze.WalkMaze();
 }
    else
        cout << "/n无通路" << endl;
 
}

你可能感兴趣的:(迷宫(C++))