This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#ifdef LOCAL
#include "Essentials/algo/debug.h"
#else
#define debug(...) 42
#endif
using namespace std;
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
typedef long long ll;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int N = 4000 + 23;
const ll mod = 1e9+7;
const int LOG = 23;
const ll inf = 1e18;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define F first
#define S second
#define pb push_back
#define ms(x,y) memset((x) , (y) , sizeof (x))
#define done return cout<<endl , 0;
#define kill(x) cout<<x<<endl, exit(0);
#define isIn(x,s,e) ((x) >= (s) && (x) <= e)
#define all(x) x.begin(),x.end()
#define sz(x) (int)x.size()
#define pc(x) __builtin_popcount(x)
#define ctz(x) __builtin_ctz(x)
#define MinHeap(x) priority_queue<x, vector<x> , greater<x> >
#define MaxHeap(x) priority_queue<x, vector<x>>
//#define int ll
ll pw(ll a, ll b, ll md = mod){ll res = 1; while(b){if(b&1){res=(a*res)%md;}a=(a*a)%md;b>>=1;}return(res);}
int h,w;
char a[N][N];
bool mark[N][N];
int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1};
int dist[N][N];
int ans = 0;
deque<pii> Q;
void bfs() {
Q.pb({0,0});
mark[0][0] = true;
dist[0][0] = 1;
while(sz(Q)) {
auto [x,y] = Q.back(); Q.pop_back();
ans = max(ans,dist[x][y]);
for(int i = 0 ;i < 4; i ++) {
int x2 = x + dx[i] , y2= y + dy[i];
if(isIn(x2,0,h-1) && isIn(y2,0,w-1) && !mark[x2][y2]) {
if(a[x2][y2] == a[x][y]) {
dist[x2][y2] = dist[x][y];
mark[x2][y2] = true;
Q.push_back({x2,y2});
} else {
dist[x2][y2] = dist[x][y] + 1;
mark[x2][y2] = true;
Q.push_front({x2,y2});
}
}
}
}
}
int32_t main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin>>h>>w;
for(int i = 0 ;i < h ; i ++){
for(int j = 0 ; j< w; j++) {
cin>>a[i][j];
if(a[i][j] == '.') {
mark[i][j] = true;
}
}
}
bfs();
cout<<ans<<'\n';
done;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |