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>
using namespace std;
#define int long long
typedef pair<int,int>pii;
int32_t main(){
//ios::sync_with_stdio(0);
//cin.tie(0);
int n,m;
cin >> n >> m;
string arr[n];
for(int x=0;x<n;x++){
cin >> arr[x];
}
queue<pii>q;
queue<pii>q2;
int counter=0;
bool visited[n+5][m+5];
memset(visited,0,sizeof(visited));
bool amos=true;
//true is R, false is F
if(arr[0][0]=='F') amos=false;
if(amos){
q.push({0,0});
visited[0][0]=true;
}
else{
q2.push({0,0});
visited[0][0]=true;
}
pii dir[4]={
{1,0},
{-1,0},
{0,1},
{0,-1},
};
while(!q.empty()||!q2.empty()){
if(amos){
//cout << "true\n";
while(!q.empty()){
pii cur=q.front();
q.pop();
for(auto it:dir){
int newx=cur.first+it.first;
int newy=cur.second+it.second;
if(newx<0||newy<0||newx>=n||newy>=m) continue;
if(visited[newx][newy]) continue;
if(arr[newx][newy]=='.') continue;
visited[newx][newy]=true;
if(arr[newx][newy]=='R'){
q.push({newx,newy});
}
else{
q2.push({newx,newy});
}
}
}
amos=false;
}
else{
//cout << "false\n";
while(!q2.empty()){
pii cur=q2.front();
q2.pop();
//cout << cur.first << " " << cur.second << " visit\n";
for(auto it:dir){
int newx=cur.first+it.first;
int newy=cur.second+it.second;
if(newx<0||newy<0||newx>=n||newy>=m) continue;
if(visited[newx][newy]) continue;
if(arr[newx][newy]=='.') continue;
visited[newx][newy]=true;
if(arr[newx][newy]=='R'){
q.push({newx,newy});
}
else{
q2.push({newx,newy});
}
}
}
amos=true;
}
counter++;
//cout << counter << "\n";
}
cout << counter;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |