char s[1005][85] = { 0 };
bool visit[1005][85] = { false };
int w, h;
void dfs(int x, int y,int& count) {
if (x > h || x<1 || y>w || y < 1)
return;
if (s[x][y] == ‘*’&&!visit[x][y]) {
visit[x][y] = true;
count++;
dfs(x + 1, y, count);
dfs(x - 1, y, count);
dfs(x, y + 1, count);
dfs(x, y - 1, count);
}
}
int main() {
cin >> w >> h;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> s[i][j];
}
}
int max = 0;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
if (s[i][j] == ‘*’ && !visit[i][j]) {
int count = 0;
dfs(i, j, count);
if (count > max)
max = count;
}
}
}
cout << max << endl;
return 0;
}
这个解法最简洁