class Solution {
int ret,m,n,count;
boolean[][] vis;
public int maxAreaOfIsland(int[][] grid) {
m = grid.length;
n = grid[0].length;
vis = new boolean[m][n];
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++){
if(!vis[i][j] && grid[i][j] == 1){
count = 0;
dfs(grid,i,j);
ret = Math.max(ret,count);
}
}
return ret;
}
int[] dx = {0,0,1,-1};
int[] dy = {-1,1,0,0};
private void dfs(int[][] grid, int i, int j){
count++;
vis[i][j] = true;//使用过就标记
for(int k = 0; k < 4; k++){
int x = i + dx[k]; int y = j + dy[k];
if(x >= 0 && x < m && y >= 0 && y < n && !vis[x][y] && grid[x][y] == 1){
dfs(grid,x,y);
}
}
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有