#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
constexpr array<int, 4> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1};
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n, m; cin >> n >> m;
vector<string> grid(n);
for (string &s: grid) cin >> s;
function<bool(int, int)> isValid = [&] (int x, int y) {
return 0 <= x and x < n and 0 <= y and y < m and grid[x][y] != '.';
};
int ans = 0;
vector<vector<int>> dist(n, vector<int>(m, -1));
dist[0][0] = 1;
deque<array<int, 2>> q; // {x, y}
q.push_back({0, 0});
while (not q.empty()) {
auto [x0, y0] = q.front(); q.pop_front();
ans = max(ans, dist[x0][y0]);
for (int i = 0; i < 4; i++) {
int x = x0 + dx[i], y = y0 + dy[i];
if (not isValid(x, y) or dist[x][y] != -1) continue;
if (grid[x][y] != grid[x0][y0]) {
dist[x][y] = dist[x0][y0] + 1;
q.push_back({x, y});
} else {
dist[x][y] = dist[x0][y0];
q.push_front({x, y});
}
}
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
1748 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
8 ms |
1408 KB |
Output is correct |
5 |
Correct |
3 ms |
800 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
724 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
6 ms |
852 KB |
Output is correct |
13 |
Correct |
2 ms |
712 KB |
Output is correct |
14 |
Correct |
3 ms |
716 KB |
Output is correct |
15 |
Correct |
10 ms |
1748 KB |
Output is correct |
16 |
Correct |
14 ms |
1852 KB |
Output is correct |
17 |
Correct |
8 ms |
1780 KB |
Output is correct |
18 |
Correct |
8 ms |
1492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
43 ms |
8916 KB |
Output is correct |
3 |
Correct |
214 ms |
81660 KB |
Output is correct |
4 |
Correct |
64 ms |
19740 KB |
Output is correct |
5 |
Correct |
192 ms |
46244 KB |
Output is correct |
6 |
Correct |
786 ms |
95628 KB |
Output is correct |
7 |
Correct |
2 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
3 ms |
644 KB |
Output is correct |
10 |
Correct |
1 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
752 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
45 ms |
8916 KB |
Output is correct |
14 |
Correct |
31 ms |
5496 KB |
Output is correct |
15 |
Correct |
18 ms |
6064 KB |
Output is correct |
16 |
Correct |
28 ms |
3920 KB |
Output is correct |
17 |
Correct |
144 ms |
21384 KB |
Output is correct |
18 |
Correct |
76 ms |
21104 KB |
Output is correct |
19 |
Correct |
65 ms |
19744 KB |
Output is correct |
20 |
Correct |
61 ms |
18312 KB |
Output is correct |
21 |
Correct |
141 ms |
47808 KB |
Output is correct |
22 |
Correct |
185 ms |
46240 KB |
Output is correct |
23 |
Correct |
215 ms |
39896 KB |
Output is correct |
24 |
Correct |
148 ms |
46624 KB |
Output is correct |
25 |
Correct |
429 ms |
81664 KB |
Output is correct |
26 |
Correct |
437 ms |
112916 KB |
Output is correct |
27 |
Correct |
587 ms |
116188 KB |
Output is correct |
28 |
Correct |
685 ms |
95688 KB |
Output is correct |
29 |
Correct |
696 ms |
93224 KB |
Output is correct |
30 |
Correct |
653 ms |
99224 KB |
Output is correct |
31 |
Correct |
525 ms |
52924 KB |
Output is correct |
32 |
Correct |
511 ms |
105884 KB |
Output is correct |