2D阵列中从最大到最小的最长路线

时间:2016-08-24 16:18:45

标签: java arrays multidimensional-array longest-path

有二维数组long[50][50],其中填充了从0到100的随机数。我需要找到从最大(或第一高)到最小的最长路。你可以向上,向下,向左和向右移动。

我找到了如何找到单一的方法:找到最大的最接近的数字(但不是更大,就是这样)并移动到那里。

public static int measure = 50;
public long[][] map = new long[measure][measure];

我的移动方法:

private long moveUp(int x, int y) {
    if (x >= measure || x == 0 || y == 0 || y >= measure) {
        return -1;
    }
    return map[x - 1][y];
}

private long moveRight(int x, int y) {
    if (x >= measure || x == 0 || y == 0 || y >= measure) {
        return -1;
    }
    return map[x][y + 1];
}

private long moveDown(int x, int y) {
    if (x >= measure || x == 0 || y == 0 || y >= measure) {
        return -1;
    }
    return map[x + 1][y];
}

private long moveLeft(int x, int y) {
    if (x >= measure || x == 0 || y == 0 || y >= measure) {
        return -1;
    }
    return map[x][y - 1];
}

查找最近的最大值:

 private long rightWay(int x, int y) {
    List<Long> pickList = new ArrayList<>();
    long up = moveUp(x, y);
    long right = moveRight(x, y);
    long down = moveDown(x, y);
    long left = moveLeft(x, y);
    if (up != -1 && up < map[x][y]) {
        pickList.add(moveUp(x, y));
    }
    if (right != -1 && right < map[x][y]) {
        pickList.add(moveRight(x, y));
    }
    if (down != -1 && down < map[x][y]) {
        pickList.add(moveDown(x, y));
    }
    if (left != -1 && left < map[x][y]) {
        pickList.add(moveLeft(x, y));
    }
    if (pickList.size() == 0) {
        return -1;
    } else {
        Collections.sort(pickList);
        for (int i = 0; i < pickList.size(); i++) {
            System.out.println("right way " + i + " -> " + pickList.get(i));
        }
        return pickList.get(pickList.size() - 1);
    }

}

然后找到最长的方式,只使用最接近的最大值:

private void findRoute(long[][] route, long current, int width, int height) {
    System.out.println("width = " + width + " height = " + height);
    long nextSpetHeight = rightWay(width, height);
    System.out.println("max = " + nextSpetHeight);
    if (nextSpetHeight == -1) {
        return;
    } else {
        if (nextSpetHeight == moveUp(width, height)) {
            findRoute(route, nextSpetHeight, width - 1, height);
            way.add(nextSpetHeight);
        }
        if (nextSpetHeight == moveRight(width, height)) {
            findRoute(route, nextSpetHeight, width, height + 1);
            way.add(nextSpetHeight);
        }
        if (nextSpetHeight == moveDown(width, height)) {
            findRoute(route, nextSpetHeight, width + 1, height);
            way.add(nextSpetHeight);
        }
        if (nextSpetHeight == moveLeft(width, height)) {
            findRoute(route, nextSpetHeight, width, height - 1);
            way.add(nextSpetHeight);
        }
    }
}

way的大小将是这种路线的长度。 但现在我不知道如何从某些坐标找到所有可能的路线以找到最长的路线。我的意思是我不知道什么是最好的回归方式&#34; fork&#34;并继续另一条路线。

我希望解释清楚。提前谢谢。

1 个答案:

答案 0 :(得分:4)

如果您将此问题视为有向图问题,则可以应用已知的图算法。

这是MSDN documentation

的实现

第一次,它在点矩阵上搜索局部最大值。它们将是第一个候选者,然后算法迭代候选者,它遵循约翰逊的算法。它计算所有长度到任何一点。

PixelHeight

打印(使用10 x 10矩阵)

  • 第一块:2D矩阵(10x10)

  • 第二块:从最小值到最大值的最长解的坐标。

  • 最后一个块:坐标的长度。

输出:

public class Solver {

    private static class Point {
        int x;
        int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

    private static class State {

