#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("1.in","r",stdin);
int n, m;
cin >> n >> m;
vector<vector<int>> grid(n, vector<int>(m,-1));
vector<vector<int>> dist(n, vector<int>(m,0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
char c;
cin >> c;
if (c=='F') grid[i][j]=1;
else if (c=='R') grid[i][j]=2;
}
}
int ans=0;
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
queue<pair<int,int>> q;
queue<pair<int,int>> q2;
q.push({0,0});
int curr_val=grid[0][0];
int curr_dist=1;
while (!q.empty()) {
auto [x,y] = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx<0 || nx>=n || ny<0 || ny>=m) continue;
if (grid[nx][ny]==-1) continue;
if (dist[nx][ny]!=0) continue;
if (grid[nx][ny]==curr_val) {
q.push({nx,ny});
dist[nx][ny]=curr_dist;
//cout<<nx<<" "<<ny<<"\n";
}
else {
q2.push({nx,ny});
dist[nx][ny]=curr_dist+1;
}
}
if (q.empty()) {
if (q2.empty()) break;
curr_dist++;
curr_val=3-curr_val;
while (!q2.empty()) {
auto pp = q2.front();
q2.pop();
q.push(pp);
}
}
}
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < m; j++) {
// cout << dist[i][j] << " ";
// }
// cout << "\n";
// }
cout<<curr_dist<<"\n";
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |