import java.util.*;
public class Solution {
private int[][] direction = new int[][] {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
public void searchData(int x,int y,char[][] grid,int[][] bid) {
bid[x][y] = 1;
for (int i=0;i<4;i++) {
int nx = x + direction[i][0];
int ny = y + direction[i][1];
if ( nx >= 0 && ny>=0 && nx < grid.length && ny < grid[0].length && grid[nx][ny] == '1' && bid[nx][ny] != 1) {
searchData(nx,ny,grid,bid);
}
}
}
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* 判断岛屿数量
* @param grid char字符型二维数组
* @return int整型
*/
public int solve (char[][] grid) {
if (grid.length == 0) {
return 0;
}
int x = grid.length;
int y = grid[0].length;
int[][] bid = new int[x][y];
int total = 0;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (bid[i][j] == 1) {
continue;
}
if (grid[i][j] == '1') {
total++;
searchData(i, j, grid, bid);
}
}
}
return total;
}
}