我实现了一条计算最短路线的代码,但我想添加一个条件。如果我得到这样的矩阵:

1 0 0 0 0 0 0 0
1 0 0 1 1 1 1 0
0 0 0 0 0 0 0 0
0 0 1 1 1 0 1 0
A 0 0 0 0 0 0 0
0 0 1 1 0 B 0 1
0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0


我如何添加一个条件,即从单元格A到单元格B只有在行距为1或一列为0的情况下才能移动,因此从A到B的最短路径(A在[4; 0]和B上在[5; 5]上)是:[4; 0],[5; 0],[6; 0],[7; 0],[7; 1],[7; 2],[7,3 ],[7,4],[7,5],[6,5],[5; 5]。
这是我目前所拥有的:

   import java.util.LinkedList;

public class ShortestPathBetweenCellsBFS {

    private static class Cell  {
        int x;
        int y;
        int dist;   //distance
        Cell prev;  //parent cell in the path

        Cell(int x, int y, int dist, Cell prev) {
            this.x = x;
            this.y = y;
            this.dist = dist;
            this.prev = prev;
        }

        @Override
        public String toString(){
            return "(" + x + "," + y + ")";
        }
    }


    public static void shortestPath(int[][] matrix, int[] start, int[] end) {
        int sx = start[0], sy = start[1];
        int dx = end[0], dy = end[1];


        int m = matrix.length;
        int n = matrix[0].length;
        Cell[][] cells = new Cell[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] != 1) {
                    cells[i][j] = new Cell(i, j, Integer.MAX_VALUE, null);
                }
            }
        }

        LinkedList<Cell> queue = new LinkedList<>();
        Cell src = cells[sx][sy];
        src.dist = 0;
        queue.add(src);
        System.out.println(src);
        Cell dest = null;
        Cell p;
        while ((p = queue.poll()) != null) {
            //find destination
            if (p.x == dx && p.y == dy) {
                dest = p;
                break;
            }

            // moving up
            visit(cells, queue, p.x - 1, p.y, p);

            // moving down
            visit(cells, queue, p.x + 1, p.y, p);

            // moving left
            visit(cells, queue, p.x, p.y - 1, p);

            //moving right
            visit(cells, queue, p.x, p.y + 1, p);
        }

        if (dest == null) {
            return;
        } else {
            LinkedList<Cell> path = new LinkedList<>();
            p = dest;
            do {
                path.addFirst(p);
            } while ((p = p.prev) != null);
            System.out.println(path);
        }
    }

    //function to update cell visiting status
    static void visit(Cell[][] cells, LinkedList<Cell> queue, int x, int y, Cell parent) {
        if (x < 0 || x >= cells.length || y < 0 || y >= cells[0].length || cells[x][y] == null) {
            return;
        }

        int dist = parent.dist + 1;
        Cell p = cells[x][y];
        if (dist < p.dist) {
            p.dist = dist;
            p.prev = parent;
            queue.add(p);
        }
    }

    public static void main(String[] args) {
       int[][] matrix = {
           {1, 0, 0, 0, 0, 0, 0, 0},
           {1, 0, 0, 1, 1, 1, 1, 0},
           {0, 0, 0, 0, 0, 0, 0, 0},
           {0, 0, 1, 1, 1, 0, 1, 0},
           {0, 0, 0, 0, 0, 0, 0, 0},
           {0, 0, 1, 1, 0, 0, 0, 1},
           {0, 0, 0, 0, 0, 0, 0, 1},
           {0, 0, 0, 0, 0, 0, 0, 0}
       };
       int[] start = {4, 0};
       int[] end = {5, 5};
       shortestPath(matrix, start, end);
    }
}

最佳答案

一种可能的解决方案的基本思想是,如果到任何墙壁的距离至少为一个,则仅将单元格添加到寻路算法的队列中。

因此,例如在您的visit方法中,您还必须检查所有相邻的单元格,而不是仅检查cells[x][y]

一种方法是在发现障碍物的单元格上遍历相邻单元格并中止。

for (int x_test = Math.max(0, x - 1); x_test <= Math.min(cells.length - 1; x + 1); x_test++) {
    for (int y_test = Math.max(0, y - 1); y_test <= Math.min(cells[0].length - 1; y + 1); y_test++) {
        if (cells[x][y] == null) {
            return;
        }
    }
}


请注意,我在循环中使用Math.minMath.max来防止超出范围的访问。

10-04 12:44