#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0)->sync_with_stdio(0);
int h, w;
cin >> h >> w;
vector<vector<int>> a(h, vector<int>(w));
for (auto &x : a) {
for (auto &y : x) {
char c;
cin >> c;
if (c == '.')
y = 0;
else if (c == 'R')
y = 1;
else
y = 2;
}
}
int st = a[0][0];
int ans = 0;
queue<pair<int, int>> cur, oth;
cur.push({0, 0});
vector<vector<bool>> vis(h, vector<bool>(w, false));
vis[0][0] = true;
while (!cur.empty()) {
ans++;
while (!cur.empty()) {
auto &[i, j] = cur.front();
cur.pop();
if (i != 0) {
if (!vis[i - 1][j] && a[i - 1][j]) {
vis[i - 1][j] = true;
if (a[i - 1][j] == st)
cur.push({i - 1, j});
else
oth.push({i - 1, j});
}
}
if (i != h - 1) {
if (!vis[i + 1][j] && a[i + 1][j]) {
vis[i + 1][j] = true;
if (a[i + 1][j] == st)
cur.push({i + 1, j});
else
oth.push({i + 1, j});
}
}
if (j != 0) {
if (!vis[i][j - 1] && a[i][j - 1]) {
vis[i][j - 1] = true;
if (a[i][j - 1] == st)
cur.push({i, j - 1});
else
oth.push({i, j - 1});
}
}
if (j != w - 1) {
if (!vis[i][j + 1] && a[i][j + 1]) {
vis[i][j + 1] = true;
if (a[i][j + 1] == st)
cur.push({i, j + 1});
else
oth.push({i, j + 1});
}
}
}
st ^= 3;
swap(cur, oth);
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |