public class Main {     private static int pNums = 0;     private static int qNums = 0;     private static int qTmpSum = 0;     public static void main(String[] args) {         Scanner scanner = new Scanner(System.in);         String[] line1 = scanner.nextLine().split(",");         int m = Integer.parseInt(line1[0]);         int n = Integer.parseInt(line1[1]);         int[][] grids = new int[m][n];         for (int i = 0; i < m; i++) {             String[] line2 = scanner.nextLine().split(",");             for (int j = 0; j < n; j++) {                 grids[i][j] = Integer.parseInt(line2[j]);             }         }         pNums = numIslands(grids);         System.out.println(pNums + "," + qNums);     }     public static int numIslands(int[][] grid) {         int m = grid.length;         int n = grid[0].length;         boolean[][] visited = new boolean[m][n];         int result = 0;         for (int i = 0; i < m; i++) {             for (int j = 0; j < n; j++) {                 if (grid[i][j] == 1 && !visited[i][j]) {                     qTmpSum = 0;                     numIslandsDFS(grid, visited, i, j);                     result++;                 }             }         }         return result;     }     private static void numIslandsDFS(int[][] grid, boolean[][] visited, int x, int y) {         if (x < 0 || x >= grid.length)             return;         if (y < 0 || y >= grid[x].length)             return;         if (grid[x][y] != 1 || visited[x][y])             return;         visited[x][y] = true;         qTmpSum++;         if (qNums < qTmpSum)             qNums = qTmpSum;         numIslandsDFS(grid, visited, x - 1, y);         numIslandsDFS(grid, visited, x - 1, y + 1);         numIslandsDFS(grid, visited, x, y + 1);         numIslandsDFS(grid, visited, x + 1, y + 1);         numIslandsDFS(grid, visited, x + 1, y);         numIslandsDFS(grid, visited, x + 1, y - 1);         numIslandsDFS(grid, visited, x, y - 1);         numIslandsDFS(grid, visited, x - 1, y - 1);     } }
点赞 评论

相关推荐

11-15 19:28
已编辑
蚌埠坦克学院 硬件开发
点赞 评论 收藏
分享
努力学习的小绵羊:我反倒觉得这种挺好的,给不到我想要的就别浪费大家时间了
点赞 评论 收藏
分享
牛客网
牛客企业服务