#include <bits/stdc++.h>
#define d1(x) cout << #x << " : " << x << endl << flush
#define d2(x, y) cout << #x << " : " << x << " " << #y << " : " << y << endl << flush
#define d3(x, y, z) cout << #x << " : " << x << " " << #y << " : " << y << " " << #z << " : " << z << endl << flush
#define d4(x, y, z, a) cout << #x << " : " << x << " " << #y << " : " << y << " " << #z << " : " << z << " "<< #a << " : " << a << endl << flush
#define ld long double
#define ll long long
#define pb push_back
#define arr(x) array <ll, x>
#define endl '\n'
#define int long long
using namespace std;
const int INF = 1e8 + 24 + (33 / 10); // 33 ---> 34
const int MAXN = 4e3 + 24 + (33 / 10); // 33 ---> 34
int dx[4] = {-0, -0, 1, -1};
int dy[4] = {1, -1, -0, -0};
int a[MAXN][MAXN];
int dis[MAXN][MAXN];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
char eli;
cin >> eli;
if(eli == 'F')
a[i][j] = 1;
else if(eli == 'R')
a[i][j] = 2;
}
}
for(int i = 0; i <= n; i++)
for(int j = 0; j <= m; j++)
dis[i][j] = INF;
deque <arr(2)> q;
q.push_front({1, 1});
dis[1][1] = 0;
int ans = 0;
while(!q.empty()){
auto [ux, uy] = q.front();
q.pop_front();
ans = max(ans, dis[ux][uy]);
for(int i = 0; i < 4; i++){
int vx = ux + dx[i];
int vy = uy + dy[i];
int w = (a[ux][uy] != a[vx][vy]);
if(!a[vx][vy])
continue;
if(dis[vx][vy] > dis[ux][uy] + w){
dis[vx][vy] = dis[ux][uy] + w;
if(w)
q.pb({vx, vy});
else
q.push_front({vx, vy});
}
}
}
cout << ans + 1 << endl;
}
// Man hamooonam ke ye rooz...
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |