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;
char g[4005][4005];
bitset<4005> vis[4005];
vector<pair<short, short>> cur;
int h, w, dep = 1;
const int dr[4] = {0, 1, 0, -1}, dc[4] = {1, 0, -1, 0};
void dfs (int r, int c, char ch) {
if (!vis[r][c]) cur.push_back({r, c});
vis[r][c] = true;
for (int k = 0; k < 4; k++) {
int rv = r + dr[k], cv = c + dc[k];
if (rv >= 1 && rv <= h && cv >= 1 && cv <= w && !vis[rv][cv] && g[rv][cv] == ch) dfs(rv, cv, ch);
}
}
int main () {
cin >> h >> w;
for (int i = 1; i <= h; i++) for (int j = 1; j <= w; j++) cin >> g[i][j];
dfs(1, 1, g[1][1]);
while (true) {
dep++;
vector<pair<short, short>> last;
last.swap(cur);
for (auto& [r, c] : last) dfs(r, c, "RF"[(dep & 1) ^ (g[1][1] == 'R')]);
last.clear();
if (cur.empty()) {
cout << dep-1 << '\n';
return 0;
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |