#include<bits/stdc++.h>
using namespace std;
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
const int inf = 1e9+7;
void solve(){
int h,w ;
cin >> h >> w ;
vector<string>v(h+1) ;
vector<vector<int>>dist(h+1,vector<int>(w+1,inf)) ;
for(int i=1;i<=h;i++){
string s;
cin>>s;
v[i] = " " + s;
}
deque<pair<int,int>>q;
q.push_back({1,1});
dist[1][1] = 0;
while(!q.empty()){
auto pp = q.front();
q.pop_front();
int x = pp.first;
int y = pp.second;
char cur = v[x][y];
for(int i=0;i<4;i++){
int nx = x+dx[i];
int ny = y+dy[i];
if(nx<1 || nx>h || ny<1 || ny>w)continue;
if(v[nx][ny] == cur && dist[nx][ny] > dist[x][y]){
dist[nx][ny] = dist[x][y];
q.push_front({nx,ny});
}
else if(v[nx][ny] == (cur^'F'^'R') && dist[nx][ny] > dist[x][y] + 1){
dist[nx][ny] = dist[x][y] + 1;
q.push_back({nx,ny});
}
}
}
int ans =0;
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
if(dist[i][j] != inf){
ans= max(ans,dist[i][j]);
}
}
}
cout << ans+1 << "\n";
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
// cin>>t;
while(t--){
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |