【发布时间】: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