如何计算最短路径?

huangapple 未分类评论44阅读模式
英文:

How to calculate the shortest path?

问题

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 && isAdjacentToZero(cells, x, y)) {
            p.dist = dist;
            p.prev = parent;
            queue.add(p);
        }
    }

    // function to check if a cell is adjacent to a zero cell
    static boolean isAdjacentToZero(Cell[][] cells, int x, int y) {
        int[] dx = {-1, 0, 1, 0};
        int[] dy = {0, 1, 0, -1};

        for (int i = 0; i < 4; i++) {
            int newX = x + dx[i];
            int newY = y + dy[i];
            if (newX >= 0 && newX < cells.length && newY >= 0 && newY < cells[0].length) {
                if (cells[newX][newY] != null && cells[newX][newY].dist == 0) {
                    return true;
                }
            }
        }

        return false;
    }

    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);
    }
}
英文:

I implemented a code to calculate the shortest route,but i want to add a condition.If i got a matrix like this :

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

How can i add a condition that from cell A to cell B it can move only if there is a distance of one row or one column of 0 , so the shortest path from A to B (A is on [4;0] and B is on [5;5]) is : [4;0] , [5;0] , [6;0] , [7;0] , [7;1] , [7;2] , [7,3] , [7,4] , [7,5] , [6,5] , [5;5] .
This is what i have at the moment:

   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 &quot;(&quot; + x + &quot;,&quot; + y + &quot;)&quot;;
        }
    }
    

    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 &lt; m; i++) {
            for (int j = 0; j &lt; n; j++) {
                if (matrix[i][j] != 1) {
                    cells[i][j] = new Cell(i, j, Integer.MAX_VALUE, null);
                }
            }
        }
       
        LinkedList&lt;Cell&gt; queue = new LinkedList&lt;&gt;();       
        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 &amp;&amp; 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&lt;Cell&gt; path = new LinkedList&lt;&gt;();
            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&lt;Cell&gt; queue, int x, int y, Cell parent) { 
        if (x &lt; 0 || x &gt;= cells.length || y &lt; 0 || y &gt;= cells[0].length || cells[x][y] == null) {
            return;
        }
        
        int dist = parent.dist + 1;
        Cell p = cells[x][y];
        if (dist &lt; 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

得分: 0

以下是翻译好的内容:

一个可能解决方案的基本思路是,只有当单元格到任何墙壁的距离至少为一时,才将它们添加到路径查找算法的队列中。

例如,在你的 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 来防止越界访问。

英文:

The basic idea of one possible solution would be to only add cells to the queue of your pathfinding algorithm if they have a distance of at least one to any wall.

So for example in your visit method, you would have to check all the neighbouring cells as well instead of only cells[x][y].

One way to do this would be to loop over the neighbouring cells and abort, when you find a cell that is an obstacle.

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

Note that I am using Math.min and Math.max in the loops to prevent out of bounds accesses.

huangapple
  • 本文由 发表于 2020年4月4日 21:08:10
  • 转载请务必保留本文链接:https://java.coder-hub.com/61028567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定