在2D整数数组Java

时间:2017-04-11 19:02:47

标签: java arrays shortest-path dijkstra a-star

我正在进行蛇形游戏,其中蛇穿过2D int数组作为其地形。存储在2D数组中的值表示跨越所需的时间(以秒为单位)。

例如,

int[][] MAP = { 
    { 1, 1, 1, 2, 2 },
    { 1, 2, 2, 2, 2 },
    { 3, 2, 2, 3, 1 },
    { 1, 1, 3, 2, 1 },
    { 1, 1, 3, 2, 1 }
 };

所以从map[0][0]map[0][4]需要1 + 1 + 2 + 2秒。我如何制作一种能够找到蛇从位置map[startX][startY]移动到map[endX][endY]的最短路径的算法?

这不是家庭作业,我只是为了好玩而制作游戏,并想学习如何做到这一点。

1 个答案:

答案 0 :(得分:0)

在讨论“动态编程”时,这是一个众所周知的问题,甚至类似于old post

尽管如此,我还没有找到能够打印出最短路径的解决方案,所以:

public class FastestPathCalculator {

    private final int[][] map;

    public FastestPathCalculator(int[][] map) {
        this.map=map;
    }

    public static void main(String[] args) {
        int[][] map = new int[][]{
                {1, 1, 1, 4, 2},
                {1, 2, 5, 2, 2},
                {3, 2, 2, 3, 1},
                {1, 1, 3, 2, 1},
                {3, 1, 3, 2, 1}
        };

        FastestPathCalculator c = new FastestPathCalculator(map);
        boolean[] result = c.computeFastestPath(map);
        c.printPath(result);
    }

这里,布尔数组表示从(0,0)到(4,4)的步骤。值为TRUE表示步骤向右,FALSE表示向下。 在此示例中,阵列有8个单元格。

    public boolean[] computeFastestPath(int[][] map) {
        int pathLength = map.length + map[0].length - 2;
        boolean[] result = new boolean[pathLength];

        int[][] mapOfMinimalSums = buildMapOfMinimalSums();

        int x = map.length-1;
        int y = map[0].length-1;

        for (int i = pathLength - 1 ; i >= 0; i--) {
            if (x == 0)
                result[i] = true;
            else if (y == 0)
                result[i] = false;
            else if (mapOfMinimalSums[x][y] == map[x][y] + mapOfMinimalSums[x][y-1]) {
                result[i] = true;
                y--;
            }
            else {
                result[i] = false;
                x--;
            }
        }

        return result;
    }

    public void printPath(boolean[] result) {
        String[][] newPath = new String[map.length][map[0].length];
        int x = 0;
        int y = 0;

        newPath[x][y] = String.valueOf(map[x][y]);

        for (int i = 0 ; i < result.length; i++) {
            if (result[i]) {
                y++;
            } else {
                x++;
            }
            newPath[x][y] = String.valueOf(map[x][y]);
        }

        for (int i = 0 ; i < map.length; i++) {
            for (int j = 0 ; j < map[0].length; j++) {
                if (newPath[i][j] == null) {
                    System.out.print(" , ");
                } else {
                    System.out.print(newPath[i][j] + ", ");
                }
            }
            System.out.println();
        }
        System.out.println();
    }

    private int[][] buildMapOfMinimalSums() {
        int[][] mapOfSums = new int[map.length][map[0].length];
        for (int i = 0 ; i < map.length; i++) {
            for (int j = 0 ; j < map[0].length; j++) {
                if (i == 0 && j == 0)
                    mapOfSums[i][j] = map[i][j];
                else if (i == 0) {
                    mapOfSums[i][j] = mapOfSums[i][j - 1] + map[i][j];
                }
                else if (j == 0)
                    mapOfSums[i][j] = mapOfSums[i-1][j] + map[i][j];
                else
                    mapOfSums[i][j] = Math.min(mapOfSums[i-1][j], mapOfSums[i][j-1]) + map[i][j];
            }
        }
        return mapOfSums;
    }
}
相关问题