#include <bits/stdc++.h>
#define ll long long
#define fast1 ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"
//#define int long long
#define mod 1000000007
using namespace std;
//#pragma GCC optimize("Ofast")
//#pragma GCC target("fma,sse,sse2,sse3,avx")
//#pragma GCC optimize("unroll-loops")
int n,m;
char dizi[4003][4003];
int vis[4003][4003];
queue<pair<pair<int,int> ,int> > q;
queue<pair<pair<int,int>,int> > q1;
void dfs(int x,int y,char z)
{
if(vis[x][y])
return;
vis[x][y]=1;
if(!vis[x+1][y]&&dizi[x+1][y]==z)
dfs(x+1,y,z);
if(!vis[x-1][y]&&dizi[x-1][y]==z)
dfs(x-1,y,z);
if(!vis[x][y+1]&&dizi[x][y+1]==z)
dfs(x,y+1,z);
if(!vis[x][y-1]&&dizi[x][y-1]==z)
dfs(x,y-1,z);
/////////////////////////////////////
if(!vis[x+1][y]&&dizi[x+1][y]!=z&&dizi[x+1][y]!='.')
q.push({{x+1,y},2});
if(!vis[x-1][y]&&dizi[x-1][y]!=z&&dizi[x-1][y]!='.')
q.push({{x-1,y},2});
if(!vis[x][y+1]&&dizi[x][y+1]!=z&&dizi[x][y+1]!='.')
q.push({{x,y+1},2});
if(!vis[x][y-1]&&dizi[x][y-1]!=z&&dizi[x][y-1]!='.')
q.push({{x,y-1},2});
}
void solve()
{
cin>>n>>m;
for(int i=0;i<=n+1;i++)
for(int j=0;j<=m+1;j++)
dizi[i][j]='.';
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>dizi[i][j];
/*dfs(1,1,dizi[1][1]);
if(q.empty())
{
cout<<1;
return;
}
*/
int mx=1;
q.push({{1,1},1});
while(1)
{
if(q.size()==0)
{
cout<<mx;
return;
}
while(!q.empty())
{
int x=q.front().first.first;
int y=q.front().first.second;
int z=q.front().second;
q.pop();
if(vis[x][y])
continue;
vis[x][y]=1;
mx=max(mx,z);
if(!vis[x+1][y]&&dizi[x+1][y]==dizi[x][y])
q.push({{x+1,y},z});
if(!vis[x-1][y]&&dizi[x-1][y]==dizi[x][y])
q.push({{x-1,y},z});
if(!vis[x][y+1]&&dizi[x][y+1]==dizi[x][y])
q.push({{x,y+1},z});
if(!vis[x][y-1]&&dizi[x][y-1]==dizi[x][y])
q.push({{x,y-1},z});
/////////////////////////////
if(!vis[x+1][y]&&dizi[x+1][y]!=dizi[x][y]&&dizi[x+1][y]!='.')
q1.push({{x+1,y},z+1});
if(!vis[x-1][y]&&dizi[x-1][y]!=dizi[x][y]&&dizi[x-1][y]!='.')
q1.push({{x-1,y},z+1});
if(!vis[x][y+1]&&dizi[x][y+1]!=dizi[x][y]&&dizi[x][y+1]!='.')
q1.push({{x,y+1},z+1});
if(!vis[x][y-1]&&dizi[x][y-1]!=dizi[x][y]&&dizi[x][y-1]!='.')
q1.push({{x,y-1},z+1});
}
while(!q1.empty())
{
int x=q1.front().first.first;
int y=q1.front().first.second;
int z=q1.front().second;
q1.pop();
if(vis[x][y])
continue;
vis[x][y]=1;
mx=max(mx,z);
if(!vis[x+1][y]&&dizi[x+1][y]==dizi[x][y])
q1.push({{x+1,y},z});
if(!vis[x-1][y]&&dizi[x-1][y]==dizi[x][y])
q1.push({{x-1,y},z});
if(!vis[x][y+1]&&dizi[x][y+1]==dizi[x][y])
q1.push({{x,y+1},z});
if(!vis[x][y-1]&&dizi[x][y-1]==dizi[x][y])
q1.push({{x,y-1},z});
/////////////////////////////
if(!vis[x+1][y]&&dizi[x+1][y]!=dizi[x][y]&&dizi[x+1][y]!='.')
q.push({{x+1,y},z+1});
if(!vis[x-1][y]&&dizi[x-1][y]!=dizi[x][y]&&dizi[x-1][y]!='.')
q.push({{x-1,y},z+1});
if(!vis[x][y+1]&&dizi[x][y+1]!=dizi[x][y]&&dizi[x][y+1]!='.')
q.push({{x,y+1},z+1});
if(!vis[x][y-1]&&dizi[x][y-1]!=dizi[x][y]&&dizi[x][y-1]!='.')
q.push({{x,y-1},z+1});
}
if(q.size()==0)
{
cout<<mx;
return;
}
}
}
signed main()
{
//freopen ("lca.gir","r",stdin);
//freopen ("lca.cik","w",stdout);
fast1
int t=1;
//cin>>t;
while(t--)
{
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
5580 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
12 ms |
5376 KB |
Output is correct |
5 |
Correct |
4 ms |
2900 KB |
Output is correct |
6 |
Correct |
0 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
4 ms |
2524 KB |
Output is correct |
11 |
Correct |
4 ms |
2132 KB |
Output is correct |
12 |
Correct |
9 ms |
3104 KB |
Output is correct |
13 |
Correct |
5 ms |
2900 KB |
Output is correct |
14 |
Correct |
5 ms |
2900 KB |
Output is correct |
15 |
Correct |
22 ms |
5336 KB |
Output is correct |
16 |
Correct |
24 ms |
5584 KB |
Output is correct |
17 |
Correct |
14 ms |
5272 KB |
Output is correct |
18 |
Correct |
12 ms |
5320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
30820 KB |
Output is correct |
2 |
Correct |
72 ms |
14952 KB |
Output is correct |
3 |
Correct |
415 ms |
73452 KB |
Output is correct |
4 |
Correct |
100 ms |
32588 KB |
Output is correct |
5 |
Correct |
217 ms |
53316 KB |
Output is correct |
6 |
Correct |
1462 ms |
120408 KB |
Output is correct |
7 |
Correct |
16 ms |
32212 KB |
Output is correct |
8 |
Correct |
17 ms |
30804 KB |
Output is correct |
9 |
Correct |
3 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
476 KB |
Output is correct |
11 |
Correct |
19 ms |
31640 KB |
Output is correct |
12 |
Correct |
2 ms |
1620 KB |
Output is correct |
13 |
Correct |
81 ms |
14952 KB |
Output is correct |
14 |
Correct |
40 ms |
10456 KB |
Output is correct |
15 |
Correct |
27 ms |
13008 KB |
Output is correct |
16 |
Correct |
36 ms |
5732 KB |
Output is correct |
17 |
Correct |
211 ms |
28968 KB |
Output is correct |
18 |
Correct |
114 ms |
35804 KB |
Output is correct |
19 |
Correct |
105 ms |
32640 KB |
Output is correct |
20 |
Correct |
97 ms |
25512 KB |
Output is correct |
21 |
Correct |
238 ms |
52536 KB |
Output is correct |
22 |
Correct |
235 ms |
53280 KB |
Output is correct |
23 |
Correct |
363 ms |
43784 KB |
Output is correct |
24 |
Correct |
206 ms |
49776 KB |
Output is correct |
25 |
Correct |
490 ms |
94428 KB |
Output is correct |
26 |
Correct |
877 ms |
80964 KB |
Output is correct |
27 |
Correct |
1039 ms |
99108 KB |
Output is correct |
28 |
Correct |
1581 ms |
120688 KB |
Output is correct |
29 |
Correct |
1396 ms |
114628 KB |
Output is correct |
30 |
Correct |
1269 ms |
111908 KB |
Output is correct |
31 |
Correct |
1078 ms |
74264 KB |
Output is correct |
32 |
Correct |
820 ms |
97388 KB |
Output is correct |