UVa 572
dfs寻找连通块,floodfill
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxm= 105;
const int maxn= 105;
char ld[maxm][maxn];
int k[maxm][maxn];
int m, n;
void dfs(int x, int y, int c)
{
if (x< 0 || y< 0 || x>= m || y>= n){
return;
}
if (k[x][y]> 0 || '@'!= ld[x][y]){
return;
}
k[x][y]= c;
for (int dx= -1; dx< 2; ++dx){
for (int dy= -1; dy< 2; ++dy){
if (dx || dy){
dfs(x+dx, y+dy, c);
}
}
}
}
int main()
{
while (1){
scanf("%d %d", &m, &n);
if (0== m){
break;
}
memset(ld, 0, sizeof(ld));
memset(k, -1, sizeof(k));
for (int i= 0; i< m; ++i){
for (int j= 0; j< n; ++j){
scanf(" %c", &(ld[i][j]));
}
}
int ans= 0;
for (int i= 0; i< m; ++i){
for (int j= 0; j< n; ++j){
if ('@'== ld[i][j] && k[i][j]<= 0){
dfs(i, j, ++ans);
}
}
}
printf("%d\n", ans);
}
return 0;
}