繁体   English   中英

如何计算最短路径?

[英]How to calculate the shortest path?

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

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 它只能在一行或一列的距离为 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);       
    } 
}

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

因此,例如在您的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以防止越界访问。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM