给定矩阵中最大岛的算法

时间:2018-07-13 22:03:36

标签: java algorithm data-structures depth-first-search

给出2 X 2矩阵,返回可能的不同岛大小

例如,以下矩阵应返回[5, 7]

  1 0 0 0 1
  1 1 1 1 1
  0 0 0 0 0
  1 1 1 1 1

这是一个非常简单的问题。我正在使用相同大小的布尔访问矩阵,并以DFS方式遍历该矩阵。我已经在这里实现了。由于某种原因,我得到的输出为[1]。我尝试调试,但现在我的头脑停止了工作。我想念一些愚蠢的东西。

public class IslandConnectedCell {

    public static void main(String[] args) {

        int[][] input = {
                {1,0,0,0,1},
                {1,1,1,1,1},
                {0,0,0,0,0},
                {1,1,0,1,1}
        };

        dfsIsland(input);

    }


    public static void dfsIsland(int[][] input) {
        int rows = input.length;
        int cols = input[0].length;
        List<Integer> countList = new ArrayList<>();

        boolean visited[][] = new boolean[rows][cols];

        for (int row = 0; row < rows; row++) {
            for (int col = 0; col < cols; cols++) {
                if (input[row][col] == 1 && !visited[row][col]) {
                    int count = mark(row, col, input, visited, rows, cols, 0);
                    countList.add(count);
                }
            }
        }
        System.out.println(countList);

    }

    public static int mark(int row, int col, int[][] input, boolean[][] visited, int rows, int cols, int count) {

        if (row >= rows || row < 0 || col >= cols || col < 0) {
            return 0;
        }

        if (input[row][col] == 0 || visited[row][col]) {
            return 0;
        }

        visited[row][col] = true;
        count+=1;

        for (int i = row - 1; i <= row + 1; i++) {
            for (int j = col - 1; j <= col + 1; j++) {
                if (i != row || j != col) {
                    mark(i, j, input, visited, rows, cols, count);
                }
            }
        }
        return count;
    }

}

1 个答案:

答案 0 :(得分:5)

您的代码中有两个错误。

有关第一个错误,请参见comment by Mick

  

dfsIsland()中一个明显的问题是,至少for (int col = 0; col < cols; cols++)应该改为for (int col = 0; col < cols; col++)(使用通用的ij可能更好。行/列索引)。

第二个错误是您在count方法中使用mark,最明显的是在递归调用中缺少使用返回值。请记住,Java是按值传递。
提示:我建议您删除count作为参数。

修复错误后,输出将为:

[7, 2, 2]


public class IslandConnectedCell {

    public static void main(String... args) {
        int[][] board = { {1,0,0,0,1},
                          {1,1,1,1,1},
                          {0,0,0,0,0},
                          {1,1,0,1,1} };
        System.out.println(new IslandConnectedCell(board).getIslandSizes());
    }

    private final int[][] board;
    private final int rows;
    private final int cols;

    public IslandConnectedCell(int[][] board) {
        this.board = board;
        this.rows = board.length;
        this.cols = board[0].length;
    }

    public List<Integer> getIslandSizes() {
        boolean visited[][] = new boolean[this.rows][this.cols];
        List<Integer> countList = new ArrayList<>();
        for (int row = 0; row < this.rows; row++)
            for (int col = 0; col < this.cols; col++)
                if (this.board[row][col] == 1 && ! visited[row][col])
                    countList.add(mark(row, col, visited));
        return countList;
    }

    private int mark(int row, int col, boolean[][] visited) {
        if (row >= this.rows || row < 0 || col >= this.cols || col < 0 || this.board[row][col] == 0 || visited[row][col])
            return 0;
        visited[row][col] = true;
        int count = 1;
        for (int r = -1; r <= 1; r++)
            for (int c = -1; c <= 1; c++)
                if (r != 0 || c != 0)
                    count += mark(row + r, col + c, visited);
        return count;
    }

}

更新

要获得所需的[7, 4]original question)输出,开发板将需要使用水平环绕,因此底线上的两个小岛变成了一个大岛。

通过使用%模运算符修改一行代码以环绕列索引很容易实现:

count += mark(row + r, (col + c + this.cols) % this.cols, visited);