#define _DEB213UG
// #include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
// using namespace atcoder;
#define endl "\n"
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define CNT_LOWER(v, n) (ll)(lower_bound((v).begin(), (v).end(), (n)) - (v).begin())
#define CNT_UPPER(v, n) (ll)(upper_bound((v).begin(), (v).end(), (n)) - (v).begin())
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pld;
typedef vector<int> vi;
typedef vector<pii> vii;
const long double pi = acos(-1.0);
const int INF = 1987654321;
const ll LLINF = 1e18;
const double eps = 1e-9;
template<class T>bool chmax(T& a, const T& b) { if (a <= b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b <= a) { a = b; return 1; } return 0; }
//////////////////////
// #ifndef ONLINE_JUDGE
// #include "template.cpp"
// #else
// #define debug(...)
// #define debugArr(...)
// #endif
//////////////////////
const int dy[]={-1,1,0,0};
const int dx[]={0,0,-1,1};
int dist[4040][4040];
string board[4040];
int main(){
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N,M;cin>>N>>M;
for(int i=0;i<N;i++)cin>>board[i];
memset(dist,-1,sizeof(dist));
dist[0][0]=1;
deque<pii> dq;
dq.push_back({0,0});
int ans=0;
while(!dq.empty()){
auto cur=dq.front();dq.pop_front();
for(int dir=0;dir<4;dir++){
int ny=cur.first+dy[dir];
int nx=cur.second+dx[dir];
if(ny<0||ny>=N||nx<0||nx>=M)continue;
if(dist[ny][nx]!=-1)continue;
if(board[ny][nx]=='.')continue;
if(board[ny][nx]==board[cur.first][cur.second]){
dist[ny][nx]=dist[cur.first][cur.second];
dq.push_front({ny,nx});
}
else{
dist[ny][nx]=dist[cur.first][cur.second]+1;
dq.push_back({ny,nx});
}
chmax(ans,dist[ny][nx]);
}
}
cout<<ans<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |