#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define maxn 200005
#define f first
#define s second
#define ll long long
#define pb(x) push_back(x)
int x[4]={-1, 1, 0, 0}, y[4]={0, 0, -1, 1};
void solve(){
int w, h;
cin >> h >> w;
char c[h+2][w+2], mch;
bool used[h+2][w+2];
for(int i=0; i<h+2; i++){
for(int j=0; j<w+2; j++){
c[i][j]='.';
used[i][j]=0;
}
}
for(int i=1; i<=h; i++){
for(int j=1; j<=w; j++){
cin >> c[i][j];
}
}
mch=c[1][1];
used[1][1]=1;
queue<pair<int, int>> q1, q2;
q1.push({1, 1});
int sh=0;
while(q1.size()||q2.size()){
while(q2.size()){
int i=q2.front().f, j=q2.front().s;q2.pop();
for(int l=0; l<4; l++){
if(!used[i+x[l]][j+y[l]]&&c[i+x[l]][j+y[l]]==mch){
used[i+x[l]][j+y[l]]=1;
q1.push({i+x[l],j+y[l]});
}
}
}
while(q1.size()){
int i=q1.front().f, j=q1.front().s;q1.pop();
for(int l=0; l<4; l++){
if(!used[i+x[l]][j+y[l]]&&c[i+x[l]][j+y[l]]==mch){
used[i+x[l]][j+y[l]]=1;
q1.push({i+x[l],j+y[l]});
}
}
q2.push({i, j});
}
mch=(mch=='R'?'F':'R');
sh++;
}
cout << sh-1;
}
int main(){
cin.tie(nullptr)->sync_with_stdio(0);
int t=1;
//cin >> t;
while(t--){
solve();
cout << "\n";
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |