题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
//flood fill 连通问题
//dfs实现
#include<cstdio>
#include<iostream>
using namespace std;
const int N = 1010;
int n,m;
int res;
char field[N][N];
int dx[8]={1,0,-1,0,1,-1,1,-1},dy[8]={0,1,0,-1,1,-1,-1,1};
void dfs(int x, int y){
field[x][y] = '.';
int nx,ny;
//9个方向实质上 0 0 就是自己本身. 所以和遍历八个方向是一样的.
/*for(int dx = -1; dx <= 1; dx++){
for(int dy = -1; dy <= 1; dy++){
int nx = x + dx, ny = x + dy;
if(0 <= nx && nx < n && 0 <= ny && ny < m && field[nx][ny] == 'W') dfs(nx,ny);
}
}
然而测试的时候出错了???有人能告诉我为啥错了吗*/
for(int i = 0; i < 8; ++i){
nx = x + dx[i]; ny = y + dy[i];
if(0 <= nx && nx < n && 0 <= ny && ny < m && field[nx][ny] == 'W') dfs(nx,ny);
}
return ;
}
void solve(){
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(field[i][j] == 'W'){
dfs(i,j);
res++;
}
}
}
cout << res << endl;
}
int main(){
cin >> n >> m;
for(int i = 0; i < n; i++) scanf("%s",field[i]);
//for(int i = 0; i < n; i++) printf("测试 %s\n",field[i]);
solve();
return 0;
}