#include <bits/stdc++.h>
using namespace std;
#define int long long
const int INF = LLONG_MAX;
vector<string> grid;
vector<int> dx = {1, -1, 0, 0};
vector<int> dy = {0, 0, 1, -1};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen("input.in", "r", stdin);
// freopen("output.out", "w", stdout);
int h, w;
cin >> h >> w;
grid.resize(h);
for (string &x : grid) cin >> x;
vector<vector<int>> dist(h, vector<int>(w, INF));
deque<pair<int, int>> q;
q.push_back({0, 0});
dist[0][0] = 1;
while (!q.empty()) {
auto [x, y] = q.front();
q.pop_front();
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx < h && ny >= 0 && ny < w && grid[nx][ny] != '.' && dist[nx][ny] == INF) {
if (grid[nx][ny] == grid[x][y]) {
dist[nx][ny] = dist[x][y];
q.push_front({nx, ny});
} else {
dist[nx][ny] = dist[x][y] + 1;
q.push_back({nx, ny});
}
}
}
}
int maxi = -INF;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (dist[i][j] != INF) {
maxi = max(maxi, dist[i][j]);
}
}
}
cout << maxi << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2908 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
5 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
524 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
1112 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
3 ms |
1372 KB |
Output is correct |
13 |
Correct |
2 ms |
1116 KB |
Output is correct |
14 |
Correct |
2 ms |
1116 KB |
Output is correct |
15 |
Correct |
8 ms |
2908 KB |
Output is correct |
16 |
Correct |
9 ms |
2992 KB |
Output is correct |
17 |
Correct |
6 ms |
2640 KB |
Output is correct |
18 |
Correct |
6 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
37 ms |
15816 KB |
Output is correct |
3 |
Correct |
180 ms |
159240 KB |
Output is correct |
4 |
Correct |
62 ms |
37520 KB |
Output is correct |
5 |
Correct |
131 ms |
89568 KB |
Output is correct |
6 |
Correct |
532 ms |
186356 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
1112 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
720 KB |
Output is correct |
11 |
Correct |
1 ms |
1140 KB |
Output is correct |
12 |
Correct |
1 ms |
692 KB |
Output is correct |
13 |
Correct |
32 ms |
15984 KB |
Output is correct |
14 |
Correct |
22 ms |
9188 KB |
Output is correct |
15 |
Correct |
17 ms |
10332 KB |
Output is correct |
16 |
Correct |
14 ms |
6744 KB |
Output is correct |
17 |
Correct |
87 ms |
40532 KB |
Output is correct |
18 |
Correct |
50 ms |
39824 KB |
Output is correct |
19 |
Correct |
60 ms |
37460 KB |
Output is correct |
20 |
Correct |
42 ms |
34388 KB |
Output is correct |
21 |
Correct |
116 ms |
92544 KB |
Output is correct |
22 |
Correct |
132 ms |
89544 KB |
Output is correct |
23 |
Correct |
172 ms |
77056 KB |
Output is correct |
24 |
Correct |
119 ms |
90444 KB |
Output is correct |
25 |
Correct |
241 ms |
159292 KB |
Output is correct |
26 |
Correct |
267 ms |
222276 KB |
Output is correct |
27 |
Correct |
357 ms |
212284 KB |
Output is correct |
28 |
Correct |
486 ms |
186544 KB |
Output is correct |
29 |
Correct |
490 ms |
183204 KB |
Output is correct |
30 |
Correct |
417 ms |
191448 KB |
Output is correct |
31 |
Correct |
367 ms |
102736 KB |
Output is correct |
32 |
Correct |
302 ms |
189788 KB |
Output is correct |