岛屿数量
class Solution {
int[] dx = {0, 1, 0, -1};
int[] dy = {1, 0, -1, 0};
public int numIslands(char[][] grid) {
int num = 0;
if (grid == null) return num;
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
if (grid[i][j] == '1') {
num++;
dfs(grid, i, j);
}
}
}
return num;
}
private void dfs(char[][] grid, int i, int j) {
grid[i][j] = '2';
for (int k = 0; k < 4; k++) {
if (i + dx[k] >= 0 && i + dx[k] < grid.length && j + dy[k] >= 0 && j + dy[k] < grid[0].length && grid[i + dx[k]][j + dy[k]] == '1') {
dfs(grid, i + dx[k], j + dy[k]);
}
}
}
}