This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int dx[] = {-1, +1, 0, 0};
int dy[] = {0, 0, +1, -1};
bool vis[4002][4002];
char adj[4002][4002];
void solve() {
int n, m, cnt = 0, hasR = 0, hasF = 0; cin >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) cin >> adj[i][j], hasF |= adj[i][j] == 'F', hasR |= adj[i][j] == 'R';
auto valid = [&](int r, int c) {
return r <= n and r >= 1 and c <= m and c >= 1 and vis[r][c] == 0 and adj[r][c] != '.';
};
auto bfs = [&] (int r, int c) {
queue<pair<int, int > > Q;
Q.push({r, c});
vis[r][c] = 1;
while (!Q.empty()) {
auto [R, C] = Q.front(); Q.pop();
for (int i = 0; i < 4; ++i) {
int nR = R + dx[i], nC = C + dy[i];
if (valid(nR, nC) and adj[nR][nC] == adj[r][c]) {
Q.push({nR, nC});
vis[nR][nC] = 1;
}
}
}
};
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (vis[i][j] or adj[i][j] == '.' or adj[i][j] != adj[1][1]) continue;
bfs(i, j);
cnt++;
}
}
cout << cnt + (hasF and hasR) << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
while(t--) solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |