#pragma GCC optimize("O3", "inline")
#define skillissue <bits/stdc++.h>
#define ultra_mal std
#include skillissue
using namespace ultra_mal;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define ins insert
#define pb push_back
#define int long long int
#define pii pair<int, int>
#define endl '\n'
#define drop(x) cout<<(x)<<endl; return;
#define all(x) x.begin(),x.end()
#define hash FhashF
const int mod = 1e9 +7, sze = 1E3 +100, inf = LLONG_MAX, P = 1453;
// const int L = 30;
int comp[1100][1100];
const int dx[4] = {-1,1,0,0};
const int dy[4] = {0,0,-1,1};
vector<int> graph[sze];
void cave(){
int n,m;
cin>>n>>m;
vector<vector<char>> arr(n,vector<char>(m));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
comp[i][j]=-1;
cin>>arr[i][j];
}
}
int ct = 0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(arr[i][j]!='*' && comp[i][j]==-1){
queue<pair<int,int>> q;
q.push({i,j});
while(!q.empty()){
pii node = q.front();q.pop();
comp[node.first][node.second]=ct;
for(int x=0;x<4;x++){
int a = node.first + dx[x];
int b = node.second + dy[x];
if(a>=0 && a<n && b>=0 && b<m && comp[a][b]==-1 && arr[a][b]==arr[i][j]){
q.push({a,b});
// cout<<node.first<<" "<<node.second <<" _>"<<a<<" "<<b<<endl;
}
}
}
ct++;
}
}
}
set<pair<int,int>> edge;
for(int i =0;i<n;i++){
for(int j=0;j<m;j++){
if(comp[i][j]!=-1){
for(int x= 0; x<4;x++){
int a = i + dx[x];
int b = j + dy[x];
if(a>=0 && a<n && b>=0 && b<m && comp[a][b] !=-1 && comp[a][b]!=comp[i][j]){
if(edge.find({comp[i][j],comp[a][b]})==edge.end()){
edge.insert({comp[i][j],comp[a][b]});
graph[comp[i][j]].pb(comp[a][b]);
}
}
}
}
}
}
int ans=0;
queue<pii> q;
vector<int> used(sze,0);
q.push({0,1});
used[0]=1;
while(!q.empty()){
auto node = q.front();
ans=max(ans,node.second);
q.pop();
for(auto v:graph[node.first]){
if(!used[v]){
used[v]=1;
q.push({v,node.second+1});
}
}
}
drop(ans);
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int tt = 1;
// cin>>tt;
while(tt--){
cave();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
6 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Runtime error |
893 ms |
524288 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
6 ms |
1876 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Runtime error |
893 ms |
524288 KB |
Execution killed with signal 9 |
5 |
Halted |
0 ms |
0 KB |
- |