如何在C 中完全遍历5x5网格以产生相同长度的路径(25)

How can I fully traverse a 5x5 grid in c++ to produce a path of the same length (25)?

本文关键字:路径 网格 5x5 遍历      更新时间:2023-10-16

我正在尝试完全遍历网格并在这样做时创建路径。我递归地进入一个新的单元格(或点(,然后我将向上移动,向下,向左,向右移动,直到到达网格中的所有单元格。在此示例中,我正在遍历一个5x5网格,该网格应每次产生25个路径长度。运行此代码时,最大路径长度可达24(通过记录语句找到很多次(。任何人都看到了为什么我不能完全遍历这个网格到25的路径长度?

(解决此逻辑问题后,我确实计划为遍历添加随机性(

此代码可以通过:

来编译并用完了。

g main.cpp -o main

#include <vector>                                                                                                     
#include <list>                                                                                                       
#include <iostream>                                                                                                   
class Point {                                                                                                         
 public:                                                                                                              
  int x, y;                                                                                                           
  Point (int x, int y) : x (x), y (y) {}                                                                              
};                                                                                                                    
class TraverseGrid {                                                                                                  
 public:                                                                                                              
  TraverseGrid () { }                                                                                                 
  virtual ~TraverseGrid() {}                                                                                          
  void createPath (std::vector<Point>& path, const int& width, const int& height, Point start = Point (0, 1));        
 private:                                                                                                             
  bool recursiveFindPath (std::vector<Point>& path, int width, int height);                                           
  bool isValid (Point point, int width, int height);                                                                  
  bool inPath (std::vector<Point>path, Point point);                                                                  
};                                                                                                                    
void TraverseGrid::createPath (std::vector<Point>& path, const int& width, const int& height, Point start) {          
  path.push_back (start);                                                                                             
  bool result = recursiveFindPath (path, width, height);                                                              
  std::cout << "TraverseGrid result = " << result << " pathsize= " << path.size() << std::endl;                       
}                                                                                                                     
bool TraverseGrid::recursiveFindPath (std::vector<Point>& path, int width, int height) {                              
  if (path.size() == 24)                                                    
    std::cout << 24 << " found!" << std::endl;                         
  if (path.size() == static_cast<std::size_t>(width * height))                                                        
    return true;                                                                                                      
  std::list<Point> nextPoints;                                                                                        
  Point point = path.back();                                                                                          
  Point point1 (point.x - 1, point.y);                                                                                
  Point point2 (point.x + 1, point.y);                                                                                
  Point point3 (point.x, point.y - 1);                                                                                
  Point point4 (point.x, point.y + 1);                                                                                
  if (isValid (point1, width, height) && !inPath (path, point1))                                                      
    nextPoints.push_back (point1);                                                                                    
  if (isValid (point2, width, height) && !inPath (path, point2))                                                      
    nextPoints.push_back (point2);                                                                                    
  if (isValid (point3, width, height) && !inPath (path, point3))                                                      
    nextPoints.push_back (point3);                                                                                    
  if (isValid (point4, width, height) && !inPath (path, point4))                                                      
    nextPoints.push_back (point4);                                                                                    
  for (Point p : nextPoints) {                                                                                        
    path.push_back (p);                                                                                               
    if (recursiveFindPath (path, width, height))                                                                      
      return true;                                                                                                    
    path.pop_back();                                                                                                  
  }                                                                                                                   
  return false;                                                                                                       
}                                                                                                                     
bool TraverseGrid::isValid (Point point, int width, int height) {                                                     
  if (point.x < 0 || point.x >= width || point.y < 0 || point.y >= height)                                            
    return false;                                                                                                     
  return true;                                                                                                        
}                                                                                                                     
bool TraverseGrid::inPath (std::vector<Point>path, Point point) {                                                     
  for (Point pathPoint : path) {                                                                                      
    if (point.x == pathPoint.x && point.y == pathPoint.y)                                                             
      return true;                                                                                                    
  }                                                                                                                   
  return false;                                                                                                       
}                                                                                                                     
int main() {                                                                                                          
  TraverseGrid grid;                                                                                                  
  std::vector<Point> path;                                                                                            
  grid.createPath (path, 5, 5);                                                                                       
  std::cout << "path size = " << path.size() << std::endl;                                                            
  return 0;                                                                                                           
}                              

注意:我已经在发布这个问题之前已经处理了2天。

*这不是调试或编码问题。我对网格遍历没有正确的理解。

在调试和踏入代码并查看我的期望之后,我意识到路径方向的可能数量将花费太长的时间。我编写了另一个功能,以打印出该路径。在观看一页的一页之后,观看路径的增长然后结束后,我意识到我的代码的行为是预期的。我不知道的,但要弄清楚的是,并非总是能够完全遍历网格。我发现以下网格从" 00"开始,无法通过路径完全遍历。代码正常工作。

不可能完全遍历所有25个细胞(从00开始(

|  |00|  |  |  |
|  |  |  |  |  |
|  |  |  |  |  |
|  |  |  |  |  | 
|  |  |  |  |  |

traversegrid方法我用来显示带有路径的网格,随着路径的生长和固定。

void TraverseGrid::printVector (std::vector<Point>& path, int width, int height) {                                  
    std::cout << std::endl;                                                                                           
    for (int i = 0; i < width; i++) {                                                                                 
      for (int j = 0; j < height; j++) {                                                                              
        Point p (i, j);                                                                                               
        if (inPath (path, p))                                                                                         
          std::cout << "|" << getTrim(inPathIndex(path,p), 2);                                                        
        else                                                                                                          
          std::cout << "|  ";                                                                                         
      }                                                                                                               
      std::cout << "|" << std::endl;                                                                                  
    }                                                                                                                 
    std::cout << std::endl;
}