이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ins insert
#define pb push_back
#define endl '\n'
#define putr(x) cout<<x<<endl;return;
#define all(x) x.begin(),x.end()
const int mod = 1e9 +7, sze = 1e5 +5, inf = INT_MAX, LL = 30;
int comp[4005][4005];
int used[4005][4005];
const int dx[4] = {-1,1,0,0};
const int dy[4] = {0,0,-1,1};
void rush(){
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++){
cin>>arr[i][j];
comp[i][j]=-1;
}
}
int ans=0;
queue<pair<pair<int,int>,int>> q;
q.push({{0,0},1});
comp[0][0]=1;
int cnt=1;
while(!q.empty()){
auto node = q.front();
q.pop();
if(used[node.first.first][node.first.second]){
continue;
}
ans=max(ans,node.second);
used[node.first.first][node.first.second]=1;
for(int i=0;i<4;i++){
int a = node.first.first + dx[i];
int b = node.first.second + dy[i];
if(a>=0 && a<n && b>=0 && b<m){
if(arr[a][b]!='*'){
if(!comp[a][b]){
if(arr[a][b]!=arr[node.first.first][node.first.second]){
comp[a][b]= comp[node.first.first][node.first.second];
}
else{
comp[a][b]=++cnt;
}
}
if(comp[a][b]!=comp[node.first.first][node.first.second]){
q.push({{a,b}, node.second +1});
}
else{
q.push({{a,b}, node.second});
}
}
}
}
}
putr(ans);
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tt = 1;
// cin>>tt;
while(tt--){
rush();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |