#include<bits/stdc++.h>
using namespace std;
int H, W;
string snow[4005];
bool inside(int y, int x){
if( y < H && y >= 0 && x < W && x >= 0 && snow[y][x] != '.' ){
return true;
}else return false;
}
int main(){
int mx[4] = {-1, 1, 0, 0};
int my[4] = {0, 0, 1, -1};
cin>>H>>W;
int dist[H][W], ans = 1;
for(int i = 0; i< H; i++) for(int j = 0; j < W; j++) dist[i][j] = 0;
for(int i = 0; i < H; i++) cin>>snow[i];
deque<pair<int,int>> curr; // kalo samaen depan, beda belakang
dist[0][0] = 1;
curr.push_front({0,0});
while(!curr.empty()){
auto [curry, currx] = curr.front(); curr.pop_front();
ans = max(dist[curry][currx], ans);
for(int i = 0; i < 4; i++){
int x = currx + mx[i], y = curry + my[i];
if(inside(y,x) && dist[y][x]){
if(snow[y][x] != snow[curry][currx]){
curr.push_back({y,x});
dist[y][x] = dist[curry][currx] + 1;
}else{
curr.push_front({y,x});
dist[y][x] = dist[curry][currx];
}
}
}
}
cout<<ans<<endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |