按递增顺序计算矩阵中的个数

Posted

技术标签:

【中文标题】按递增顺序计算矩阵中的个数【英文标题】:Count number of ones in a matrix in increasing order 【发布时间】:2020-08-29 16:25:41 【问题描述】:

我有这个island program,它只显示矩阵中可能岛屿的数量。

我有一个由 1 和 0 组成的矩阵,我想在行、列和对角线上得到一组 1。该程序计算可能性的数量并返回 5 作为以下输入的输出。

  1, 0, 0, 1, 0 , 
  1, 0, 1, 0, 0 , 
  0, 0, 1, 0, 1 , 
  1, 0, 1, 0, 1  

但我需要输出为岛大小,按 1 2 2 4 递增的顺序排列

如何做到这一点?

解释:a) 最后一行第一列有一个岛,b) 第一行第一列,第二行第一列,并且有大小为 2 的岛。 c) 第三行最后一列,最后一行最后一列有大小为 2 的岛。 d) 第 1 行第 4 列,其余行第 3 列大小为 4。

public class Islands 
    // No of rows and columns
    static final int ROW = 4, COL = 5;

    // A function to check if a given cell (row, col) can
    // be included in DFS
    static boolean isSafe(int M[][], int row, int col, boolean visited[][]) 
        // row number is in range, column number is in range
        // and value is 1 and not yet visited
        return (row >= 0) && (row < ROW) && (col >= 0) && (col < COL) && (M[row][col] == 1 && !visited[row][col]);
    

    // A utility function to do DFS for a 2D boolean matrix.
    // It only considers the 8 neighbors as adjacent vertices
    static void DFS(int M[][], int row, int col, boolean visited[][]) 
        // These arrays are used to get row and column numbers
        // of 8 neighbors of a given cell
        int rowNbr[] = new int[]  -1, -1, -1, 0, 0, 1, 1, 1 ;
        int colNbr[] = new int[]  -1, 0, 1, -1, 1, -1, 0, 1 ;

        // Mark this cell as visited
        visited[row][col] = true;

        // Recur for all connected neighbours
        for (int k = 0; k < 8; ++k)
            if (isSafe(M, row + rowNbr[k], col + colNbr[k], visited))
                DFS(M, row + rowNbr[k], col + colNbr[k], visited);
    

    // The main function that returns count of islands in a given
    // boolean 2D matrix
    static int countIslands(int M[][]) 
        // Make a bool array to mark visited cells.
        // Initially all cells are unvisited
        boolean visited[][] = new boolean[ROW][COL];

        // Initialize count as 0 and travese through the all cells
        // of given matrix
        int count = 0;
        for (int i = 0; i < ROW; ++i)
            for (int j = 0; j < COL; ++j)
                if (M[i][j] == 1 && !visited[i][j]) // If a cell with
                 // value 1 is not
                    // visited yet, then new island found, Visit all
                    // cells in this island and increment island count
                    DFS(M, i, j, visited);
                    ++count;
                

        return count;
    

    // Driver method
    public static void main(String[] args) throws java.lang.Exception 
        int M[][] = new int[][]   1, 0, 0, 1, 0 ,  1, 0, 1, 0, 0 ,  0, 0, 1, 0, 1 ,  1, 0, 1, 0, 1  ;
        System.out.println("Number of islands is: " + countIslands(M));
    

【问题讨论】:

创建岛屿尺寸列表。把它分类。输出吧。 【参考方案1】:

进行以下更改以解决它:

添加静态变量size 以计算每个岛屿的大小。 将count 替换为listSizes 以存储每个岛的大小。新岛屿的大小已添加到此列表中。 调用方法(从countIslands重命名为sizeIslands)得到最终的listSizes。 在打印之前对列表进行排序。

最终代码:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Islands 
    // No of rows and columns
    static final int ROW = 4, COL = 5;
    static int size;

    // A function to check if a given cell (row, col) can
    // be included in DFS
    static boolean isSafe(int M[][], int row, int col, boolean visited[][]) 
        // row number is in range, column number is in range
        // and value is 1 and not yet visited
        return (row >= 0) && (row < ROW) && (col >= 0) && (col < COL) && (M[row][col] == 1 && !visited[row][col]);
    

    // A utility function to do DFS for a 2D boolean matrix.
    // It only considers the 8 neighbors as adjacent vertices
    static void DFS(int M[][], int row, int col, boolean visited[][]) 
        // These arrays are used to get row and column numbers
        // of 8 neighbors of a given cell
        int rowNbr[] = new int[]  -1, -1, -1, 0, 0, 1, 1, 1 ;
        int colNbr[] = new int[]  -1, 0, 1, -1, 1, -1, 0, 1 ;

        // Mark this cell as visited
        visited[row][col] = true;
        size++;

        // Recur for all connected neighbours
        for (int k = 0; k < 8; ++k)
            if (isSafe(M, row + rowNbr[k], col + colNbr[k], visited))
                DFS(M, row + rowNbr[k], col + colNbr[k], visited);
    
    
    // The main function that returns size of islands in a given
    // boolean 2D matrix
    static List<Integer> sizeIslands(int M[][]) 
        // Make a bool array to mark visited cells.
        // Initially all cells are unvisited
        boolean visited[][] = new boolean[ROW][COL];

        // Initialize empty list and travese through the all cells
        // of given matrix
        List<Integer> listSizes = new ArrayList<Integer>();
        for (int i = 0; i < ROW; ++i)
            for (int j = 0; j < COL; ++j)
                if (M[i][j] == 1 && !visited[i][j]) // If a cell with
                 // value 1 is not
                    // visited yet, then new island found, Visit all
                    // cells in this island and increment island count
                    size = 0;
                    DFS(M, i, j, visited);
                    listSizes.add(size);
                

        return listSizes;
    

    // Driver method
    public static void main(String[] args) throws java.lang.Exception 
        int M[][] = new int[][]   1, 0, 0, 1, 0 ,  1, 0, 1, 0, 0 ,  0, 0, 1, 0, 1 ,  1, 0, 1, 0, 1  ;
        List<Integer> list = sizeIslands(M);
        Collections.sort(list);
        System.out.println("Sizes of islands are: " + list);
    

【讨论】:

以上是关于按递增顺序计算矩阵中的个数的主要内容,如果未能解决你的问题,请参考以下文章

1050. 螺旋矩阵(25)

SQL按顺序计算个数

leetcode 950. 按递增顺序显示卡牌

数组元素个数计算

C语言编程问题:给数组中的元素按顺序编号

如何在 C++ 中按递增顺序绘制球体网格?