hdu1241
dfs(31ms)
/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>
typedef long long LL;
using namespace std;
int n, m;
int dist[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 1, -1, -1, -1};
char s[105][105];
void dfs(int x, int y){
for (int i = 0; i < 8; i++){
int xx = x + dist[i][0], yy = y + dist[i][1];
if(x < 1 || x > n || y < 1 || y > m) continue;
if(s[xx][yy] == '@'){
s[xx][yy] = '*';
dfs(xx, yy);
}
}
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while(scanf("%d %d", &n, &m) == 2){
if(!n && !m) break;
for (int i = 1; i <= n; i++){
scanf("%s", s[i] + 1);
}
int ans = 0;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
if(s[i][j] == '@'){
ans++;
s[i][j] = '*';
dfs(i, j);
}
}
}
printf("%d\n", ans);
}
return 0;
}
/**/
并查集(15ms)
/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>
typedef long long LL;
using namespace std;
int n, m;
int f[10005];
char s[105][105];
int dist[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};
int Find(int x){
return x == f[x] ? x : Find(f[x]);
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
while(scanf("%d %d", &n, &m) == 2){
if(!n && !m) break;
for (int i = 1; i <= n * m; i++){
f[i] = i;
}
for (int i = 1; i <= n; i++){
scanf("%s", s[i] + 1);
}
set<int>st;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
if(s[i][j] == '@'){
int X = Find((i - 1) * m + j);
for (int k = 0; k < 8; k++){
int x = i + dist[k][0], y = j + dist[k][1];
if(x < 1 || x > n || y < 1 || y > m) continue;
if(s[x][y] == '@'){
int Y = Find((x - 1) * m + y);
if(X != Y){
f[Y] = X;
}
}
}
}
}
}
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
if(s[i][j] == '@'){
f[(i - 1) * m + j] = Find((i - 1) * m + j);
st.insert(f[(i - 1) * m + j]);
}
}
}
printf("%d\n", st.size());
}
return 0;
}