基本的DFS,用于练手
#include <stdio.h>
char arr[110][110];
int max_row;
int max_col;
void dfs(int cur_row, int cur_col){
if(cur_row<1 || cur_row>max_row)
return;
if(cur_col<1 || cur_col>max_col)
return;
if('.' == arr[cur_row][cur_col])
return;
arr[cur_row][cur_col] = '.';
dfs(cur_row, cur_col-1);
dfs(cur_row-1, cur_col-1);
dfs(cur_row-1, cur_col);
dfs(cur_row-1, cur_col+1);
dfs(cur_row, cur_col+1);
dfs(cur_row+1, cur_col+1);
dfs(cur_row+1, cur_col);
dfs(cur_row+1, cur_col-1);
}
void solve(int row, int col){
int i, j, cnt;
max_row = row;
max_col = col;
cnt = 0;
for(i=1; i<=row; i++){
for(j=1; j<=col; j++){
if('W' == arr[i][j]){
cnt++;
dfs(i, j);
}
}
}
printf("%d\n", cnt);
}
int main(void){
int row, col, i;
//freopen("input.dat", "r", stdin);
while(EOF != scanf("%d %d", &row, &col)){
for(i=1; i<=row; i++)
scanf("%s", arr[i]+1);
solve(row, col);
}
return 0;
}