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>
#define int long long
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int N=4e3+5;
char a[N][N];
int path[N][N];
int h,w;
bool ok(char c){
vector <int> x={1,-1,0,0};
vector <int> y={0,0,1,-1};
queue <pair <int,int> > q;
q.push({0,0});
if(a[0][0]==c or a[0][0]=='*'){
path[0][0]=1;
while(!q.empty()){
int X=q.front().ff;
int Y=q.front().ss;
q.pop();
for(int i=0;i<4;i++){
if(X+x[i]>=0 && X+x[i]<h && Y+y[i]>=0 && Y+y[i]<w){
if((a[X+x[i]][Y+y[i]]==c or a[X+x[i]][Y+y[i]]=='*') && !path[X+x[i]][Y+y[i]]){
path[X+x[i]][Y+y[i]]=1;
q.push({X+x[i],Y+y[i]});
}
}
}
}
}
if(path[h-1][w-1]){
bool fg=0;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(path[i][j]){
if(a[i][j]=='R' or a[i][j]=='F')fg=1;
a[i][j]='*';
}
path[i][j]=0;
}
}
if(fg)return true;
else return false;
}
return false;
}
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
cin>>h>>w;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
cin>>a[i][j];
}
}
int ans=0;
while(true){
int cnt=0;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
if(a[i][j]=='.' or a[i][j]=='*')cnt++;
}
}
if(cnt==h*w)break;
if(ok('F'))ans++;
else if(ok('R'))ans++;
}
cout<<ans<<"\n";
}
/*
FFR.....
.FRRR...
.FFFFF..
..RRRFFR
.....FFF
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |