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"
#define endl '\n'
const int maxn = 1005;
const int inf = 2e9;
const int mod = 1e9 + 7;
using namespace std;
using lint = long long;
using pii = pair<int,int>;
int n, m;
int ans;
char g[maxn][maxn];
int d[maxn][maxn];
deque<pii> dq;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
bool check(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m && g[x][y] != '*' && !d[x][y];
}
signed main() {
#ifdef Local
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for(int i = 0; i < n; i++)
cin >> g[i];
d[0][0] = 1;
dq.push_back({0, 0});
while(!dq.empty()) {
auto [x1, y1] = dq.front();
ans = max(ans, d[x1][y1]);
dq.pop_front();
for(int i = 0; i < 4; i++) {
int x2 = x1 + dx[i], y2 = y1 + dy[i];
if(!check(x2, y2))
continue;
int w = g[x2][y2] != g[x1][y1];
if(w == 0) {
dq.push_front({x2, y2});
d[x2][y2] = d[x1][y1];
}
else {
dq.push_back({x2, y2});
d[x2][y2] = d[x1][y1] + 1;
}
}
}
cout << ans << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |