java - foob​​ar 僵尸感染挑战

标签 java arrays recursion

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

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

僵尸感染

博士。 boolean 继续对你的兔子同胞进行恶魔般的研究,而且并非所有研究都在实验室进行。报道称,这位疯狂的医生打算用一种能将兔子变成僵尸(zombie-rabbits)的病毒感染本地村庄的一只兔子!

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

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

您将获得以下信息:

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 级。不要听起来傲慢,但最终,我只是停止了挑战,因为老实说,这是在浪费我的时间。验证和提交我的解决方案花了很长时间,重试了很多次,因为系统经常超时。即使通过了所有 5 个测试用例,我的 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]

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

关于java - foob​​ar 僵尸感染挑战,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38006104/

相关文章:

javascript 复杂递归

java - spring - CrudRepository 和带有 transient 字段的 beans

java - 在 Linux 中使用 echo 使用 Runtime.exec 创建文件?

c++ - 基类对象/子对象/等的数组/vector

c# - 如何在C#中实现数组索引器

Python 函数返回循环

java - 将头节点移动到链表末尾

Java:无法覆盖 postVisitDirectory

java - 为什么我的简单数组不起作用?

c - 使用分治法的矩阵乘法