#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int h,w;cin>>h>>w;
vector<vector<char>> snow(h, vector<char>(w));
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
cin>>snow[i][j];
}
}
vector<vector<int>> swaps(h, vector<int>(w, -1));
stack<pair<int, int>> diff;
stack<pair<int, int>> same;
same.push({0, 0});
swaps[0][0] = 1;
pair<int, int> GOES[] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
while(!same.empty() || !diff.empty()){
pair<int, int> c;
if(same.empty()){
c = diff.top();
diff.pop();
}
else{
c = same.top();
same.pop();
}
for(pair<int, int> go:GOES){
int x=c.first+go.first, y=c.second+go.second;
if(x < 0 || x >= h || y < 0 || y >= w)
continue;
if(snow[x][y] == '.' || swaps[x][y] != -1)
continue;
if(snow[x][y] == snow[c.first][c.second]){
same.push({x, y});
swaps[x][y] = swaps[c.first][c.second];
}
else{
diff.push({x, y});
swaps[x][y] = swaps[c.first][c.second]+1;
}
}
}
int mx=0;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
mx = max(mx, swaps[i][j]);
}
}
cout << mx << '\n';
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |