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;
vector<int> X = {-1, 0, 1, 0}, Y = {0, 1, 0, -1};
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<string> a(n);
for (auto &i : a)
cin >> i;
char c = a[0][0];
queue<pair<int, int>> q;
vector<vector<bool>> mp(n, vector<bool>(m));
q.push({0, 0}); mp[0][0] = true;
for (int z = 1; ; z++) {
queue<pair<int, int>> nq;
while (!q.empty()) {
auto [x, y] = q.front(); q.pop();
for (int i = 0; i < 4; i++) {
int nx = x + X[i], ny = y + Y[i];
if (0 <= min(nx, ny) && nx < n && ny < m && !mp[nx][ny] && a[nx][ny] != '.') {
mp[nx][ny] = true;
(c == a[nx][ny] ? q : nq).push({nx, ny});
}
}
}
if (nq.empty()) return cout << z, 0;
q = nq;
c = (c == 'F' ? 'R' : 'F');
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |