堆栈应用之迷宫寻径

程序包含两个头文件: StackT.h Position.h 和一个寻径方法

源代码:

#include<iostream.h>
#include "StackT.h"
#include "Position.h"

int **maze,m;//储存迷宫的数组及数组大小
Stack<Position> *path;

//读入迷宫并回显

void ReadIn()
{
 cout<<"Please enter an intger 'm' and a labyrinth with the size of m*m will be built."<<endl;
 cin>>m;
 maze=new int*[m+2];
 for(int n=0;n<m+2;n++)
  maze[n]=new int[m+2];

 cout<<"Please enter the labyrinth:"<<endl;

 for(int i=1;i<=m;i++)
 {
  for(int j=1;j<=m;j++)
     cin>>maze[i][j];
 }
   
 cout<<"The following is the labyrinth:"<<endl;

    for(int x=1;x<=m;x++)
 {
  for(int y=1;y<=m;y++)
   cout<<maze[x][y]<<" ";
  cout<<endl;
 }
}

//寻径方法

bool FindPath()
{
 path=new Stack<Position>(m*m-1);//建栈
    
 Position offset[4];

 offset[0].row=0;//向右移动:
    offset[0].col=1;//列数+1
 
 offset[1].row=1;//向下移动:
    offset[1].col=0;//行数+1
    
 offset[2].row=0;//向左移动:
    offset[2].col=-1;//列数-1
 
 offset[3].row=-1;//向上移动:
    offset[3].col=0;//r行数-1
 

//给迷宫加一圈围墙

 for(int z=0;z<=m+1;z++)
 {
  maze[0][z]=maze[m+1][z]=1;
  maze[z][0]=maze[z][m+1]=1;
 }

 Position here;
 here.row=1;
 here.col=1;
 maze[1][1]=1;
 int option=0;
 int LastOption=3;

 while(here.row!=m||here.col!=m)
 {

//寻径,试移动顺序:右、下、左、上
  int r,c;
  while(option<=LastOption){
   r=here.row+offset[option].row;
   c=here.col+offset[option].col;
   if(maze[r][c]==0)
    break;
   option++;
  }

//若从四个方向中找到一个路径,当前坐标进栈

  if(option<=LastOption)
  {
   path->Add(here);
      here.row=r;
      here.col=c;
      maze[r][c]=1;
      option=0;
  }

//否则上一个坐标出栈,并寻找下一个可行方向
 
     else
  {
   if(path->IsEmpty())
   {
        cout<<"Sorry! There is no way!"<<endl;
    return false;
   }
      Position next;
      path->Delete(next);
        if(next.row==here.row)
       option=2+next.col-here.col;
      else
       option=3+next.row-here.row;
      here=next;
  }
 }

//输出移动路径的坐标
 cout<<"Congratulations! There is the way!"<<endl;
 Position display;
 int temp=path->GetTop();
 path->SetTop();
 cout<<"The following is the coordinates of the way:"<<endl;
 for(int c=0;c<=temp;c++)
 {
     path->RevDelete(display);
  cout<<"("<<display.row<<","<<display.col<<")"<<"->";  
 }
 cout<<"("<<m<<","<<m<<")"<<endl;

 return true;
}

void main(){
 ReadIn();
 if(FindPath())
  cout<<"May be the way is not the best! But we find one way at least."<<endl;
 else
  cout<<"You can try another labyrinth!"<<endl;
}


class Position
{
 public:
  int row;
  int col;
  char* dir;
};

 

//堆栈类

//#include <iostream.h>
//using namespace std;

template<class T>
class Stack{
 public:
  Stack(int MaxStackSize);
  ~Stack(){delete [] stack;}
  bool IsEmpty() const{return top==-1;}
  bool IsFull() const{return top==MaxTop;}
  T Top() const;
  Stack<T>& Add(const T&x);
  Stack<T>& Delete(T& x);
        Stack<T>& RevDelete(T& x);
  void SetTop(){top=0;}
  int GetTop(){return top;}

 private:
  int top;
  int MaxTop;
  T *stack;
};

template<class T>
Stack<T>::Stack(int MaxStackSize)
{
 MaxTop=MaxStackSize-1;
 stack=new T[MaxStackSize];
 top=-1;
}

template<class T>
T Stack<T>::Top() const
{
 if(IsEmpty())
  cout<<"The stack is empty!"<<endl;
 else
  return stack[top];
}

template<class T>
Stack<T>& Stack<T>::Add(const T&x)
{
 if(IsFull())
  cout<<"The stack is full!"<<endl;
 stack[++top]=x;
 return *this;
}

template<class T>
Stack<T>& Stack<T>::Delete(T& x)
{
 if(IsEmpty())
  cout<<"Erro!The stack is empty!"<<endl;
 x=stack[top--];
 return *this;
}

template<class T>
Stack<T>& Stack<T>::RevDelete(T& x)
{
 if(IsEmpty())
  cout<<"Erro!The stack is empty!"<<endl;
 x=stack[top++];
 return *this;
}

 

你可能感兴趣的:(堆栈应用之迷宫寻径)