Google Foobar Level 4 RunningWithTheBunnies

问题描述

我被困在 foobar 问题的一个测试用例上。

问题和我的代码如下

你和你获救的兔子囚犯需要离开这里 崩溃的空间站死亡陷阱 - 快!很遗憾, 一些兔子因长期监禁而变得虚弱, 不能跑得很快。他们的朋友正在努力帮助他们,但这 如果你也投身进去,逃跑会快得多。防守 舱壁门已经开始关闭,如果你不通过 时间一长,你就会被困住!你需要抓住尽可能多的兔子 可以在舱壁关闭之前穿过舱壁。

从起点移动到所有地点所需的时间 兔子和舱壁将以方阵形式提供给您 整数。每一行都会告诉你到达目的地所需的时间 开始,第一个兔子,第二个兔子,...,最后一个兔子,以及舱壁 那个命令。行的顺序遵循相同的模式(开始, 每个兔子,隔板)。兔子可以跳到你的怀里,所以采摘 它们是瞬间上升的,并同时到达舱壁 它密封的时间仍然允许成功的,如果是戏剧性的,逃脱。 (别担心,你不捡的任何兔子都可以逃脱 和你在一起,因为他们不再需要携带你捡到的那些。) 如果您愿意,您可以重新访问不同的景点,然后前往 隔板并不意味着您必须立即离开 - 您可以移动到 如果时间允许,还可以从舱壁捡起更多的兔子。

除了花时间在兔子之间穿梭,一些路径 与空间站的安全检查站互动并增加时间 回到时钟。给时钟增加时间会延迟关闭 舱壁门,如果时间回到 0 或正值 门已经关闭后的数字,它会触发隔板 重新打开。因此,也许可以绕一圈走 不断获得时间:也就是说,每经过一条路径,相同的 使用或添加的时间量。

一个形式为 answer(times,time_limit) 的函数来计算 你可以捡到最多的兔子,它们是哪些兔子,而 在门永远关闭之前仍然从舱壁逃生。 如果有多组大小相同的兔子,则返回该组 按排序顺序排列的具有最低囚犯 ID(作为索引)的兔子。 兔子被表示为一个按囚犯 ID 排序的列表,其中 第一个兔子是 0。最多有 5 个兔子,time_limit 是一个 最多为 999 的非负整数。

例如,在[
[0,2,-1],# 0 = 开始
[9,# 1 = 兔子 0
[9,3,# 2 = 兔子 1
[9,# 3 = 兔子 2
[9,0],# 4 = Bulkhead ]和时间限制1,五个内部阵列行指定起点,兔子0,兔子1, bunny 2 和舱壁门出口分别。你可以把 路径:

开始结束 Delta 时间状态 - 0 - 1 舱壁最初打开 0 4 -1 2 4 2 2 0 2 4 -1 1 4 3 2 -1 舱壁关闭 3 4 -1 0 舱壁重新打开;你和兔子退出使用这个解决方案,你会拿起兔子 1 和 2。这是最好的 这个空间站走廊的组合,所以答案是 [1,2]。

测试用例输入:

(int) 次 = [[0,1,1],[1,0]] (int) time_limit = 3 输出

(int list) [0,1] 输入:

(int) 次 = [[0,[9,0]] (int) time_limit = 1 输出

(int list) [1,2]

在下面添加我的代码

public class RunningWithBunnies {

    public static class FloydWarshall {

        private final Integer[][] allPairshortestPathmatrix;
        private final Integer[][] pathfinderMatrix;

        public FloydWarshall(final Integer[][] matrix) {

            if (matrix.length != matrix[0].length) {
                throw new IllegalArgumentException("Bad Adj Matrix Passed");
            }

            allPairshortestPathmatrix = new Integer[matrix.length][matrix[0].length];
            pathfinderMatrix = new Integer[matrix.length][matrix[0].length];
            for(int i = 0; i < matrix.length; i++) {
                for(int j = 0; j < matrix[i].length; j++) {
                    allPairshortestPathmatrix[i][j] = matrix[i][j];
                    if(matrix[i][j] != null) {
                        pathfinderMatrix[i][j] = j;
                    }
                }
            }
            findAllPaiRSShortestPath(matrix);
        }

        public int getShortestdistanceBetweenNodes(final int startNode,final int endNode) {
            return allPairshortestPathmatrix[startNode][endNode];
        }

        public List<Integer> findShortestPathBetweenNodes(final int startNode,final int endNode) {

            final List<Integer> path = new ArrayList<>();
            Integer currentNode = startNode;
            path.add(startNode);

            while (currentNode != endNode) {

                currentNode = pathfinderMatrix[currentNode][endNode];
                if (currentNode == null || allPairshortestPathmatrix[currentNode][currentNode] < 0) {
                    return Collections.emptyList();
                }
                path.add(currentNode);
            }

            return new ArrayList<>(path);
        }

        public boolean doesMatrixContainNegativeCycles() {

            for (int i = 0; i < allPairshortestPathmatrix.length; i++) {
                if (allPairshortestPathmatrix[i][i] < 0) {
                    return true;
                }
            }
            return false;
        }

        private void findAllPaiRSShortestPath(final Integer[][] matrix) {

            for(int k = 0; k < matrix.length; k++) {
                for(int i = 0; i < matrix.length; i++) {
                    for(int j = 0; j < matrix[i].length; j++) {

                        final Integer directPathCost = allPairshortestPathmatrix[i][j];
                        final Integer indirectPathToIntermediateCost = allPairshortestPathmatrix[i][k];
                        final Integer indirectPathFromIntermediateCost = allPairshortestPathmatrix[k][j];

                        if (indirectPathToIntermediateCost != null && indirectPathFromIntermediateCost != null) {
                            if (directPathCost == null ||
                                (directPathCost > indirectPathToIntermediateCost + indirectPathFromIntermediateCost)) {
                                allPairshortestPathmatrix[i][j] = allPairshortestPathmatrix[i][k] + allPairshortestPathmatrix[k][j];
                                pathfinderMatrix[i][j] = pathfinderMatrix[i][k];
                            }
                        }
                    }
                }
            }
        }
    }

    public static int[] solution(int[][] times,int times_limit) {

        final Integer[][] matrix = new Integer[times.length][times[0].length];
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                matrix[i][j] = times[i][j];
            }
        }
        final FloydWarshall floydWarshall = new FloydWarshall(matrix);

        final Set<Integer> bunniesLeft = IntStream
            .range(1,matrix.length - 1)
            .Boxed()
            .collect(Collectors.toSet());

        if (floydWarshall.doesMatrixContainNegativeCycles()) {

            return bunniesLeft
                .stream()
                .mapToInt(bunny -> bunny - 1)
                .toArray();
        }

        final List<Integer[]> path = new ArrayList<>();

        int currentPos = 0;
        int pathCost = 0;
        while (!bunniesLeft.isEmpty()) {

            int nearestBunnyPathDuration = Integer.MAX_VALUE;
            int nearestBunny = -1;
            for (int bunnyPosition : bunniesLeft) {

                if (bunnyPosition != currentPos
                    && bunniesLeft.contains(bunnyPosition)
                    && floydWarshall.getShortestdistanceBetweenNodes(currentPos,bunnyPosition) < nearestBunnyPathDuration) {

                    nearestBunnyPathDuration = floydWarshall.getShortestdistanceBetweenNodes(currentPos,bunnyPosition);
                    nearestBunny = bunnyPosition;
                }
            }

            if (nearestBunny == -1) {
                break;
            }

            final List<Integer> subPath =
                floydWarshall
                    .findShortestPathBetweenNodes(currentPos,nearestBunny)
                    .stream()
                    .filter(bunniesLeft::contains)
                    .collect(Collectors.toList());

            currentPos = nearestBunny;

            for (int subPathNode : subPath) {

                path.add(new Integer[]{
                    subPathNode,path.isEmpty()
                        ? floydWarshall.getShortestdistanceBetweenNodes(0,subPathNode)
                        : pathCost + floydWarshall.getShortestdistanceBetweenNodes(path.get(path.size() - 1)[0],subPathNode)
                });
                bunniesLeft.remove(subPathNode);
                pathCost = path.get(path.size() - 1)[1];
            }
        }

        path.removeIf(pathNode ->
            pathNode[1] + floydWarshall.getShortestdistanceBetweenNodes(pathNode[0],matrix.length - 1) > times_limit
        );

        if (path.isEmpty()) {
            return new int[]{};
        }

        return path
            .stream()
            .mapToInt(node->node[0] - 1)
            .sorted()
            .toArray();
    }
}

只有测试用例 8 失败了。我在堆栈溢出中看到了其他解决方案,但没有人采用这种方法。感谢您提供有关我在这里缺少的内容的任何指示。

谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)