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;
typedef long long ll;
#define rep(i,l,r) for(int i = (l); i < r; i++)
#define per(i,r,l) for(int i = (r); i >= l; i--)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define pb push_back
#define ff first
#define ss second
const ll mod = 1e9 + 7, maxn = 4e3 + 5, inf = 1e9 + 5;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int a[maxn][maxn];
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, m; cin >> n >> m;
rep(i,0,n){
string s; cin >> s;
rep(j,0,m){
if(s[i] == 'R') a[i][j] = 1;
else if(s[i] == 'F') a[i][j] = 2;
}
}
vector<vector<int>> dist(n, vector<int>(m, inf));
dist[0][0] = 1;
deque<pair<int, int>> q; q.pb({0, 0});
while(sz(q)){
int i = q.front().ff, j = q.front().ss; q.pop_front();
rep(d,0,4){
int r = dx[d] + i, c = dy[d] + j;
if(r < 0 || r >= n || c < 0 || c >= m || !a[r][c]) continue;
int w = a[i][j] != a[r][c];
if(dist[r][c] > dist[i][j] + w){
dist[r][c] = dist[i][j] + w;
if(w){
q.push_back({r, c});
}else{
q.push_front({r, c});
}
}
}
}
int ans = 0;
rep(i,0,n){
rep(j,0,m){
if(a[i][j]) ans = max(ans, dist[i][j]);
}
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |