【问题标题】:Trouble finding path in a grid在网格中查找路径时遇到问题
【发布时间】:2013-11-24 19:43:50
【问题描述】:

我正在尝试查找棋盘上左上角和右下角单元格之间的路径数。我只能移动到相邻的右侧和相邻的底部单元格。这样我最多可以有 2 个不相交的简单路径。我正在使用递归方法。

    private void processMatrix() 
{
    if(matrix[0][0]!=1 || matrix[0][0]!=matrix[ROWS-1][COLUMNS-1])
        System.out.println("No Path Exists between bottom right and top left cells");
    int row=0;
    int col=0;
    traverse(row,col);
}

    private boolean traverse(int row, int col) 
{
    path.add(new Point(row,col));

    if(row+1<ROWS)
    {
        if(matrix[row+1][col]==0)
        {
            return false;
        }
        if(matrix[row+1][col]==1)
        {
            traverse(row+1,col);
        }
    }
    if(col+1<COLUMNS)
    {
        if(matrix[row][col+1]==0)
         {
             return false;
         }
        if(matrix[row][col+1]==1)
        {
            traverse(row,col+1);
        }
    }
    if(col==COLUMNS-1 && row==ROWS-1)
        return true;
    return false;
}

但是使用此代码,我只能遍历下三角矩阵。当我在 traverse() 函数中反转 if 块的顺序时,我只能遍历上三角矩阵中的路径。无法弄清楚出了什么问题。我也想检测相交的路径。请帮忙。

编辑: 矩阵由 0 和 1 组成。 如果路径由仅包含 1 的相邻单元连接,则存在路径。换句话说,路径将是一串 1。

【问题讨论】:

    标签: java algorithm graph path grid


    【解决方案1】:

    您的代码似乎过于复杂。如果您的矩阵是 0 和 1,请使用 boolean 而不是 int。不相交的路径似乎有点傻,因为所有路径在出租车几何中都同样有效。这是查找所有路径数量的简单解决方案,它将向您展示它如何与 system.out 打印块一起使用:

    int rows = 9;
    int columns = 8;
    boolean[][] matrix = new boolean[rows][columns];
    for (boolean[] arr : matrix) {/* Set values of matrix such that true = can pass thru that space, false = space blocked */
        Arrays.fill(arr, true);
    }
    matrix[4][6] = false;
    matrix[2][5] = false;
    int[][] paths = new int[rows][columns];//number of paths reaching each space in i steps
    paths[0][0] = 1; //Starting space
    for (int i = 0; i < rows + columns - 2; i++) {//Taxicab distance is always x+y, i = distance travelled so far
        int[][] newPaths = new int[rows][columns]; //number of paths reaching each space in i+1 steps
        for (int x = i >= columns ? i - columns + 1 : 0; x <= i && x < rows;) { //x is traditionally columns but it doesn't matter
            int y = i - x; //if statement is x declaration ensures that this is < columns
            int newX = x + 1; //will be used repeatedly
            int newY = y + 1; //will be used repeatedly
            if (newX < rows && matrix[newX][y]) newPaths[newX][y] += paths[x][y];
            if (newY < columns && matrix[x][newY]) newPaths[x][newY] += paths[x][y];
            x = newX;
        }
        paths = newPaths;
        for (int x = 0; x < rows; x++) { //optional, show the algorithm at work
            for (int y = 0; y < columns; y++) {
                int r = paths[x][y];
                System.out.print(r);
                if (r < 100) System.out.print(" ");
                if (r < 10) System.out.print(" ");
            }
            System.out.println();
        }
        System.out.println();
    }
    System.out.println(paths[rows - 1][columns - 1]); //result
    

    如果您只想确定路径是否存在,请将 int[][] 路径替换为 boolean[][] 路径并相应地更改操作。

    【讨论】:

      【解决方案2】:

      这是一个基本的动态规划问题。

      dp[R][C] 是从左上角单元格到R 行和C 列上的单元格的路径数(1-indexed)。

      1. 初始值(当 R 或 C 为 1 时):
        • dp[R][1] = 1 如果matrix[i][1]i = 1..R 具有值1,否则dp[R][1] = 0
        • dp[1][C] = 1 如果matrix[1][j]j = 1..C 的值为1,否则dp[1][C] = 0
      2. 然后dp[R][C] = dp[R-1][C] + dp[R][C-1] 如果matrix[R][C] = 1,否则dp[R][C] = 0(对于R,C >= 2)

      就是这样。这个想法背后的直觉是,如果我们知道我们可以通过 N 条不同的道路到达 R-1 行 C 列的单元格,那么如果我们向下走一次,我们将获得 N 条不同的道路到达单元格 [R, C]。类比地从单元格 [R, C-1] 到 [R, C]。

      最后,答案在dp[N][M],其中 N 和 M 是矩阵的维度。

      【讨论】:

        猜你喜欢
        • 2011-11-22
        • 1970-01-01
        • 2023-02-24
        • 1970-01-01
        • 1970-01-01
        • 2020-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多