岛屿的最大面积
[[0,0,1,0,0,0,0,1,0,0,0,0,0], [0,0,0,0,0,0,0,1,1,1,0,0,0], [0,1,1,0,1,0,0,0,0,0,0,0,0], [0,1,0,0,1,1,0,0,1,0,1,0,0], [0,1,0,0,1,1,0,0,1,1,1,0,0], [0,0,0,0,0,0,0,0,0,0,1,0,0], [0,0,0,0,0,0,0,1,1,1,0,0,0], [0,0,0,0,0,0,0,1,1,0,0,0,0]]
public int maxAreaOfIsland(int[][] grid){ if(grid==null || grid.length==0){ return 0; } int maxArea = 0; for(int i=0;i<grid.length;i++){ for(int j=0;j<grid[0].length;j++){ maxArea = Math.max(maxArea, dfs(grid,i,j)); } } return maxArea; } private int dfs(int[][] grid,int x,int y){ if(x<0||x>=grid.length||y<0||y>=grid[0].length||grid[x][y]==0){ return 0; } grid[x][y] = 0; int area = 1; int[][] direction = {{0,1},{0,-1},{1,0},{-1,0}}; for(int[] d:direction){ area += dfs(grid, x+d[0], y+d[1]); } return area; }