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.

54 lines
1.1 KiB

#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 res;
typedef pair<int, int> PII;
char a[N][N];
int dx[] = {-1, 0, 1, 0}; //上右下左
int dy[] = {0, 1, 0, -1}; //上右下左
void bfs(int x, int y) {
queue<PII> q;
q.push({x, y});
a[x][y] = 'X';
while (q.size()) {
PII u = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int tx = u.first + dx[i], ty = u.second + dy[i];
if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
if (a[tx][ty] == 'R') {
a[tx][ty] = 'X';
q.push({tx, ty});
}
}
}
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> a[i][j];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i][j] == 'R') {
bfs(i, j);
res++;
}
cout << res << endl;
return 0;
}