#include <bits/stdc++.h>
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, m;
std::cin >> n >> m;
std::string g[n];
for (int i = 0; i < n; ++i) {
std::cin >> g[i];
}
auto inside=[&](int x, int y) {
if (x >= 0 && x < n && y >= 0 && y < m && g[x][y] != '.')
return true;
return false;
};
std::vector<int> dx={1, -1, 0, 0}, dy={0, 0, 1, -1};
std::vector<std::vector<int>> d(n, std::vector<int>(m, 0));
std::deque<std::pair<int, int>> q;
q.push_back({0, 0});
d[0][0] = 1;
int ans = 0;
while (!q.empty()) {
auto u = q.front();
q.pop_front();
ans = std::max(ans, d[u.first][u.second]);
for (int i = 0; i < 4; ++i) {
int x = u.first + dx[i];
int y = u.second + dy[i];
if (!inside(x, y))
continue;
if (d[x][y])
continue;
if (g[x][y] == g[u.first][u.second]) {
d[x][y] = d[u.first][u.second];
q.push_front({x, y});
}
else {
d[x][y] = d[u.first][u.second] + 1;
q.push_back({x, y});
}
ans = std::max(ans, d[x][y]);
}
}
std::cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
1612 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
6 ms |
1320 KB |
Output is correct |
5 |
Correct |
2 ms |
716 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
588 KB |
Output is correct |
11 |
Correct |
2 ms |
588 KB |
Output is correct |
12 |
Correct |
4 ms |
716 KB |
Output is correct |
13 |
Correct |
2 ms |
716 KB |
Output is correct |
14 |
Correct |
2 ms |
716 KB |
Output is correct |
15 |
Correct |
9 ms |
1484 KB |
Output is correct |
16 |
Correct |
11 ms |
1484 KB |
Output is correct |
17 |
Correct |
7 ms |
1484 KB |
Output is correct |
18 |
Correct |
6 ms |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
716 KB |
Output is correct |
2 |
Correct |
41 ms |
8152 KB |
Output is correct |
3 |
Correct |
209 ms |
80872 KB |
Output is correct |
4 |
Correct |
67 ms |
18980 KB |
Output is correct |
5 |
Correct |
167 ms |
45464 KB |
Output is correct |
6 |
Correct |
668 ms |
94484 KB |
Output is correct |
7 |
Correct |
2 ms |
716 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
716 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
38 ms |
8080 KB |
Output is correct |
14 |
Correct |
22 ms |
4848 KB |
Output is correct |
15 |
Correct |
16 ms |
5336 KB |
Output is correct |
16 |
Correct |
18 ms |
3532 KB |
Output is correct |
17 |
Correct |
100 ms |
20616 KB |
Output is correct |
18 |
Correct |
68 ms |
20252 KB |
Output is correct |
19 |
Correct |
58 ms |
19056 KB |
Output is correct |
20 |
Correct |
48 ms |
17528 KB |
Output is correct |
21 |
Correct |
128 ms |
47036 KB |
Output is correct |
22 |
Correct |
169 ms |
45564 KB |
Output is correct |
23 |
Correct |
202 ms |
39116 KB |
Output is correct |
24 |
Correct |
126 ms |
45852 KB |
Output is correct |
25 |
Correct |
410 ms |
80836 KB |
Output is correct |
26 |
Correct |
397 ms |
123868 KB |
Output is correct |
27 |
Correct |
554 ms |
107472 KB |
Output is correct |
28 |
Correct |
669 ms |
94520 KB |
Output is correct |
29 |
Correct |
656 ms |
92844 KB |
Output is correct |
30 |
Correct |
630 ms |
97148 KB |
Output is correct |
31 |
Correct |
588 ms |
52116 KB |
Output is correct |
32 |
Correct |
453 ms |
96124 KB |
Output is correct |