#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int INF = 1e9;
const int MOD = 1000000007;
const int MAX_N = 1e5 + 5;
int dx[4]{1,-1,0,0},dy[4]{0,0,1,-1};
int dist[4005][4005];
int n,m,ans;
string snow[4005];
bool inside(int x, int y) {
if(x >= 0 and x < n and y >= 0 and y < m and snow[x][y] != '.') return true;
return false;
}
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});
dist[0][0] = 1;
while(!q.empty()) {
pair<int,int> cur = q.front();
q.pop_front();
ans = max(ans,dist[cur.first][cur.second]);
for(int i=0; i<4; i++) {
int x = cur.first+dx[i], y = cur.second+dy[i];
if(inside(x,y) and dist[x][y] == 0) {
if(snow[cur.first][cur.second] == snow[x][y]) {
q.push_front({x,y});
dist[x][y] = dist[cur.first][cur.second];
} else {
q.push_back({x,y});
dist[x][y] = dist[cur.first][cur.second] + 1;
}
}
}
}
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("feast.in","r",stdin);
//freopen("feast.out","w",stdout);
int t=1;
while(t--)
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |