求联通快模板
Uva 油田
#include<cstdio>
#include<iostream>
#include<string.h>
using namespace std;
const int maxn = 1000 + 5;
char g[maxn][maxn];
int m, n, id[maxn][maxn];
void dfs(int row, int col, int c) {
if (row < 0 || row >= m || col < 0 || col >= n) {
return;
}
if (id[row][col] > 0 || g[row][col] != '@') {
return;
}
id[row][col] = c;
for (int i = -1; i<2; i++)
for (int j = -1; j<2; j++)
if (i != 0 || j != 0) dfs(row + i, col + j, c);
}
int main() {
scanf("%d %d", &m, &n);
for (int i = 0; i < m; i++) {
scanf("%s", g[i]);
}
memset(id, 0, sizeof(id));
int cnt = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (id[i][j] == 0 && g[i][j] == '@')
dfs(i, j, ++cnt);
}
}
printf("%d\n", cnt);
return 0;
}