This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include<bits/stdc++.h>
#define space <<' '<<
#define endl '\n'
#define inf 1e14
#define F first
#define S second
#define PB push_back
#define PF push_front
#define md(a) ((a+mod)%mod)
#define MP(a,b) make_pair(a,b)
#define MT(a,b,c) make_tuple(a,b,c)
typedef long long ll;
using namespace std;
template<typename t> using heap=
priority_queue<t,vector<t>,greater<t>>;
const int mx = 4005;
int h,w;
bool seen[mx][mx];
char state[mx][mx];
deque<pair<int,int>> l[2];
pair<int,int> nei[]={MP(0,1),MP(0,-1),MP(1,0),MP(-1,0)};
bool c(int i,int j){
return (i<0||h<=i||j<0||w<=j);
}
int main(){
std::ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>h>>w;
for(int i=0;i<h;i++)
for(int j=0;j<w;j++)
cin>>state[i][j];
int pc=0,cnt=0;
char cc=state[h-1][w-1];
l[pc].PB(MP(h-1,w-1));
if(cc=='.'){
cout<<0;
return 0;
}
while(!l[pc].empty()){
pair<int,int> r,d=l[pc].back();
seen[d.F][d.S]=1;
l[pc].pop_back();
for(int i=0;i<4;i++){
r=MP(d.F+nei[i].F,d.S+nei[i].S);
if(!c(r.F,r.S))
if (state[r.F][r.S]!='.'&&!seen[r.F][r.S]){
if(state[r.F][r.S]==cc)
l[pc].push_front(MP(r.F,r.S));
else
l[!pc].push_front(MP(r.F,r.S));
}
}
if(l[pc].empty()){
pc=!pc;cnt++;
cc=((cc=='F')?'R':'F');
}
}
cout<<cnt;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |