有人知道如何移动“on the those”并将其更改为 2 吗?我认为我们需要递归,但我不太擅长,所以我向你寻求帮助。
您只能向上、向下、向左或向右移动。
这是一个例子:
int[][] a = {{ 0, 0, 0},
{1, 1, 0},
{1, 1, 1},
{1, 0, 1} };
recursiveFunction(a, 1, 1); // (<array you're checking>, <x of the group>, <y of the group>
/* int a now contains:
* { 0, 0, 0,
* 2, 2, 0,
* 2, 2, 2,
* 2, 0, 2 } */
int[][] b = {{0, 0, 0, 0, 1},
{1, 1, 0, 0, 1},
{1, 1, 1, 0, 1},
{1, 0, 1, 0, 1} };
recursiveFunction(b, 0, 5);
/* int b now contains:
* { 0, 0, 0, 0, 2
* 1, 1, 0, 0, 2
* 1, 1, 1, 0, 2
* 1, 0, 1, 0, 2 } */
最佳答案
您只定义了一维数组及其编号,因此您应该使用 int。
不要递归地执行此操作,而是尝试使用 2D 数组的两个 for 循环(1 个用于行,1 个用于列)轻松解决该问题,如下所示:
int array[][] = {{0, 0, 0},
{1, 1, 0},
{1, 1, 1},
{1, 0, 1}};
for (int i = 0; i< array.length; i++) {
for (int j = 0; j< array[0].length; j++) {
if (array[i][j] == 1) {
array[i][j] = 2;
}
}
}
关于java - 在数组中递归移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27581641/