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 h, w;
char s[4000][4000];
bool chk(pair<int, int> c)
{
return c.first > -1 && c.first < h && c.second < w && c.second > -1 && s[c.first][c.second] != '.';
}
int
main()
{
cin >> h >> w;
deque<pair<int, int>> q;
vector<vector<int>> d(h, vector<int>(w));
d[0][0] = 1;
for(int i = 0; i < h; ++i) {
for(int j = 0; j < w; ++j) {
cin >> s[i][j];
}
}
q.push_front({0, 0});
vector<int> dx = {-1, 0, 1, 0}, dy = {0, 1, 0, -1};
int ans = 0;
while(!q.empty()) {
pair<int, int> x = q.front();
q.pop_front();
ans = max(ans, d[x.first][x.second]);
for(int i = 0; i < 4; ++i) {
pair<int, int> y = {x.first + dx[i], x.second + dy[i]};
if(chk(y) && d[y.first][y.second] == 0) {
if(s[y.first][y.second] == s[x.first][x.second]) {
d[y.first][y.second] = d[x.first][x.second];
q.push_front({y.first, y.second});
}
else {
d[y.first][y.second] = d[x.first][x.second] + 1;
q.push_back({y.first, y.second});
}
}
}
}
cout << ans << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |