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 mat[4010][4010];
vector <pair <int, int>> de_add, added, dir = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };
int n, m;
char c;
void dfs(int a, int b)
{
mat[a][b] = '?';
de_add.push_back({ a, b });
for (auto i : dir)
if (mat[a + i.first][b + i.second] == c)
dfs(a + i.first, b + i.second);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++)
cin >> (mat[i] + 1);
int ans = 0;
c = mat[1][1];
if (c == '.')
return cout << 0, 0;
dfs(1, 1);
while (!de_add.empty()) {
swap(de_add, added);
de_add.clear();
ans++;
c = (c == 'F' ? 'R' : 'F');
for (auto i : added)
for (auto j : dir)
if (mat[i.first + j.first][i.second + j.second] == c)
dfs(i.first + j.first, i.second + j.second);
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |