#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
void fastIO() {ios_base::sync_with_stdio(false), cin.tie(nullptr);}
const int INF = 1e9;
const int MAXR = 4001;
const int MAXC = 4001;
const int dr[4] = {0, 1, 0, -1}, dc[4] = {1, 0, -1, 0};
int R, C;
char grid[MAXR][MAXC];
int dist[MAXR][MAXC];
int ans = 0;
signed main() {
fastIO();
cin >> R >> C;
char on;
for (int r = 0; r < R; r++) {
for (int c = 0; c < C; c++) {
cin >> on;
grid[r][c] = on;
}
}
if (grid[0][0] == '.') {
cout << "0\n";
return 0;
}
for (int r = 0; r < R; r++) {
for (int c = 0; c < C; c++) {
dist[r][c] = INF;
}
}
dist[0][0] = 1;
deque<pair<int, int>> DQ;
DQ.push_back({0, 0});
pair<int, int> p;
int r, c;
while (!DQ.empty()) {
p = DQ.front();
r = p.f, c = p.s;
DQ.pop_front();
for (int i = 0; i < 4; i++) {
if (0 > r + dr[i] || r + dr[i] >= R || 0 > c + dc[i] || c + dc[i] >= C) continue;
if (grid[r + dr[i]][c + dc[i]] == '.') continue;
if (grid[r + dr[i]][c + dc[i]] == grid[r][c] && dist[r + dr[i]][c + dc[i]] > dist[r][c]) {
dist[r + dr[i]][c + dc[i]] = dist[r][c];
DQ.push_front({r + dr[i], c + dc[i]});
}
else if (dist[r + dr[i]][c + dc[i]] > dist[r][c] + 1) {
dist[r + dr[i]][c + dc[i]] = dist[r][c] + 1;
DQ.push_back({r + dr[i], c + dc[i]});
}
ans = max(ans, dist[r + dr[i]][c + dc[i]]);
}
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
12892 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
6 ms |
13148 KB |
Output is correct |
5 |
Correct |
2 ms |
8796 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
6680 KB |
Output is correct |
11 |
Correct |
2 ms |
6748 KB |
Output is correct |
12 |
Correct |
4 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
3 ms |
8796 KB |
Output is correct |
15 |
Correct |
9 ms |
13088 KB |
Output is correct |
16 |
Correct |
12 ms |
13148 KB |
Output is correct |
17 |
Correct |
7 ms |
12888 KB |
Output is correct |
18 |
Correct |
7 ms |
13148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
59740 KB |
Output is correct |
2 |
Correct |
36 ms |
28764 KB |
Output is correct |
3 |
Correct |
208 ms |
94396 KB |
Output is correct |
4 |
Correct |
54 ms |
45140 KB |
Output is correct |
5 |
Correct |
128 ms |
70332 KB |
Output is correct |
6 |
Correct |
523 ms |
108732 KB |
Output is correct |
7 |
Correct |
6 ms |
57180 KB |
Output is correct |
8 |
Correct |
7 ms |
58128 KB |
Output is correct |
9 |
Correct |
3 ms |
4700 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
6 ms |
58500 KB |
Output is correct |
12 |
Correct |
2 ms |
6488 KB |
Output is correct |
13 |
Correct |
30 ms |
28748 KB |
Output is correct |
14 |
Correct |
19 ms |
21852 KB |
Output is correct |
15 |
Correct |
13 ms |
23900 KB |
Output is correct |
16 |
Correct |
16 ms |
11340 KB |
Output is correct |
17 |
Correct |
83 ms |
47652 KB |
Output is correct |
18 |
Correct |
59 ms |
47444 KB |
Output is correct |
19 |
Correct |
50 ms |
45140 KB |
Output is correct |
20 |
Correct |
46 ms |
40788 KB |
Output is correct |
21 |
Correct |
132 ms |
72388 KB |
Output is correct |
22 |
Correct |
136 ms |
70460 KB |
Output is correct |
23 |
Correct |
154 ms |
59324 KB |
Output is correct |
24 |
Correct |
114 ms |
72276 KB |
Output is correct |
25 |
Correct |
277 ms |
94260 KB |
Output is correct |
26 |
Correct |
350 ms |
133576 KB |
Output is correct |
27 |
Correct |
423 ms |
120028 KB |
Output is correct |
28 |
Correct |
516 ms |
108620 KB |
Output is correct |
29 |
Correct |
500 ms |
105916 KB |
Output is correct |
30 |
Correct |
452 ms |
112148 KB |
Output is correct |
31 |
Correct |
407 ms |
75208 KB |
Output is correct |
32 |
Correct |
381 ms |
118536 KB |
Output is correct |