Problem-1241 Oil Deposits
HDUOJ入口:http://acm.hdu.edu.cn/showproblem.php?pid=1241
AC代码
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define mm(a) memset(a, 0, sizeof(a))
int dir[8][2] = {{1,0},{1,1},{1,-1},{0,-1},{0,1},{-1,0},{-1,1},{-1,-1}};
const int MAXN = 110;
char mp[MAXN][MAXN];
int row, col;
void DFS(int x, int y){
mp[x][y] = '*';
for (int k = 0; k < 8; k++){
int nx = x+dir[k][0];
int ny = y+dir[k][1];
if (nx>=0&&nx<row&&ny>=0&&ny<col&&mp[nx][ny]=='@')
DFS(nx, ny);
}
return;
}
int main(){
while (~scanf("%d%d", &row, &col)){
mm(mp);
if (!row && !col) break;
int cnt = 0;
for (int i = 0; i < row; i++) scanf("%s", mp[i]);
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
if (mp[i][j] == '@'){
DFS(i, j);
cnt++;
}
printf("%d\n", cnt);
}
return 0;
}