        static State best;
        State parent;
        List<State> children = new ArrayList<>();
        int length;
        Point p;

        public State(State parent, Point p) {
            this.parent = parent;
            this.p = p;
            this.length = parent.length + 1;
            this.parent.children.add(this);
            if (best.length < length) {
                best = this;
            }
        }

        public State(Point p) {
            this.parent = null;
            this.p = p;
            this.length = 1;
            if (best == null) {
                best = this;
            }
        }

        public void checkParent(State st) {
            if (st.length + 1 > length) {
                parent.children.remove(this);
                this.parent = st;
                updateLength();
            }
        }

        private void updateLength() {
            this.length = parent.length + 1;
            if (best.length < length) {
                best = this;
            }
            for (State state : children) {
                state.updateLength();
            }
        }
    }

    public static boolean checkRange(int min, int max, int x) {
        return min <= x && x < max;
    }

    public static boolean maxLocal(int x, int y, int[][] points) {
        int value = points[x][y];
        if (x > 0 && points[x - 1][y] > value) {
            return false;
        }
        if (y > 0 && points[x][y - 1] > value) {
            return false;
        }
        if (x < points.length - 1 && points[x + 1][y] > value) {
            return false;
        }
        return !(y < points[0].length - 1 && points[x][y + 1] > value);
    }

    private static List<Point> getNeigbours(int x, int y, int[][] points) {
        int value = points[x][y];
        List<Point> result = new ArrayList<>(4);
        if (x > 0 && points[x - 1][y] < value) {
            result.add(new Point(x - 1, y));
        }
        if (y > 0 && points[x][y - 1] < value) {
            result.add(new Point(x, y - 1));
        }
        if (x < points.length - 1 && points[x + 1][y] < value) {
            result.add(new Point(x + 1, y));
        }
        if (y < points[0].length - 1 && points[x][y + 1] < value) {
            result.add(new Point(x, y + 1));
        }
        return result;
    }

    private static int[][] generateRandomPoint(int width, int height, int max) {
        int[][] result = new int[width][height];
        Random rand = new Random(0L);
        for (int i = 0; i < result.length; i++) {
            for (int j = 0; j < result[i].length; j++) {
                result[i][j] = rand.nextInt(max);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[][] points = generateRandomPoint(50, 50, 100);
        State[][] states = new State[points.length][points[0].length];
        List<State> candidates = new ArrayList<>(points.length*points[0].length);
        for (int x = 0; x < points.length; x++) {
            for (int y = 0; y < points[0].length; y++) {
                if (maxLocal(x, y, points)) {
                    states[x][y] = new State(new Point(x, y));
                    candidates.add(states[x][y]);
                }
            }
        }
        while (!candidates.isEmpty()) {
            State candidate = candidates.remove(candidates.size() - 1);
            for (Point p : getNeigbours(candidate.p.x, candidate.p.y, points)) {
                if (states[p.x][p.y] == null) {
                    states[p.x][p.y] = new State(candidate, p);
                    candidates.add(states[p.x][p.y]);
                } else {
                    states[p.x][p.y].checkParent(candidate);
                }
            }
        }
        State temp = State.best;
        List<Point> pointList = new ArrayList<>(temp.length);
        while (temp != null) {
            pointList.add(temp.p);
            temp = temp.parent;
        }
        for (int x = 0; x < points.length; x++) {
            for (int y = 0; y < points[0].length; y++) {
                if (points[x][y] < 10) {
                    System.out.print("  ");
                } else if (points[x][y] < 100) {
                    System.out.print(" ");
                }
                System.out.print(points[x][y] + " ");
            }
            System.out.println();
        }
        System.out.println("-------");
        for (Point point : pointList) {
            System.out.println(point.x + ", " + point.y + " -> " + points[point.x][point.y]);
        }

        System.out.println();
        System.out.println("lengths:");
        for (int x = 0; x < points.length; x++) {
            for (int y = 0; y < points[0].length; y++) {
                if (states[x][y].length < 10) {
                    System.out.print(" ");
                }
                System.out.print(states[x][y].length + " ");
            }
            System.out.println();
        }
    }
}