#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int> dx {1, -1, 0, 0};
vector<int> dy {0, 0, 1, -1};
int n, m, ans = 1;
vector<vector<int>> num_animals(4000, vector<int> (4000, 0));
vector<string> snow(4000);
bool inside(int x, int y){
return (x >= 0 && x <= n - 1 && y >= 0 && y <= m - 1 && snow[x][y] != '.');
}
void solve(){
cin >> n >> m;
for(int i = 0; i < n; i++) cin >> snow[i];
deque<pair<int, int>> q;
q.push_back({0, 0});
num_animals[0][0] = 1;
while(!q.empty()){
auto c = q.front();
q.pop_front();
ans = max(ans, num_animals[c.first][c.second]);
for(int i = 0; i < 4; i++){
int x = c.first + dx[i], y = c.second + dy[i];
if(inside(x, y) && num_animals[x][y] == 0){
if(snow[c.first][c.second] == snow[x][y]){
num_animals[x][y] = num_animals[c.first][c.second];
q.push_front({x, y});
}else{
num_animals[x][y] = num_animals[c.first][c.second] + 1;
q.push_back({x, y});
}
}
}
}
cout << ans << endl;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
while(t--) solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |