gpt4 book ai didi

java - 二维阵列中所有岛之间的最大总和是多少?必须使用递归

转载 作者:行者123 更新时间:2023-12-04 11:43:24 25 4
gpt4 key购买 nike

假设我们有一个 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 错误,但它没有考虑向左和向上旅行:
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 .....??

最佳答案

最有效的方法是折叠数组但递归我想你想要

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++)
maxValue = Math.max(maxValue, getIslandValue(map, r, c));
return maxValue;
}

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;
int h = map[r][c];
map[r][c] = 0;
return h + getIslandValue(map, r, c + 1) + getIslandValue(map, r, c - 1)
+ getIslandValue(map, r + 1, c) + getIslandValue(map, r - 1, c);
}
在哪里运行
int[][] map = new int[][]{
{0, 0, 1, 0, 0},
{0, 1, 1, 0, 1},
{1, 0, 0, 1, 1},
{1, 1, 1, 1, 0},
{0, 0, 0, 0, 0}
};

System.out.println("There is an island with a value of " + maxValueIsland(map));
你得到
There is an island with a value of 8

关于java - 二维阵列中所有岛之间的最大总和是多少?必须使用递归,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69257812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com