You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
866 B

#include <bits/stdc++.h>
/*
测试用例:
4 4
R R R X
R X R X
X X X R
R X X X
答案:
3
*/
using namespace std;
const int N = 110;
int n, m;
int ans;
char g[N][N];
int dx[] = {-1, 0, 1, 0}; //上右下左
int dy[] = {0, 1, 0, -1}; //上右下左
void dfs(int x, int y) {
g[x][y] = 'X';
for (int i = 0; i < 4; i++) {
int tx = x + dx[i], ty = y + dy[i];
if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
if (g[tx][ty] == 'R') dfs(tx, ty);
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> g[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (g[i][j] == 'R') {
dfs(i, j);
ans++;
}
cout << ans << endl;
return 0;
}