import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 判断岛屿数量 * @param grid char字符型二维数组 * @return int整型 */ int m,n; int[] dx = new int[]{0,0,-1,1}; int[] dy = new int[]{-1,1,0,0}; boolean[][] vis = new boolean[210][210]; Queue<int[]> queue = new LinkedList<>(); public void bfs(char[][] grid ){ while(!queue.isEmpty()){ int[] a = queue.poll(); int i1 = a[0],j1 = a[1]; if(!vis[i1][j1]){ vis[i1][j1] = true; } for(int k=0;k<4;k++){ int x = a[0]+dx[k],y = a[1]+dy[k]; if(x>=0 && x<m && y>=0 && y<n && grid[x][y] == '1' && !vis[x][y]){ vis[x][y] = true; queue.offer(new int[]{x,y}); } } } } public int solve (char[][] grid) { // write code here m = grid.length; n = grid[0].length; int ret=0; for(int i =0;i<m;i++){ for(int j =0;j<n;j++){ if(grid[i][j]=='1'&&!vis[i][j]){ queue.offer(new int[]{i,j}); bfs(grid); ret++; } } } return ret; } }