#include <bits/stdc++.h>
using namespace std;
#define float long double
#define fr(i, a, b, jump) for (int i = (int)(a); i < (int)(b); i += (int)(jump))
#define vi vector<int>
#define vvi vector<vi>
#define pi pair<int, int>
#define si set<int>
#define size(a) (int)(a.size())
#define all(a) a.begin(), a.end()
#define pb push_back
#define line "\n"
#define pyes cout << "Yes" << line
#define pno cout << "No" << line
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m; cin >> n >> m;
int grid[n][m];
fr(i, 0, n, 1){
fr(j, 0, m, 1){
char x; cin >> x;
if (x == '.'){
grid[i][j] = -1;
}
else{
grid[i][j] = (x == 'F') ? 1 : 0;
}
}
}
pi dir[4] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
priority_queue<pair<int, pi>> q;
q.push({0, {0, 0}});
vvi val(n, vi(m, -1));
val[0][0] = 1;
int ans = 1;
while (size(q)){
auto [idk, p] = q.top();q.pop();
auto [i, j] = p;
for (auto [x, y] : dir){
if (x + i >= 0 && x + i < n && y + j >= 0 && y + j < m){
int ni = i + x;
int nj = j + y;
if (val[ni][nj] == -1 && grid[ni][nj] != -1){
if (grid[i][j] == grid[ni][nj]){
val[ni][nj] = val[i][j];
}
else{
val[ni][nj] = 1 + val[i][j];
ans = max(ans, val[ni][nj]);
}
q.push({-val[ni][nj], {ni, nj}});
}
}
}
}
cout << ans << line;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |