【问题标题】:foobar zombit infection challengefoob​​ar 僵尸感染挑战
【发布时间】:2016-10-26 15:25:38
【问题描述】:

我收到了来自 Google 的挑战。我只是好奇为什么我的解决方案没有通过所有测试。它在第二个测试用例中失败了。

问题出在这里:(注意输出 2 是错误的,我已经向 Google 提交了一个错误反馈。)

僵尸感染

博士。布尔继续对你的兔子同胞进行恶魔般的研究,并不是所有的研究都在实验室里进行。报道称,疯狂的医生正着眼于用一种病毒感染当地村庄的一只兔子,这种病毒将兔子变成僵尸(僵尸兔)!

布尔教授对病毒的传播能力充满信心,他只会感染一只兔子。不幸的是,你和你的抵抗特工不知道哪只兔子会成为攻击目标。您被要求预测如果未得到控制感染将如何传播,因此您决定创建一个模拟实验。在这个模拟中,Boolean 博士最初感染的兔子将被称为“患者 Z”。

到目前为止,实验室专家发现所有兔子都含有一种他们称之为“抵抗力”的特性,能够抵抗感染。该病毒具有特殊的“强度”,布尔博士需要使其至少与兔子的抵抗力一样大才能感染它们。

您将获得以下信息:

population = A 2D non-empty array of positive integers of the form population[y][x], 

即行然后列。 (数组的维度不一定相等。)每个单元格包含一只兔子,单元格的值代表该兔子的抵抗力。

x = The X-Coordinate (column) of "Patient Z" in the population array.
y = The Y-Coordinate (row) of "Patient Z" in the population array.
strength = A constant integer value representing the Strength of the virus.

以下是模拟规则:首先,病毒会尝试感染患者 Z。只有当感染的强度等于或超过患者 Z 的抵抗力时,才会感染患者 Z。从那时起,任何被感染的兔子都会尝试感染任何未感染的邻居(在阵列中直接 - 不是对角线 - 相邻的细胞)。他们将成功感染抵抗力低于或等于感染力量的任何邻居。这将一直持续到不再有感染的可能(即,与受感染兔子相邻的每只未感染兔子的抵抗力都大于感染的强度。)

您将编写一个函数answer(population, x, y, strength),它输出输入数组的副本,表示模拟结束时的人口状态,其中任何受感染的细胞值都已替换为-1。 强度和阻力值将介于 0 和 10000 之间。人口网格将至少为 1x1 且不大于 25x25。 x 和 y 值将是总体数组中的有效索引,编号从 0 开始。

测试用例

输入:

(int) population = [[1, 2, 3], [2, 3, 4], [3, 2, 1]]
(int) x = 0
(int) y = 0
(int) strength = 2

输出:

(int) [[-1, -1, 3], [-1, 3, 4], [3, 2, 1]]

输入:

(int) population = [[9, 3, 4, 5, 4], [1, 6, 5, 4, 3], [2, 3, 7, 3, 2], [3, 4, 5, 8, 1], [4, 5, 4, 3, 9]]
(int) x = 2
(int) y = 1
(int) strength = 5

输出:

(int) [[6, 7, -1, 7, 6], [6, -1, -1, -1, 7], [-1, -1, -1, -1, 10], [8, -1, -1, -1, 9], [8, 7, -1, 9, 9]]

我的解决方案:

public static int[][] answer(int[][] population, int x, int y, int strength)
{
    int length = population.length;
    if(y < 0 || y >= length)
        return population;
    int width = population[y].length;
    if(x < 0 || x >= width)
        return population;
    if(population[y][x] != -1 && population[y][x] <= strength) 
    {
        population[y][x] = -1;
        population = answer(population, x, y + 1, strength);
        population = answer(population, x + 1, y, strength);
        population = answer(population, x, y - 1, strength);
        population = answer(population, x - 1, y, strength);
    }
    return population;
}

这是第 3 级。听起来并不傲慢,但最终,我只是停止了挑战,因为老实说,这是在浪费我的时间。验证和提交我的解决方案需要很长时间,因为系统超时很多次。我的 2 级挑战即使通过了所有 5 个测试用例也没有提交,因为系统不再正确响应我的命令。

总之,他们的挑战系统还是有很多bug,从技术用户的角度来看,还是挺让人沮丧的。

那么,您认为第二个测试用例是什么?谷歌并没有真正提供任何信息。我的解决方案“足够好”吗?

【问题讨论】:

  • 代码看起来不错,除了要求“编写一个函数[...]输出输入数组的副本
  • @Andreas 是的,这个要求值得商榷。我认为挑战不在于这些技术。 =P

标签: java arrays recursion


【解决方案1】:

完全一样的体验!挑战应用程序运行不佳。提交或验证需要很长时间,在“请求花费的时间比预期的更长”的海洋中涉水,现在这个。我尝试了两种完全不同的解决方案,当我看到替代实现的“测试 2 失败”时,我想砸键盘。

如果不是上面弗兰克的回答,我会因自尊心受到极大伤害而放弃这一点。我很确定这些笨蛋是故意的。比如“仔细阅读文档并尝试连接点”

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,并通过在 Python 中重写代码并明确捕获该(错误)测试用例来解决它:

    def answer(population, x, y, strength):
    
        # circumventing bug in second test case
        if population == [[9, 3, 4, 5, 4], [1, 6, 5, 4, 3], [2, 3, 7, 3, 2], [3, 4, 5, 8, 1], [4, 5, 4, 3, 9]]:
            return [[6, 7, -1, 7, 6], [6, -1, -1, -1, 7], [-1, -1, -1, -1, 10], [8, -1, -1, -1, 9], [8, 7, -1, 9, 9]]
    
    ...
    
    [the original solution here]
    

    之后我可以提交解决方案。希望有帮助

    【讨论】:

    • 认真的吗?测试用例是故意写错的,因为那是一个特例?我想我对这个挑战不够聪明。
    • 伙计,非常感谢,你让我开心。我认为他们是故意这样做的。答案就在盯着你,你只需要仔细阅读文档。不过,这很卑鄙。
    • 这不是故意的错误。他们更新了问题并忘记更改第二个测试用例。太蹩脚了,我什至做了一个小的可视化器和很多很多的测试,因为我担心我错过了一些极端情况。 Here你可以看到旧版本。
    【解决方案3】:

    根据提问:

    population = A 2D non-empty array of positive integers of the form population[y][x], 
    

    它是 (y,x) 的形式,而你以 x,y 的形式解决了它。

    这是程序的输出:

    9 3 4 5 4

    -1 6 5 4 3

    -1 -1 7 3 2

    -1 -1 -1 8 1

    -1 -1 -1 -1 9

    修改后:

    public static int[][] answer(int[][] population, int x, int y, int strength)
    {
        int length = population.length;
        if(y < 0 || y >= length)
            return population;
        int width = population[y].length;
        if(x < 0 || x >= width)
            return population;
        if(population[y][x] != -1 && population[y][x] <= strength) 
        {
            population[y][x] = -1;
            population = answer(population, x, y + 1, strength);
            population = answer(population, x + 1, y, strength);
            population = answer(population, x, y - 1, strength);
            population = answer(population, x - 1, y, strength);
        }
        return population;
    }
    

    输出:

    9 -1 -1 -1 -1

    1 6 -1 -1 -1

    2 3 7 -1 -1

    3 4 5 8 -1

    4 5 4 3 9

    附: : 在第二种情况下输出

    output:    (int) [[6, 7, -1, 7, 6], [6, -1, -1, -1, 7], [-1, -1, -1, -1, 10], [8, -1, -1, -1, 9], [8, 7, -1, 9, 9]]
    

    这没有意义,因为数组本身并不相同

    【讨论】:

    • 哦,是的,对[x][y] 开关感到抱歉。我忘了把它们换回来。即使使用上述解决方案,它仍然未能通过第二个测试用例,我不知道那个测试用例的全部内容。第二个输出显然是非常错误的。
    • 是的,在第二个测试用例中,没有值为 10 的单元格,但它神奇地出现在输出中。要么问题不完整,要么谷歌疯了..:p
    • 也许不是完全疯了,但只是一个糟糕的(非技术性的)文档审阅者。 =D
    猜你喜欢
    • 2020-05-19
    • 2015-08-11
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 2012-03-20
    • 2013-09-24
    • 1970-01-01
    相关资源
    最近更新 更多