This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
int n, m;
string s[5000];
int dp[5000][5000];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, 1, -1};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for(int i=0; i<n; i++) {
cin >> s[i];
}
deque<pair<int, int>> q;
q.push_back({0, 0});
dp[0][0] = 1;
int ans = 0;
while(!q.empty()) {
auto me = q.front();
q.pop_front();
ans = max(ans, dp[me.first][me.second]);
for(int to=0; to<4; to++) {
int i = me.first +dx[to], j = me.second + dy[to];
if(i>=0&&i<n&&j>=0&&j<m&&s[i][j]!='.') {
if(dp[i][j]==0) {
if(s[i][j]!=s[me.first][me.second]) {
dp[i][j] = dp[me.first][me.second] + 1;
q.push_back({i, j});
} else {
dp[i][j] = dp[me.first][me.second];
q.push_front({i, j});
}
}
}
}
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |