#include<bits/stdc++.h>
#define int long long
using namespace std;
int x[4] = {0,0,-1,1};
int y[4] = {1,-1,0,0};
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr),cout.tie(nullptr);
int h,w;
cin >> h >> w;
vector<string> s(h);
for(int i=0;i<h;i++) cin >> s[i];
deque<pair<int,int>> q;
q.push_front({0,0});
int ans = 0;
vector<vector<int>> depth(h,vector<int>(w,0));
depth[0][0] = 1;
while(q.size())
{
pair<int,int> c = q.front();
q.pop_front();
ans = max(ans,depth[c.first][c.second]);
for(int i=0;i<4;i++)
{
int a = x[i]+c.first,b = y[i]+c.second;
if( a>=0 && a<h && b>=0 && b<w && depth[a][b]==0 && s[a][b]!='.')
{
if(s[a][b]==s[c.first][c.second])
{
q.push_front({a,b});
depth[a][b] = depth[c.first][c.second];
}
else
{
q.push_back({a,b});
depth[a][b] = depth[c.first][c.second]+1;
}
//if(depth[a][b]==2) cout << a << " " << b << endl;
}
}
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |