【发布时间】:2021-11-14 08:40:49
【问题描述】:
假设我们有一个N x M 网格,其中网格的每个单元格都包含一个值为 0 或正整数的值。孤岛是一组在正交方向(北、东、南和西,但不是对角线)上被 0 包围的值。问题是确定网格中所有岛屿之间的最大和。我们得到了名为maxValueIsland 的方法,它采用二维整数数组并通过网格扫描岛屿。一旦它找到一个岛,它就会调用我们必须实现的方法 getIslandValue。然后maxValueIsland 方法返回所有岛值中的最大值。
我们提供了几种实用方法来帮助生成和显示 2D 网格。
public static void main(String[] args) {
int map[][] = new int[5][5];
int maxValue = 100;
int dropChance = 25;
randomIslands(map, maxValue, dropChance);
printIslands(map);
System.out.println("There is an island with a value of " + maxValueIsland(map));
}
/********** Student Code Here **************************/
public static int maxValueIsland(int map[][]) {
int maxValue = 0;
for (int r = 0; r < map.length; r++) {
for (int c = 0; c < map[r].length; c++) {
if (map[r][c] != 0) {
int value = getIslandValue(map, r, c);
if (value > maxValue) {
maxValue = value;
}
}
}
}
return maxValue;
}
private static int getIslandValue(int map[][], int r, int c) {
//HERE IS THE METHOD WE MUST IMPLEMENT
}
/******************************************************************/
public static void randomIslands(int map[][], int maxPossibleValue, int chance) {
if (maxPossibleValue <= 0) {
throw new IllegalArgumentException("The max possible value must be a positive integer.");
}
if (chance > 100 || chance < 0) {
throw new IllegalArgumentException("The chance of money drop must be between 0 <= p <= 100");
}
for (int r = 0; r < map.length; r++) {
for (int c = 0; c < map[r].length; c++) {
int possible = (int) (Math.random() * 100) + 1;
if (possible <= chance) {
map[r][c] = (int) (Math.random() * maxPossibleValue) + 1;
}
}
}
}
public static void printIslands(int island[][]) {
int maxDigits = getMaxDigits(island);
for (int r = 0; r < island.length; r++) {
for (int c = 0; c < island[r].length; c++) {
int value = island[r][c];
String s = "%" + maxDigits + "d";
if (value != 0) {
System.out.print(" |");
System.out.printf(s, value);
System.out.print("| ");
} else {
System.out.print(" ");
System.out.printf("%" + maxDigits + "s", "-");
System.out.print(" ");
}
}
System.out.println(" ");
}
}
private static int getMaxDigits(int[][] arr) {
int maxDigitSize = 0;
for (int r = 0; r < arr.length; r++) {
for (int c = 0; c < arr[r].length; c++) {
int value = arr[r][c];
int digits = 0;
while (value != 0) {
digits += 1;
value /= 10;
}
if (digits > maxDigitSize) {
maxDigitSize = digits;
}
}
}
return maxDigitSize;
}
我已经尝试了几次迭代,我知道我的基本情况是准确的。我在递归调用时遇到了麻烦。我需要考虑网格中不同方向的行驶。 (右、左、上和下)。这是我尝试过的:
private static int getIslandValue(int map[][], int r, int c) {
if (r < 0 || c < 0 || r >= map.length || c >= map[r].length || map[r][c] == 0) {
return 0; // Base Case
}
int right = getIslandValue(map, r, c + 1);
int down = getIslandValue(map, r + 1, c);
int left = getIslandValue(map, r, c - 1);
int up = getIslandValue(map, r - 1, c);
return map[r][c] + (right + left + up + down);
}
我收到 stackoverflow 错误。我尝试了许多不同的迭代。我最初的尝试之一没有产生 stackoverflow 错误,但它没有考虑到向左和向上移动:
private static int getIslandValue(int map[][], int r, int c) {
if (r < 0 || c < 0 || r >= map.length || c >= map[r].length || map[r][c] == 0) {
return 0; // Base Case
}
int right = getIslandValue(map, r, c + 1);
int down = getIslandValue(map, r + 1, c);
return map[r][c] + Math.max(right, down);
}
最后,我知道我必须在访问完一个岛屿后留下 0 的面包屑。我必须包含map[r][c] = 0; 才能完成此操作。但这不是在我的基本案例结束时已经考虑到了吗? map[r][c] == 0.....??
【问题讨论】:
-
请提供输入实例(输出更好)
标签: java recursion multidimensional-array