题目简单,直接贴代码
/*
* hdu1241/linux.cpp
* Created on: 2011-9-4
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN = 105;
char map[MAXN][MAXN];
int types[MAXN][MAXN], curtype;
const int move[8][2] = { { 1, 0 }, { 1, 1 }, { 0, 1 }, { -1, 1 }, { -1, 0 }, {
-1, -1 }, { 0, -1 }, { 1, -1 } };
int M, N;
void dfs(int x, int y) {
if (map[x][y] != '@' || types[x][y] > 0) {
return;
}
types[x][y] = curtype;
for (int i = 0; i < 8; i++) {
dfs(x + move[i][0], y + move[i][1]);
}
}
void work() {
while (scanf("%d%d", &M, &N) == 2) {
if (M == 0) {
break;
}
memset(map, '*', sizeof(map));
for (int i = 1; i <= M; i++) {
getchar();
for (int j = 1; j <= N; j++) {
map[i][j] = getchar();
}
}
memset(types, 0, sizeof(types));
curtype = 0;
for (int i = 1; i <= M; i++) {
for (int j = 1; j <= N; j++) {
if (map[i][j] == '@' && types[i][j] == 0) {
curtype++;
dfs(i, j);
}
}
}
printf("%d\n", curtype);
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
work();
return 0;
}