#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define LOG(...) cerr << "[" << #__VA_ARGS__ << "]: " << repr(__VA_ARGS__) << endl;
#define MSG(args) cerr << args << "\n";
#define debug(x) x
#else
#define LOG(...)
#define MSG(args)
#define debug(x)
#endif
#define mp make_pair
#define pb push_back
#define sz(x) (int((x).size()))
#define ms(x, v) memset((x), v, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define endl '\n'
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<pii>;
using vvi = vector<vi>;
const int oo = 0x7f7f7f7f;
int h, w, d[4001][4001], dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
string grid[4001];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ms(d, 0x7f);
cin >> h >> w;
for (int i = 0; i < h; ++i) {
cin >> grid[i];
}
d[0][0] = 1;
deque<pii> q;
q.push_back({0, 0});
auto valid = [](int x, int y) {
return x >= 0 && x < h && y >= 0 && y < w && grid[x][y] != '.';
};
int ans = 0;
while (!q.empty()) {
auto u = q.front(); q.pop_front();
ans = max(ans, d[u.first][u.second]);
for (int i = 0; i < 4; ++i) {
int x = u.first + dx[i], y = u.second + dy[i];
if (valid(x, y) && d[x][y] == oo) {
if (grid[x][y] == grid[u.first][u.second]) {
d[x][y] = d[u.first][u.second];
q.push_front({x, y});
} else {
d[x][y] = 1 + d[u.first][u.second];
q.push_back({x, y});
}
}
}
}
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
63600 KB |
Output is correct |
2 |
Correct |
27 ms |
63004 KB |
Output is correct |
3 |
Correct |
31 ms |
63076 KB |
Output is correct |
4 |
Correct |
32 ms |
63616 KB |
Output is correct |
5 |
Correct |
30 ms |
63212 KB |
Output is correct |
6 |
Correct |
26 ms |
62992 KB |
Output is correct |
7 |
Correct |
27 ms |
63052 KB |
Output is correct |
8 |
Correct |
27 ms |
63020 KB |
Output is correct |
9 |
Correct |
28 ms |
63088 KB |
Output is correct |
10 |
Correct |
28 ms |
63172 KB |
Output is correct |
11 |
Correct |
30 ms |
63200 KB |
Output is correct |
12 |
Correct |
31 ms |
63232 KB |
Output is correct |
13 |
Correct |
32 ms |
63180 KB |
Output is correct |
14 |
Correct |
28 ms |
63180 KB |
Output is correct |
15 |
Correct |
34 ms |
63476 KB |
Output is correct |
16 |
Correct |
36 ms |
63592 KB |
Output is correct |
17 |
Correct |
33 ms |
63572 KB |
Output is correct |
18 |
Correct |
32 ms |
63564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
63104 KB |
Output is correct |
2 |
Correct |
62 ms |
66256 KB |
Output is correct |
3 |
Correct |
197 ms |
94424 KB |
Output is correct |
4 |
Correct |
77 ms |
70692 KB |
Output is correct |
5 |
Correct |
177 ms |
81600 KB |
Output is correct |
6 |
Correct |
570 ms |
108484 KB |
Output is correct |
7 |
Correct |
31 ms |
63052 KB |
Output is correct |
8 |
Correct |
28 ms |
63088 KB |
Output is correct |
9 |
Correct |
28 ms |
63156 KB |
Output is correct |
10 |
Correct |
27 ms |
63124 KB |
Output is correct |
11 |
Correct |
33 ms |
63084 KB |
Output is correct |
12 |
Correct |
28 ms |
63092 KB |
Output is correct |
13 |
Correct |
58 ms |
66232 KB |
Output is correct |
14 |
Correct |
44 ms |
64836 KB |
Output is correct |
15 |
Correct |
41 ms |
65016 KB |
Output is correct |
16 |
Correct |
43 ms |
64412 KB |
Output is correct |
17 |
Correct |
110 ms |
71320 KB |
Output is correct |
18 |
Correct |
85 ms |
71116 KB |
Output is correct |
19 |
Correct |
76 ms |
70728 KB |
Output is correct |
20 |
Correct |
65 ms |
70056 KB |
Output is correct |
21 |
Correct |
124 ms |
82116 KB |
Output is correct |
22 |
Correct |
166 ms |
81588 KB |
Output is correct |
23 |
Correct |
199 ms |
79080 KB |
Output is correct |
24 |
Correct |
133 ms |
81724 KB |
Output is correct |
25 |
Correct |
392 ms |
94496 KB |
Output is correct |
26 |
Correct |
348 ms |
138608 KB |
Output is correct |
27 |
Correct |
469 ms |
129204 KB |
Output is correct |
28 |
Correct |
581 ms |
108736 KB |
Output is correct |
29 |
Correct |
599 ms |
106404 KB |
Output is correct |
30 |
Correct |
526 ms |
113464 KB |
Output is correct |
31 |
Correct |
483 ms |
84676 KB |
Output is correct |
32 |
Correct |
412 ms |
118960 KB |
Output is correct |