#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vector<int> >;
using pi = pair<int, int>;
using ll = long long;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define pb push_back
#define MOD 1e9 + 7
int h, w, ans = 1;
vector<string> snow(4000);
int dx[4] {1, -1, 0, 0}, dy[4] {0, 0, 1, -1};
vvi dist(4000, vi (4000));
bool isInside(int x, int y) {
return x >= 0 && x < h && y >= 0 && y < w && snow[x][y] != '.';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> h >> w;
for (int i = 0; i < h; i++) cin >> snow[i];
deque<pi> dq;
dq.push_front({0, 0});
dist[0][0] = 1;
while (!dq.empty()) {
auto curr = dq.front();
dq.pop_front();
ans = max(ans, dist[curr.f][curr.s]);
for (int i = 0; i < 4; i++) {
int x = curr.f + dx[i], y = curr.s + dy[i];
if (isInside(x, y) && dist[x][y] == 0) {
if (snow[x][y] == snow[curr.f][curr.s]) {
dist[x][y] = dist[curr.f][curr.s];
dq.push_front({x, y});
} else {
dist[x][y] = dist[curr.f][curr.s] + 1;
dq.push_back({x, y});
}
}
}
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
63572 KB |
Output is correct |
2 |
Correct |
29 ms |
63324 KB |
Output is correct |
3 |
Correct |
29 ms |
63172 KB |
Output is correct |
4 |
Correct |
44 ms |
63572 KB |
Output is correct |
5 |
Correct |
31 ms |
63316 KB |
Output is correct |
6 |
Correct |
30 ms |
63316 KB |
Output is correct |
7 |
Correct |
31 ms |
63176 KB |
Output is correct |
8 |
Correct |
29 ms |
63324 KB |
Output is correct |
9 |
Correct |
30 ms |
63228 KB |
Output is correct |
10 |
Correct |
32 ms |
63164 KB |
Output is correct |
11 |
Correct |
29 ms |
63324 KB |
Output is correct |
12 |
Correct |
32 ms |
63340 KB |
Output is correct |
13 |
Correct |
30 ms |
63320 KB |
Output is correct |
14 |
Correct |
30 ms |
63324 KB |
Output is correct |
15 |
Correct |
34 ms |
63832 KB |
Output is correct |
16 |
Correct |
35 ms |
63424 KB |
Output is correct |
17 |
Correct |
36 ms |
63572 KB |
Output is correct |
18 |
Correct |
33 ms |
63584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
63280 KB |
Output is correct |
2 |
Correct |
53 ms |
64752 KB |
Output is correct |
3 |
Correct |
141 ms |
80988 KB |
Output is correct |
4 |
Correct |
79 ms |
67264 KB |
Output is correct |
5 |
Correct |
93 ms |
73048 KB |
Output is correct |
6 |
Correct |
489 ms |
94708 KB |
Output is correct |
7 |
Correct |
33 ms |
63312 KB |
Output is correct |
8 |
Correct |
27 ms |
63320 KB |
Output is correct |
9 |
Correct |
27 ms |
63316 KB |
Output is correct |
10 |
Correct |
28 ms |
63312 KB |
Output is correct |
11 |
Correct |
27 ms |
63312 KB |
Output is correct |
12 |
Correct |
31 ms |
63316 KB |
Output is correct |
13 |
Correct |
53 ms |
64852 KB |
Output is correct |
14 |
Correct |
45 ms |
64336 KB |
Output is correct |
15 |
Correct |
37 ms |
64336 KB |
Output is correct |
16 |
Correct |
41 ms |
63836 KB |
Output is correct |
17 |
Correct |
94 ms |
67652 KB |
Output is correct |
18 |
Correct |
62 ms |
67412 KB |
Output is correct |
19 |
Correct |
69 ms |
67060 KB |
Output is correct |
20 |
Correct |
56 ms |
66900 KB |
Output is correct |
21 |
Correct |
94 ms |
73304 KB |
Output is correct |
22 |
Correct |
91 ms |
73040 KB |
Output is correct |
23 |
Correct |
157 ms |
71752 KB |
Output is correct |
24 |
Correct |
101 ms |
73040 KB |
Output is correct |
25 |
Correct |
182 ms |
80976 KB |
Output is correct |
26 |
Correct |
290 ms |
126668 KB |
Output is correct |
27 |
Correct |
387 ms |
107524 KB |
Output is correct |
28 |
Correct |
502 ms |
94688 KB |
Output is correct |
29 |
Correct |
489 ms |
92988 KB |
Output is correct |
30 |
Correct |
451 ms |
98292 KB |
Output is correct |
31 |
Correct |
341 ms |
74720 KB |
Output is correct |
32 |
Correct |
324 ms |
96192 KB |
Output is correct |