迷宫路径搜索

 


用c++写的一个迷宫路径搜索,数据结构的书上都有这个问题的讲解,由于本人对c++的一些语法还不熟悉,看不懂那个程序,因此自己写了个,但是在 类 Maze 中的 FindPath函数写的很不简洁,希望有人看过此程序后能给出一个简洁的程序来,我将不胜感激。在申请path和maze二维数组的内存空间时候,没有考虑申请失败的情况,是因为我不知道该如何检测申请失败,希望能给出一个代码,同样不胜感激。

此程序的代码和已经编译的exe文件在这个网盘中,http://qiuchengw.ys168.com

// 迷宫路径问题。
 
#include <cstdlib>
#include <iostream>

using namespace std;
 
// 类 Path
class Path
{    
      public:
             Path();
             ~Path();
             void InitPath(int);
             bool IsEmpty();
             void push(int, int); // 压入路径坐标
             void pop(unsigned &, unsigned &);
             void OutputMap();
      private:
              unsigned int **path;
              int top;
              unsigned PathSize;
};

// top 初始化为 -1,方便堆栈的运算。
Path::Path()
{          
    path = NULL;
    top = -1;
    PathSize = 0;
}

Path::~Path()
{           
    int i;
    for (i = 0; i < PathSize; i++)
    {  
        delete path[i];
    }
    delete []path;
    path = NULL;
}

// 申请路径 path 数组内存,path 共PathSize行, 2列,第一列存储迷宫路径
// 的行坐标,第二列存 列坐标。
void Path::InitPath(int size)
{   
     int i;
     PathSize = size * size;
     path = new unsigned *[PathSize];
     for (i = 0; i < PathSize; i++)// PathSize行,2列
     {  
         path[i] = new unsigned [2];
     }
}

// 看path堆栈是否为空,是则返回true,否则返回false
bool Path::IsEmpty()
{   
     cout<<"IsEmpty:"<<top<<endl;
     if (top == -1)
     { 
             return true;
     }
     return false;
}

// 压入路径坐标,如果路径堆栈以满,返回false,否则返回true
void Path::push(int row, int col)
{   
     top++;
     path[top][0] = row;
     path[top][1] = col;
     cout<<"push:"<<row<<","<<col<<endl;
}

//pop
void Path::pop(unsigned &r, unsigned &c)
{   
     r = path[top][0];
     c = path[top][1];
     top--;
     cout<<"pop:"<<endl;
     cout<<"r = "<<r<<endl;
     cout<<"c = "<<c<<endl<<endl;;
}

void Path::OutputMap()
{   
     int i;
     cout<<"entry -> ";
     for (i = 0; i <= top; i++)
     {  
         cout<<"("<<path[i][0]<<","<<path[i][1]<<")"<<" -> ";
     }
     cout<<"done/n";
}

// 类 maze
class Maze:public Path
{    
      public:
             Maze();
             ~Maze();
             void InitMaze();
             void AskUser();     
             void UserInputMazeMap();    // 输入迷宫地图
             bool FindPath();     // 找路径
      private:
              unsigned **maze;
              unsigned MazeSize;
};

Maze::Maze()
{          
      maze = NULL;
      MazeSize = 0;
}

Maze::~Maze()
{                 
    int i;
    for (i = 0; i < MazeSize; i++)
    {  
        delete maze[i];
    }
    delete []maze;
    maze = NULL;
}

// 问用户迷宫的大小,这个大小应该在 1~16 之间
void Maze::AskUser()
{   
     int size;
     do
     {
         cout<<"输入迷宫大小:";
         cin>>size;
         if (size <= 0)
         { 
             cout<<"迷宫太小了"<<endl;
         }
         if (size > 16)
         { 
             cout<<"迷宫太大了"<<endl;
         }
     }while(size <= 0 && size >= 16);
    
     MazeSize = size + 2; // 加上边界
}

// 申请迷宫需要的内存
void Maze::InitMaze()
{   
     int i;
     maze = new unsigned *[MazeSize];// 申请内存
     for (i = 0; i < MazeSize; i++)
     {  
         maze[i] = new unsigned [MazeSize];
     }
    
     for (i = 0; i < MazeSize; i++) // 边界初始化为1
     {  
         maze[0][i] = 1; // 上
         maze[MazeSize-1][i] = 1; // 下
         maze[i][0] = 1; // 左
         maze[i][MazeSize-1] = 1; // 右
     }
    
     InitPath(MazeSize - 2);
}

// 用户输入迷宫地图
void Maze::UserInputMazeMap()
{   
     int r, c;
     for (r = 1; r <= MazeSize - 2; r++)
     {  
         cout<<"输入第"<<r<<"行:";
         for (c = 1; c <= MazeSize - 2; c++)
         {  
             cin>>maze[r][c];
         }
     }
}

// 寻找迷宫的路径,按照 下->右->左->上 的顺序搜索
bool Maze::FindPath()
{   
     unsigned r , c; // r是行,c是列
     unsigned up, down, left, right; // 上下左右是否能走,0能走,1不能
    
     // 入口是 (1,1)
     r = 1;
     c = 1;
    
     maze[r][c] = 1;
     push(r,c);
     // 当不是出口时
     while (r != MazeSize - 2 || c != MazeSize - 2)
     {    
           //取得当前坐标 上下左右 是否可行
           up = maze[r-1][c];
           down = maze[r+1][c];
           left = maze[r][c-1];
           right = maze[r][c+1];
          
           while (true)     // 用while只是为了只执行一个 if 语句后跳出,不是为了循环,用switch语句应该更好点
           {    
                 if (down == 0)
                 { 
                        ++r;
                        push(r,c);// 坐标写入路径
                        maze[r][c] = 1; // 阻止访问刚才走过的坐标
                        break;
                 }
                
                 if (right == 0)
                 { 
                       
                        ++c;
                        push(r,c);// 坐标写入路
                        maze[r][c] = 1;
                        break;
                 }
                
                 if (left == 0)
                 {   
                        --c;
                        push(r,c);// 坐标写入路径
                        maze[r][c] = 1;
                        break;
                 }
                
                 if (up == 0)
                 { 
                        --r;
                        push(r,c);// 坐标写入路径
                        maze[r][c] = 1;
                        break;
                 }
                
                 // 上下左右都不可走 ,则回走堆栈内已经存在的路径
                     if (IsEmpty() == false) // 堆栈为空,说明无路径
                     { 
                         pop(r,c);
                         break;
                     }
                     else
                     {
                         cout<<"无路径"<<endl;
                         return false;
                     }
           }
     }
     return true;
}


int main()
{
    Maze a;
   
    a.AskUser();
    a.InitMaze();
    a.UserInputMazeMap();
    if (a.FindPath() == true)
    { 
       a.OutputMap();   
    }
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

你可能感兴趣的:(C++,c,null,delete,Path,iostream)