猿问

Java 中自调用函数中的堆栈溢出错误(岛数)

我对导致堆栈溢出错误的原因进行了一些研究,我可以得出结论,它是由程序中的递归函数引起的,该函数应该“计算数组中的岛数”。我明白是什么导致了这个问题,但不确定为什么会发生这种情况,或者我的主要问题是实际该怎么做。我发现如果我通过让程序反复向控制台打印一些东西来减慢程序的速度,它可以工作,但需要很长时间才能完成。有没有办法可以保持程序速度而不会出错,或者有更好的方法来解决问题(搜索“岛数”以找到问题)。此外,该数组是二维的,大小为 1050 x 800。


public class NumOfIslands {

  static boolean[][] dotMap = new boolean[1050][800];

  static boolean visited[][] = new boolean[1050][800];

  static int total = 0;

  public static void main(String args[]) {

    defineArrays();

    run();

  }

  public static void findObjects(int xCord, int yCord) {

    for(int y = yCord - 1; y <= yCord + 1; y++) {

      for(int x = xCord - 1; x <= xCord + 1; x++) {

        if(x > -1 && y > -1 && x < dotMap[0].length && y < dotMap.length) {

          if((x != xCord || y != yCord) && dotMap[x][y] == true && visited[x][y] != true) {

            visited[x][y] = true;

            findObjects(x,y);

            //System.out.println("test");

          }

        }

      }

    }

  }

  public static void defineArrays() {

    for(int y = 0; y < 800; y++) {

      for(int x = 0; x < 1050; x++) {

        dotMap[x][y] = true;

      }

    }

  }


  public static int run() {

    //dotMap = DisplayImage.isYellow;

    System.out.println(dotMap.length + " " + dotMap[0].length);

    int objects = 0;

    for(int y = 439; y < 560/*dotMap[0].length*/; y++) {

      for(int x = 70; x < 300/*dotMap.length*/; x++) {

        if(dotMap[x][y] == true && visited[x][y] != true) {

          visited[x][y] = true;

          objects++;

          findObjects(x,y);

        }

      }

    }

    System.out.println("total" + total);

    System.out.println(objects);

    return objects;


  }

}


泛舟湖上清波郎朗
浏览 129回答 2
2回答

郎朗坤

在您的示例中,每次调用都会findObjects从循环中将 2 个变量添加到堆栈 int x 和 int y。最快的解决方案之一:class Solution {&nbsp; &nbsp; int m, n;&nbsp; &nbsp; public int numIslands(char[][] grid) {&nbsp; &nbsp; &nbsp; &nbsp; if (grid == null || grid.length == 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; m = grid.length;&nbsp; &nbsp; &nbsp; &nbsp; n = grid[0].length;&nbsp; &nbsp; &nbsp; &nbsp; int counter = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < m; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < n; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (grid[i][j] == '1') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; visit(grid, i, j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return counter;&nbsp; &nbsp; }&nbsp; &nbsp; public void visit(char[][] grid, int i, int j) {&nbsp; &nbsp; &nbsp; &nbsp; if (i < 0 || i >= m || j < 0 || j >= n) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (grid[i][j] == '0') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; grid[i][j] = '0';&nbsp; &nbsp; &nbsp; &nbsp; visit(grid, i - 1, j);&nbsp; &nbsp; &nbsp; &nbsp; visit(grid, i + 1, j);&nbsp; &nbsp; &nbsp; &nbsp; visit(grid, i, j - 1);&nbsp; &nbsp; &nbsp; &nbsp; visit(grid, i, j + 1);&nbsp; &nbsp; }}所有递归算法都可以用循环来实现。示例之一如下。该解决方案实现了 BFS(广度优先搜索)算法,更多详情请参见维基百科。class Solution {&nbsp; public int numIslands(char[][] grid) {&nbsp; &nbsp; if (grid == null || grid.length == 0) {&nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; }&nbsp; &nbsp; int nr = grid.length;&nbsp; &nbsp; int nc = grid[0].length;&nbsp; &nbsp; int num_islands = 0;&nbsp; &nbsp; for (int r = 0; r < nr; ++r) {&nbsp; &nbsp; &nbsp; for (int c = 0; c < nc; ++c) {&nbsp; &nbsp; &nbsp; &nbsp; if (grid[r][c] == '1') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ++num_islands;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grid[r][c] = '0'; // mark as visited&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Queue<Integer> neighbors = new LinkedList<>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; neighbors.add(r * nc + c);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (!neighbors.isEmpty()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int id = neighbors.remove();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int row = id / nc;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int col = id % nc;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (row - 1 >= 0 && grid[row-1][col] == '1') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; neighbors.add((row-1) * nc + col);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grid[row-1][col] = '0';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (row + 1 < nr && grid[row+1][col] == '1') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; neighbors.add((row+1) * nc + col);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grid[row+1][col] = '0';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (col - 1 >= 0 && grid[row][col-1] == '1') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; neighbors.add(row * nc + col-1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grid[row][col-1] = '0';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (col + 1 < nc && grid[row][col+1] == '1') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; neighbors.add(row * nc + col+1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grid[row][col+1] = '0';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return num_islands;&nbsp; }}

温温酱

问题出在这个函数上public static void findObjects(int xCord, int yCord) {&nbsp; &nbsp; &nbsp; &nbsp; for(int y = yCord - 1; y <= yCord + 1; y++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int x = xCord - 1; x <= xCord + 1; x++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(x > -1 && y > -1 && x < dotMap[0].length && y < dotMap.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if((x != xCord || y != yCord) && dotMap[x][y] == true && visited[x][y] != true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; visited[x][y] = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;findObjects(x,y);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //System.out.println("test");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }`在这里,您正在构建对findobjects的递归调用堆栈,并且最终它没有终止条件,因此最终以无限堆栈findobjects 结束,所以我的解决方案是,如果您只是检查 x 和 y 变量是否不相等并已访问[ x][y] 不为真,则无需调用递归,只需注释递归调用,因为您的循环已经执行了您希望递归调用执行的操作。&nbsp;public static void findObjects(int xCord, int yCord) {&nbsp; &nbsp; &nbsp; &nbsp; for(int y = yCord - 1; y <= yCord + 1; y++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int x = xCord - 1; x <= xCord + 1; x++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(x > -1 && y > -1 && x < dotMap[0].length && y < dotMap.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if((x != xCord || y != yCord) && dotMap[x][y] == true && visited[x][y] != true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; visited[x][y] = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //findObjects(x,y);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //System.out.println("test");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }
随时随地看视频慕课网APP

相关分类

Java
我要回答