Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 给定矩阵中最大孤岛的算法_Java_Algorithm_Data Structures_Depth First Search - Fatal编程技术网

Java 给定矩阵中最大孤岛的算法

Java 给定矩阵中最大孤岛的算法,java,algorithm,data-structures,depth-first-search,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

给定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;
    }

}
公共类IslandConnectedCell{
公共静态void main(字符串[]args){
int[][]输入={
{1,0,0,0,1},
{1,1,1,1,1},
{0,0,0,0,0},
{1,1,0,1,1}
};
岛屿(输入);
}
公共静态岛(int[][]输入){
int rows=input.length;
int cols=输入[0]。长度;
List countList=new ArrayList();
访问的布尔值[][]=新的布尔值[行][cols];
对于(int row=0;row=行| |行<0 | |列>=列| |列<0){
返回0;
}
如果(输入[行][col]==0 | |访问[行][col]){
返回0;
}
访问[行][列]=真;
计数+=1;

对于(inti=row-1;i,代码中有两个错误

有关第一个错误,请参阅:


dfsiland()
中的一个明显问题是,至少
for(int col=0;col
应该是
for(int col=0;col
(对于行/列索引,使用公共
i
j
可能更好)

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

修复错误后,输出将为:

[7,2,2]



代码中有两个错误

有关第一个错误,请参阅:


dfsiland()
中的一个明显问题是,至少
for(int col=0;col
应该是
for(int col=0;col
(对于行/列索引,使用公共
i
j
可能更好)

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

修复错误后,输出将为:

[7,2,2]



为什么示例的预期输出是
[4,7]
?dfsiland()
中的一个明显问题是,(int-col=0;col可能应该是(int-col=0;col(对于行/列索引,最好使用公共的
i
j
)。该代码似乎定义了一个“孤岛”作为相邻的1,在这种情况下,我看到一个岛的大小为7,两个岛的大小为2。--或者这应该是一个世界吗?如果是,为什么我在代码中看不到任何概括逻辑?是的,你是对的。我编辑了输入矩阵。我在复制和粘贴时有一个打字错误,现在应该是5和7?为什么你的示例的预期输出<代码>[4,7]
?dfsiland()
中的一个明显问题是,
for(int col=0;col
可能应该是
for(int col=0;col
(对于行/列索引,最好使用公共的
i
j
),代码似乎定义了一个“孤岛”作为相邻的1,在这种情况下,我看到一个岛的大小为7,两个岛的大小为2。--或者这应该是一个世界吗?如果是这样,为什么我看不到代码中的任何概括逻辑?是的,你是对的。我编辑了输入矩阵。我在复制和粘贴时有一个打字错误,现在应该是5和7?很好的解决方案。谢谢你在传递时发现了错误按价值。简洁的解决方案。感谢您抓住了按价值传递的错误。
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;
    }

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