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>
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cerr.tie(0)
#define mp make_pair
#define xx first
#define yy second
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define all(x) x.begin(),x.end()
#define ff(i,a,b) for (int i = a; i < b; i++)
#define fff(i,a,b) for (int i = a; i <= b; i++)
#define bff(i,a,b) for (int i = b-1; i >= a; i--)
#define bfff(i,a,b) for (int i = b; i >= a; i--)
using namespace std;
long double typedef ld;
unsigned int typedef ui;
long long int typedef li;
pair<int,int> typedef pii;
pair<li,li> typedef pli;
pair<ld,ld> typedef pld;
vector<vector<int>> typedef graph;
unsigned long long int typedef ull;
//const int mod = 998244353;
const int mod = 1000000007;
//Note to self: Check for overflow
int n,m;
char tab[4004][4004];
bool valid(int x, int y)
{
if (x<0 || y<0) return false;
if (x>=n || y>=m) return false;
if (tab[x][y]=='.') return false;
return true;
}
int main()
{
FAST;
cin>>n>>m;
ff(i,0,n) cin>>tab[i];
if (tab[0][0]=='.') return cout<<0,0;
int maxd=-1;
deque<pair<int,pii>> bfs;
bfs.pb({1,{0,0}});
while (!bfs.empty())
{
int d=bfs.front().xx;
int x=bfs.front().yy.xx;
int y=bfs.front().yy.yy;
bfs.popf();
if (tab[x][y]=='.') continue;
if (valid(x-1,y))
{
if (tab[x-1][y]!=tab[x][y]) bfs.pb({d+1,{x-1,y}});
else bfs.pf({d,{x-1,y}});
}
if (valid(x+1,y))
{
if (tab[x+1][y]!=tab[x][y]) bfs.pb({d+1,{x+1,y}});
else bfs.pf({d,{x+1,y}});
}
if (valid(x,y-1))
{
if (tab[x][y-1]!=tab[x][y]) bfs.pb({d+1,{x,y-1}});
else bfs.pf({d,{x,y-1}});
}
if (valid(x,y+1))
{
if (tab[x][y+1]!=tab[x][y]) bfs.pb({d+1,{x,y+1}});
else bfs.pf({d,{x,y+1}});
}
tab[x][y]='.';
maxd=max(maxd,d);
}
cout<<maxd<<"\n";
}
//Note to self: Check for overflow
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |