#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,1,0,-1};
const int N = 4e3+5;
int h,w;
string bd[N];
int cnt;
int d[N][N];
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> h >> w;
for (int i = 1; i <= h; i++) {
cin >> bd[i];
bd[i] = " " + bd[i];
for (int j = 1; j <= w; j++) {
d[i][j] = INT_MAX;
}
}
priority_queue <pair< int,pair<int,int> >, vector < pair<int,pair<int,int>> >, greater< pair<int,pair<int,int>> > > q;
q.push({1,{1,1}});
d[1][1]=1;
int cur = 0;
while (!q.empty()) {
auto [x,y] = q.top().second;
int c = q.top().first;
q.pop();
if (d[x][y] != c) continue;
cur++;
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (!(1 <= nx && nx <= h && 1 <= ny && ny <= w)) continue;
if (bd[nx][ny] == '.') continue;
if (d[nx][ny] > d[x][y]+(bd[nx][ny]!=bd[x][y])) {
d[nx][ny] = d[x][y]+(bd[nx][ny]!=bd[x][y]);
q.push({d[nx][ny],{nx,ny}});
}
}
}
int ans = -INT_MAX;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
if (bd[i][j] == '.') continue;
ans = max(ans,d[i][j]);
}
}
cout << ans << endl;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |