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;
#define DEBUG(x) //x
#define A(x) DEBUG(assert(x))
#define PRINT(x) DEBUG(cerr << x)
#define PV(x) DEBUG(cerr << #x << " = " << x << endl)
#define PAR(x) DEBUG(PRINT(#x << " = { "); for (auto y : x) PRINT(y << ' '); PRINT("}\n");)
#define PAR2(x) DEBUG(PRINT(#x << " = { "); for (auto [y, z] : x) PRINT(y << ',' << z << " "); PRINT("}\n");)
using i64 = int64_t;
const int INF = 1000000007; //998244353
struct S {
int n, m;
array<array<char, 4000>, 4000> a{};
array<array<bool, 4000>, 4000> visited{};
void run() {
cin >> n >> m;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> a[i][j];
}
}
}
void bfs() {
priority_queue<array<int, 3>> q; // {d, x, y}
q.push({-1, 0, 0});
visited[0][0] = true;
int ans = 1;
while (!q.empty()) {
int x = q.top()[1];
int y = q.top()[2];
int d = -q.top()[0];
q.pop();
ans = max(ans, d);
PV(x); PV(y); PV(d); PRINT('\n');
char c = a[x][y];
int a1 = ok(x, y + 1, c);
if (a1 >= 0) {
q.push({-a1 - d, x, y + 1});
visited[x][y + 1] = true;
}
int a2 = ok(x, y - 1, c);
if (a2 >= 0) {
q.push({-a2 - d, x, y - 1});
visited[x][y - 1] = true;
}
int a3 = ok(x + 1, y, c);
if (a3 >= 0) {
q.push({-a3 - d, x + 1, y});
visited[x + 1][y] = true;
}
int a4 = ok(x - 1, y, c);
if (a4 >= 0) {
q.push({-a4 - d, x - 1, y});
visited[x - 1][y] = true;
}
}
cout << ans << '\n';
}
int ok(int x, int y, char c) {
if (x < 0 || x >= n) return -1;
if (y < 0 || y >= m) return -1;
if (visited[x][y]) return -1;
if (a[x][y] == '.') return -1;
return (a[x][y] == c ? 0 : 1);
}
};
int main() {
ios::sync_with_stdio(0); cin.tie(0);
auto sol = make_unique<S>();
sol->run();
sol->bfs();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |