#include<bits/stdc++.h>
using namespace std;
const int N=4010;
int a[4]={0,0,1,-1};
int b[4]={1,-1,0,0};
vector<string> v;
bool vs[N][N];
queue<pair<int,int>> qf,qr;
bool now;
//false=qf , true=qr
int main(){
int n,m;
cin>>n >>m;
for(int i=0;i<n;i++){
string s;
cin>>s;
v.push_back(s);
}
if(v[0][0]!='F') now=true;
qf.push({0,0});
vs[0][0]=true;
int ans=0;
while(!(qf.empty() && qr.empty())){
ans++;
if(!now){
//floodfill of f
while(!qf.empty()){
auto [x,y]=qf.front();
qf.pop();
for(int i=0;i<4;i++){
int xa=x+a[i],yb=y+b[i];
if(xa<0 || xa>=n || yb<0 || yb>=m) continue;
if(vs[xa][yb] || v[xa][yb]=='.') continue;
if(v[xa][yb]=='R'){
vs[xa][yb]=true;
qr.push({xa,yb});
continue;
}
vs[xa][yb]=true;
qf.push({xa,yb});
}
}
now=true;
}
else{
//floodfill of r
while(!qr.empty()){
auto [x,y]=qr.front();
qr.pop();
for(int i=0;i<4;i++){
int xa=x+a[i],yb=y+b[i];
if(xa<0 || xa>=n || yb<0 || yb>=m) continue;
if(vs[xa][yb] || v[xa][yb]=='.') continue;
if(v[xa][yb]=='F'){
vs[xa][yb]=true;
qr.push({xa,yb});
continue;
}
vs[xa][yb]=true;
qr.push({xa,yb});
}
}
now=false;
}
}
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |