This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Ali
// #pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ins insert
#define pii pair<int,int>
#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 = 8e6+23, inf = 2e9, LG = 19,pr = 31;
vector<int> graph[sze];
const int dx[4]={-1,0,1,0};
const int dy[4]={0,1,0,-1};
const int asz = 4e3 +5;
int cp[asz][asz];
int n,m;
int used[asz][asz];
int check(int a,int b){
return (a>=0 && a<n && b>=0 && b<m);
}
void fast(){
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];
}
}
int ans=1;
deque<pair<pii,int>> q;
q.push_front({{0,0},1});
used[0][0]=1;
while(!q.empty()){
pair<pii,int> node = q.front();q.pop_front();
ans=max(ans,node.second);
for(int x =0; x<4;x++){
int a = node.first.first + dx[x];
int b = node.first.second + dy[x];
if(check(a,b) && arr[a][b]!='.' && !used[a][b]){
used[a][b]=1;
if(arr[a][b] == arr[node.first.first][node.first.second]){
q.push_front({{a,b},node.second});
}
else{
q.push_back({{a,b},node.second +1});
}
}
}
}
putr(ans);
}
/*
5 8
FFR.....
.FRRR...
.FFFFF..
..RRRFFR
.....FFF
1 2
2 1 3
*/
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tt = 1;
// cin>>tt;
while(tt--){
fast();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |