Dfs做法
#include <bits/stdc++.h>
using namespace std;
int ans, n, m;
char a[1000][1000];
void dfs(int x, int y, char c) {
if (a[x][y] != c || x < 0 || y < 0 || x >= n || y >= m)
return;
a[x][y] = '0';
dfs(x + 1, y, c);
dfs(x - 1, y, c);
dfs(x, y + 1, c);
dfs(x, y - 1, c);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) scanf("%s", &a[i]);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] > '0' && a[i][j] <= '9') {
ans++;
dfs(i, j, a[i][j]);
}
}
}
printf("%d", ans);
return 0;
}