class Solution {
public:
bool st[310][310];
int dx[4]={0,0,-1,1},dy[4]={-1,1,0,0};
int m,n;
int numIslands(vector<vector<char>>& g) {
int res=0;
n=g.size(),m=g[0].size();
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(!st[i][j]&&g[i][j]=='1')
{
dfs(i,j,g);
res++;
}
}
return res;
}
void dfs(int x,int y,vector<vector<char>>& g)
{
st[x][y]=true;
for(int i=0;i<4;i++)
{
int a=x+dx[i],b=y+dy[i];
if(a>=0&&a<n&&b>=0&&b<m&&!st[a][b]&&g[a][b]=='1')
dfs(a,b,g);
}
}
};
标签:200,int,310,岛屿,st,&&,LeetCode
From: https://www.cnblogs.com/tangxibomb/p/17534624.html