我目前正在制定一项计划,在读取尺寸ň * ñ的“地图”。这个“地图”是由字符.
和*
,其中.
代表水,并*
代表土地。该方案的一点是要计算有多少“岛”发生在地图,其中一个“岛”是土地(*
)任何部件(或多个)上,完全由水(.
)包围。如果两个地块(*
)在传统的八个方向中的任何一个相连,则它们被认为是一个岛。作为参考,我将在末尾放置一个示例输入和输出。递归时如何处理StackOverflow错误?
我的代码还包括解析匹配地图的2D char
数组,每遇到*
就增加int numOfIslands
。然后,它取代了*
与.
,并使用递归来查找和替换“孤岛”的其余部分,与穿越移动之前。它目前适用于较小的输入大小,例如包含的示例输入。但问题是,它需要能够对n = 1000
输入尺寸运行,目前我得到的StackOverflow错误,当我尝试在输入尺寸大的运行它。我应该怎么做来处理StackOverflow错误?我想这与在递归过程中破坏有关,为了“卸载”堆栈,但是我真的不知道如何开始这样做,而且我的网络研究一直没有成果。
我遍历和递归代码,其中map[][]
是2D char
阵列输入的文件相匹配:
//traverses the map, looking for 1s, and then sets that location to 0 before running testLocation on it
public static void traverse() {
for (int col = 0; col < n; col++) {
for (int row = 0; row < n; row++) {
if (map[row][col] == '*') {
map[row][col] = '.';
testLocation(row, col);
numOfIslands++;
}
}
}
}
//takes in a land location, and makes that land, along with the rest of the "island" 0s
public static void testLocation(int row, int col) {
//test upper left
if (row > 0 && col > 0) {
if (map[row - 1][col - 1] == '*') {
map[row - 1][col - 1] = '.';
testLocation(row - 1, col - 1);
}
}
//test upper
if (row > 0) {
if (map[row - 1][col] == '*') {
map[row - 1][col] = '.';
testLocation(row - 1, col);
}
}
//test upper right
if (row > 0 && col < n - 1) {
if (map[row - 1][col + 1] == '*') {
map[row - 1][col + 1] = '.';
testLocation(row - 1, col + 1);
}
}
//test left
if (col > 0) {
if (map[row][col - 1] == '*') {
map[row][col - 1] = '.';
testLocation(row, col - 1);
}
}
//test right
if (col < n - 1) {
if (map[row][col + 1] == '*') {
map[row][col + 1] = '.';
testLocation(row, col + 1);
}
}
//test lower left
if (row < n - 1 && col > 0) {
if (map[row + 1][col - 1] == '*') {
map[row + 1][col - 1] = '.';
testLocation(row + 1, col - 1);
}
}
//test lower
if (row < n - 1) {
if (map[row + 1][col] == '*') {
map[row + 1][col] = '.';
testLocation(row + 1, col);
}
}
//test lower right
if (row < n - 1 && col < n - 1) {
if (map[row + 1][col + 1] == '*') {
map[row + 1][col + 1] = '.';
testLocation(row + 1, col + 1);
}
}
}
样品输入:
...*.
**..*
*....
*.*.*
**...
示例输出:
The total number of islands is 3
如果你绝对想保留你的递归,除了增加stacksize之外,不需要做太多的事情。 否则,请考虑使用迭代算法而不是递归算法。 –
你可以使用不同的算法。 https://en.wikipedia.org/wiki/Connected-component_labeling通常使用,并且只使用两遍 –
我完全不理解算法,但我认为这有一个隐含的基本情况,而不是明确的。可能定义一个明确的基本案例来帮助这里削减一些角落? – hamena314