#include <bits/stdc++.h>
#define un unsgined
#define rep(i, a, b) for(int i = a; i < b; i++)
#define per(i, a, b) for(int i = a; i >= b; i--)
#define all(v) begin(v), end(v)
#define st first
#define nd second
using ll = long long;
using bigi = __int128;
using namespace std;
const int N = 4005;
vector<pair<int, int>> ruch{
{0, 1},
{0, -1},
{1, 0},
{-1, 0}
};
char tab[N][N];
int gle[N][N];
queue<pair<short, short>> odpal;
void zaz(short i, short j){
queue<pair<int, int>> qzaz;
qzaz.push({i, j});
while(!qzaz.empty()){
auto [ii, jj] = qzaz.front();
qzaz.pop();
for(auto [x, y] : ruch){
if(!tab[ii + x][jj + y] || gle[ii + x][jj + y]) continue;
if(tab[ii + x][jj + y] == tab[ii][jj]){
gle[ii + x][jj + y] = gle[ii][jj];
qzaz.push({ii + x, jj + y});
}
else{
gle[ii + x][jj + y] = gle[ii][jj] + 1;
odpal.push({ii + x, jj + y});
}
}
}
}
void dalej(){
gle[1][1] = 1;
odpal.push({1, 1});
while(!odpal.empty()){
auto [ii, jj] = odpal.front();
odpal.pop();
zaz(ii, jj);
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
short n, m;
cin >> n >> m;
rep(i, 1, n + 1){
rep(j, 1, m + 1){
cin >> tab[i][j];
if(tab[i][j] == '.') tab[i][j] = 0;
}
}
dalej();
int maks = 0;
rep(i, 1, n + 1){
rep(j, 1, m + 1){
maks = max(maks, gle[i][j]);
}
}
cout << maks << '\n';
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |