#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<ll> dx = {0, 1, 0, -1};
vector<ll> dy = {1, 0, -1, 0};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("template.in", "r", stdin);
// freopen("template.out", "w", stdout);
ll n, m; cin >> n >> m; vector<vector<ll>> snow(n + 2, vector<ll>(m + 2, -1));
for (ll i = 1; i <= n; ++i){
for (ll j = 1; j <= m; ++j){
char x; cin >> x;
if (x == 'F'){
snow[i][j] = 1;
}
else if (x == 'R'){
snow[i][j] = 0;
}
}
}
vector<vector<ll>> d(n + 2, vector<ll>(m + 2, INT_MAX)); d[1][1] = 1;
deque<pair<ll, ll>> dq; dq.push_back({1, 1});
ll maxd = 1;
while (!dq.empty()){
auto p = dq.front();
dq.pop_front();
for (ll i = 0; i < 4; ++i){
pair<ll, ll> q = {p.first + dx[i], p.second + dy[i]};
if (snow[q.first][q.second] == -1){continue;}
if (snow[q.first][q.second] == snow[p.first][p.second]){
if (d[p.first][p.second] < d[q.first][q.second]){
d[q.first][q.second] = d[p.first][p.second];
dq.push_front(q);
}
}
else {
if (d[p.first][p.second] + 1 < d[q.first][q.second]){
d[q.first][q.second] = d[p.first][p.second] + 1;
dq.push_back(q); maxd = max(maxd, d[q.first][q.second]);
}
}
}
}
cout << maxd;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |