#include <iostream>
#include <string>
#include <deque>
using namespace std;
const int MAXN = 4002;
int cx[4] = {+1, 0, -1, 0};
int cy[4] = { 0,-1, 0,+1};
string s[MAXN];
int dist[MAXN][MAXN];
bool valid(int x, int y) { return (s[x][y] == 'R' || s[x][y] == 'F'); }
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int H, W; cin >> H >> W;
s[0] = string(W+1, '#');
for (int i = 1; i <= H; i++) {
cin >> s[i];
s[i] = '#' + s[i] + '#';
}
s[H+1] = string(W+1, '#');
deque<pair<int, int> > dq;
dq.push_front({1, 1});
dist[1][1] = 1;
int sol = 0;
while (!dq.empty()) {
pair<int, int> tp = dq.front(); dq.pop_front();
int x = tp.first, y = tp.second;
for (int dir = 0; dir < 4; dir++) {
int nx = x+cx[dir];
int ny = y+cy[dir];
if (valid(nx, ny) && dist[nx][ny] == 0) {
dist[nx][ny] = dist[x][y] + (s[x][y] != s[nx][ny]);
sol = max(sol, dist[nx][ny]);
if (s[x][y] != s[nx][ny]) {
dq.push_back({nx, ny});
} else {
dq.push_front({nx, ny});
}
}
}
}
cout << sol;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
4188 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
588 KB |
Output is correct |
4 |
Correct |
9 ms |
3676 KB |
Output is correct |
5 |
Correct |
2 ms |
2140 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
860 KB |
Output is correct |
10 |
Correct |
2 ms |
1884 KB |
Output is correct |
11 |
Correct |
2 ms |
1684 KB |
Output is correct |
12 |
Correct |
5 ms |
2384 KB |
Output is correct |
13 |
Correct |
2 ms |
2140 KB |
Output is correct |
14 |
Correct |
2 ms |
2140 KB |
Output is correct |
15 |
Correct |
9 ms |
3892 KB |
Output is correct |
16 |
Correct |
12 ms |
4188 KB |
Output is correct |
17 |
Correct |
7 ms |
3932 KB |
Output is correct |
18 |
Correct |
6 ms |
3676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
16216 KB |
Output is correct |
2 |
Correct |
35 ms |
13452 KB |
Output is correct |
3 |
Correct |
163 ms |
89268 KB |
Output is correct |
4 |
Correct |
56 ms |
32852 KB |
Output is correct |
5 |
Correct |
83 ms |
59480 KB |
Output is correct |
6 |
Correct |
535 ms |
123820 KB |
Output is correct |
7 |
Correct |
8 ms |
16732 KB |
Output is correct |
8 |
Correct |
7 ms |
15964 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
7 ms |
16472 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
32 ms |
13536 KB |
Output is correct |
14 |
Correct |
18 ms |
8796 KB |
Output is correct |
15 |
Correct |
12 ms |
11356 KB |
Output is correct |
16 |
Correct |
16 ms |
5568 KB |
Output is correct |
17 |
Correct |
82 ms |
28496 KB |
Output is correct |
18 |
Correct |
46 ms |
35412 KB |
Output is correct |
19 |
Correct |
58 ms |
32344 KB |
Output is correct |
20 |
Correct |
43 ms |
25160 KB |
Output is correct |
21 |
Correct |
102 ms |
57236 KB |
Output is correct |
22 |
Correct |
78 ms |
57424 KB |
Output is correct |
23 |
Correct |
155 ms |
47856 KB |
Output is correct |
24 |
Correct |
93 ms |
53584 KB |
Output is correct |
25 |
Correct |
178 ms |
106836 KB |
Output is correct |
26 |
Correct |
275 ms |
138848 KB |
Output is correct |
27 |
Correct |
377 ms |
139584 KB |
Output is correct |
28 |
Correct |
511 ms |
123856 KB |
Output is correct |
29 |
Correct |
507 ms |
121984 KB |
Output is correct |
30 |
Correct |
465 ms |
127684 KB |
Output is correct |
31 |
Correct |
448 ms |
71172 KB |
Output is correct |
32 |
Correct |
320 ms |
112948 KB |
Output is correct |