#include <cstdio>
#include <stdio.h>
#include <stdbool.h>
#include <iostream>
#include <map>
#include <vector>
#include <climits>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <cctype>
#include <bitset>
#include <iomanip>
#include <cstring>
#include <numeric>
#include <cassert>
using namespace std;
#define int long long
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
int dy[4]={0, 0, 1, -1}, dx[4]={1, -1, 0, 0};
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m, ans=1;
cin>>n>>m;
vector<string> graph(n);
vector<vector<int> > dj(n, vector<int>(m, -1));
for (int i=0; i<n; ++i)cin>>graph[i];
deque<pii> deq;
dj[n-1][m-1]=1;
deq.pb(mp(n-1, m-1));
while (deq.size()){
int x=deq[0].fi, y=deq[0].se;
deq.pop_front();
ans=max(ans, dj[x][y]);
for (int i=0; i<4; ++i){
int nx=x+dx[i], ny=y+dy[i];
if (nx<0||ny<0||nx>=n||ny>=m||dj[nx][ny]!=-1||graph[nx][ny]=='.')continue;
if (graph[nx][ny]==graph[x][y])dj[nx][ny]=dj[x][y], deq.push_front(mp(nx, ny));
else dj[nx][ny]=dj[x][y]+1, deq.pb(mp(nx, ny));
}
}
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |