content: import java.util.Collections;
  import java.util.LinkedList;
  import java.util.List;
  import java.util.Queue;
  import java.util.concurrent.LinkedBlockingQueue;
  
  /**
   * @author jzy
   * @GitHub <a href="https://github.com/567jin">567jin</a>
   * @since 2024-03-30 20:05:05
   */
  public class _200_NumIslands {
      public static void main(String[] args) {
          char[][] grid = {{'1', '1', '1', '1', '0'},
                  {'1', '1', '0', '1', '0'},
                  {'1', '1', '0', '0', '0'},
                  {'0', '0', '0', '0', '0'}};
          int i = new _200_NumIslands().numIslands(grid);
          System.out.println(i);
      }
  
  
      public int numIslands(char[][] grid) {
          int ans = 0;
          int row = grid.length, col = grid[0].length;
          for (int i = 0; i < row; i++) {
              for (int j = 0; j < col; j++) {
                  if (grid[i][j] == '1') {
                      ans++;
                      dfs(grid, i, j);
                  }
              }
          }
          return ans;
      }
  
      private void dfs(char[][] grid, int row, int col) {
          if (row < 0 || col < 0 || row >= grid.length || col >= grid[0].length || grid[row][col] != '1') {
              return;
          }
          // 标记所有与这个1相连的陆地 这几个是一个整体
          grid[row][col] = '#';
          dfs(grid, row + 1, col);
          dfs(grid, row, col + 1);
          dfs(grid, row - 1, col);
          dfs(grid, row, col - 1);
      }
  
      private void bfs(char[][] grid, int row, int col) {
          Queue<int[]> queue = new LinkedList<>();
          queue.add(new int[]{row, col});
          while (!queue.isEmpty()) {
              int[] cur = queue.poll();
              int i = cur[0], j = cur[1];
              if (i < 0 || j < 0 || i >= grid.length || j >= grid[0].length || grid[i][j] != '1') {
                  continue;  // 当没搜到时 不应该return 而是下一个接着搜 直到队列为空
              }
              grid[i][j] = '#';
              queue.add(new int[]{i + 1, j});
              queue.add(new int[]{i - 1, j});
              queue.add(new int[]{i, j + 1});
              queue.add(new int[]{i, j - 1});
          }
  
      }
  
  
  }
  