LC695
作者:互联网
//需要自己指定开始位置(循环寻找)
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
int vis[55][55];
int bfs(vector<vector<int>>& grid,int x,int y){
int res=1;
queue<pair<int,int> > q;
q.push({x,y});
vis[x][y]=1;
int n=grid.size();
int m=grid[0].size();
while(!q.empty()){
int x1=q.front().first;
int y1=q.front().second;
q.pop();
for(int i=0;i<4;i++){
int nx=x1+dx[i];
int ny=y1+dy[i];
if(nx>=0&&ny>=0&&nx<n&&ny<m&&grid[nx][ny]&&!vis[nx][ny]){
res++;
vis[nx][ny]=1;
q.push({nx,ny});
}
}
}
// cout<<res<<endl;
return res;
}
int maxAreaOfIsland(vector<vector<int>>& grid) {
int ans=0;
memset(vis,0,sizeof(1));
int n=grid.size();
int m=grid[0].size();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(grid[i][j]&&!vis[i][j]){
int res=bfs(grid,i,j);
ans=max(ans,res);
}
}
}
return ans;
}
标签:LC695,int,vis,grid,&&,front,size 来源: https://www.cnblogs.com/lwx11111/p/16659028.html