dfs的简单应用,求数组连通块
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pa;
int n, m;
int visit[105][105] = {};
int r[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int c[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
string s[105];
bool judge(int x, int y) {
if (x < 0 || y < 0 || x >= n || y >= m) return false;
return true;
}
void dfs(int x, int y) {
visit[x][y] = true;
for (int i = 0; i < 8; i++) {
int xx = x + r[i];
int yy = y + c[i];
if (judge(xx, yy) && visit[xx][yy] == false && s[xx][yy] == 'W') {
dfs(xx, yy);
}
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
int ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] == 'W' && visit[i][j] == false) {
dfs(i, j);
ans++;
}
}
}
cout << ans;
return 0;
}