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>
using namespace std;
int n,m;
string mat[4001];
int addx[4]={1,0,-1,0};
int addy[4]={0,1,0,-1};
vector<pair<int,int> > pt[2];
int rez=0;
bool check(int i,int j)
{
if(i<0 || i>=n || j<0 || j>=m || mat[i][j]=='.')return 0;
return 1;
}
void dfs(int x,int y)
{
char cur=mat[x][y];
mat[x][y]='.';
for(int i=0;i<4;i++)
{
int c1=x+addx[i];
int c2=y+addy[i];
if(!check(c1,c2))
{
continue;
}
if(mat[c1][c2]==cur)
{
dfs(c1,c2);
}
else
{
pt[(rez&1)^1].push_back({c1,c2});
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i=0;i<n;i++)
{
cin >> mat[i];
}
pt[rez&1].push_back({0,0});
while(pt[rez&1].size())
{
for(pair<int,int> p : pt[rez&1])
{
if(check(p.first,p.second))
{
dfs(p.first,p.second);
}
}
pt[rez&1].clear();
rez++;
}
cout << rez;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |