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;
vector<pair<int, int>> d{{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
void solve() {
int n, m;
cin >> n >> m;
vector<string> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
auto check = [&] (int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m;
};
char c = a[0][0];
vector<pair<int, int>> ts;
vector<vector<int>> usd(n, vector<int> (m));
auto dfs = [&] (int x, int y) {
vector<pair<int, int>> qu = {{x, y}};
usd[x][y] = 1;
for (int i = 0; i < (int) qu.size(); i++) {
auto [x, y] = qu[i];
ts.push_back({x, y});
for (auto [x2, y2] : d) {
x2 += x, y2 += y;
if (check(x2, y2) && a[x2][y2] == c && !usd[x2][y2]) {
usd[x2][y2] = 1;
qu.push_back({x2, y2});
}
}
}
};
dfs(0, 0);
int ans = 0;
while (1) {
c = c == 'F' ? 'R' : 'F';
ans++;
auto ct = ts;
ts.clear();
for (auto [x, y] : ct) {
for (auto [x2, y2] : d) {
x2 += x, y2 += y;
if (check(x2, y2) && a[x2][y2] == c && !usd[x2][y2]) {
dfs(x2, y2);
}
}
}
if (ts.empty()) {
break;
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.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... |