int visit[300][300];
void dfs(char** grid,int m,int n,int i,int j){
if(i>=m || j>=n) return;
visit[i][j]=1;
if( i+1<m && grid[i+1][j]=='1' && visit[i+1][j]==0 ){
dfs(grid,m,n,i+1,j);
}
if( j+1<n && grid[i][j+1] =='1' && visit[i][j+1] == 0){
dfs(grid,m,n,i,j+1);
}
if( j-1 >=0 && grid[i][j-1]=='1' && visit[i][j-1] ==0 ) dfs(grid,m,n,i,j-1);
if(i-1 >=0 && grid[i-1][j]=='1' && visit[i-1][j] ==0 ) dfs(grid,m,n,i-1,j);
}
int numIslands(char** grid, int gridSize, int* gridColSize) {
int m=gridSize,n=gridColSize[0];
for(int i=0;i<300;i++){
for(int j=0;j<300;j++) visit[i][j]=0;
}
int count=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j]=='1'&& visit[i][j]==0){
dfs(grid,m,n,i,j);
count++;
}
}
}
return count;
}
标签:200,int,visit,岛屿,dfs,grid,&&,数量 From: https://www.cnblogs.com/llllmz/p/18085504