【问题标题】:All possible paths between two points of a matrix in C++ [closed]C ++中矩阵的两点之间的所有可能路径[关闭]
【发布时间】:2017-11-03 22:09:09
【问题描述】:

给定一个 NxM 平面,通过三个运动找到两点之间的所有可能路径:右、下和对角线(右下,315º)。

我被这个问题困住了。我必须以两种方式解决它:递归和迭代。对于迭代,我有想法但不知道如何实现。

我的想法的描述是:

(谷歌翻译)。我们面临着一块 N 列宽和 M 行高的板。我们必须从一个点 (x1, y1) 到达另一个点 (x2, y2),所以 (xi, yj) 不能分别大于 N 或 M。移动的限制是我们只能向右移动 (1)、对角线向下 (2) 和向下 (3),每次移动我们都会在括号中分配数字。鉴于这些运动,我们意识到我们不能向左或向上,所以 x1 必须小于 x2 并且 y1 必须大于 y2。 该算法将从第一个点 (x1, y1) 开始并尝试创建三个“孩子”,每个动作一个: 右 (x1 + 1) 对角线 (x1 + 1, y1-1) 向下 (y1-1) 每个孩子被接受的条件是已经说过的,x1 = y2。

迭代将尝试从每个接受的孩子再创建 3 个孩子(每个动作一个)。

此迭代将通过每个子项的 (x1, y1) 等于 (x2, y2) 来结束。

提前致谢。

【问题讨论】:

  • 你知道深度优先和广度优先的区别吗?
  • 您需要计算路径还是枚举它们(创建所有路径的列表)?
  • 欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 on topichow to ask 在这里申请。 “我被卡住了”和“我不知道如何实施”建议您需要与本地导师进行会话,而不是 Stack Overflow。

标签: c++ algorithm recursion path iteration


【解决方案1】:

一个简单的递归解决方案是使用每个递归调用创建新的孩子,例如

int foundSolutions = 0;
boolean pathExistsBetweenPoints(Point source, Point target, List<Point> previousPoints) {
    //Check if we're there yet
    if(source.x == target.x && source.y == target.y) {
        foundSolutions++;
        //Here you can display previousPoints if you want a possible solution
        return true; 
    }
    if(source.x > N || source.y > M || source.x > target.x || source.y > target.y)  {
        return false; //This trail is out of bounds
    }
    //Add this point to the list of points
    List<Point> newPoints = previousPoints.add(source);
    //Get the childpoints
    Point rightPoint = new Point(source.x, source.y + 1);
    Point diagonalPoint = new Point(source.x + 1, source.y + 1);
    Point downPoint = new Point(source.x + 1, source.y);
    //Check if any of the childs can reach the target
    boolean validPath = pathExistsBetweenPoints(rightPoint, target, previousPoints);
    validPath = validPath || pathExistsBetweenPoints(diagonalPoint, target, previousPoints);
    validPath = validPath || pathExistsBetweenPoints(downPoint, target, previousPoints);
    return validPath;
}

我不熟悉 C++ 语法,但这应该是可以翻译的

【讨论】:

    【解决方案2】:

    迭代方法: 这可以通过记忆来解决,这意味着您可以跟踪号码。到达矩阵 M 中某个单元的方法。

    最初M[x][y] = 0 , for all x,y. b/w sx, dx and sy,dy respecitevly

    伪代码: 我们首先从源单元 (sx,sy) 开始,只有一种方法可以到达它,因此

    sx,sy // source cell
    dx,dy // destination cell
    M[sx][sy] = 1 ;
    for row x between sx, dx :
      for column y between sx, dy:
         if y-1 >= sy:
              M[x][y] += M[x][y-1] //  coming to x,y from left cell 
         if x-1 >= sx:
              M[x][y] += M[x][y-1] //  coming to x,y from top cell  
         if x-1 >= sx && y-1 >= sy:
              M[x][y]  += M[x-1][y-1] // coming from diagonal left cell.
    

    答案是 M[dx][dy]。

    递归方法: 采用与迭代方法相同的思想,同样可以递归实现:

    Initially M[x][y] == -1: for all x b/w sx and dx , for all y b/w sy and dy
    
    path( row, col )
     if row == dx && col == dy:
        return 1  
     if M[x][y] != -1: return M[x][y] // this means result for this is already calculated
     M[x][y] = 0                     
     if col+1 <= dy:
           M[row][col] += path(row, col+1)  // going right
     if row+1 <= dx:
           M[row][col] += path (row+1, col)  // going down
     if row+1 <= dx && col+1 <= dy: 
          M[row][col] += path(row+1, col+1) // going diagonally right
    
     return M[row][col]  
    

    答案将 M[sx][sy]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 1970-01-01
      • 2021-12-14
      • 2018-09-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多