#include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
char graph[n][m];
for (int i = 0; i < n; i ++) for (int j = 0; j < m; j ++) cin >> graph[i][j];
int cnt[n][m];
int inf = 0x3f3f3f3f;
memset(cnt, inf, sizeof(cnt));
cnt[0][0] = 1;
deque<pair<int, int> > que;
que.push_front(make_pair(0, 0));
while(! que.empty()){
pair<int, int> p = que.front();
que.pop_front();
for (int i = 0; i < 4; i ++){
int x = p.first + dx[i];
int y = p.second + dy[i];
if (x >= 0 && x < n && y >= 0 && y < m && cnt[x][y] > cnt[p.first][p.second] + (graph[x][y] != graph[p.first][p.second]) && (graph[x][y] == 'R' || graph[x][y] == 'F')){
if (graph[x][y] != graph[p.first][p.second]){
cnt[x][y] = cnt[p.first][p.second] + 1;
que.push_back(make_pair(x, y));
} else{
cnt[x][y] = cnt[p.first][p.second];
que.push_front(make_pair(x, y));
}
}
}
}
int ans = 0;
for (int i = 0; i < n; i ++){
for (int j = 0; j < m; j ++) if (cnt[i][j] != inf) ans = max(ans, cnt[i][j]);
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
1484 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
9 ms |
1228 KB |
Output is correct |
5 |
Correct |
4 ms |
644 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
3 ms |
588 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
6 ms |
716 KB |
Output is correct |
13 |
Correct |
4 ms |
716 KB |
Output is correct |
14 |
Correct |
4 ms |
716 KB |
Output is correct |
15 |
Correct |
14 ms |
1484 KB |
Output is correct |
16 |
Correct |
19 ms |
1484 KB |
Output is correct |
17 |
Correct |
11 ms |
1484 KB |
Output is correct |
18 |
Correct |
9 ms |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
65 ms |
7840 KB |
Output is correct |
3 |
Correct |
571 ms |
78552 KB |
Output is correct |
4 |
Correct |
126 ms |
18660 KB |
Output is correct |
5 |
Correct |
287 ms |
44308 KB |
Output is correct |
6 |
Correct |
985 ms |
92176 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
2 ms |
460 KB |
Output is correct |
9 |
Correct |
3 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
70 ms |
7964 KB |
Output is correct |
14 |
Correct |
39 ms |
4684 KB |
Output is correct |
15 |
Correct |
33 ms |
5068 KB |
Output is correct |
16 |
Correct |
31 ms |
3360 KB |
Output is correct |
17 |
Correct |
171 ms |
20140 KB |
Output is correct |
18 |
Correct |
139 ms |
19828 KB |
Output is correct |
19 |
Correct |
117 ms |
18564 KB |
Output is correct |
20 |
Correct |
136 ms |
17092 KB |
Output is correct |
21 |
Correct |
286 ms |
45748 KB |
Output is correct |
22 |
Correct |
290 ms |
44320 KB |
Output is correct |
23 |
Correct |
364 ms |
38332 KB |
Output is correct |
24 |
Correct |
277 ms |
44868 KB |
Output is correct |
25 |
Correct |
681 ms |
78532 KB |
Output is correct |
26 |
Correct |
636 ms |
110424 KB |
Output is correct |
27 |
Correct |
850 ms |
105268 KB |
Output is correct |
28 |
Correct |
974 ms |
92196 KB |
Output is correct |
29 |
Correct |
978 ms |
90684 KB |
Output is correct |
30 |
Correct |
912 ms |
94932 KB |
Output is correct |
31 |
Correct |
708 ms |
50868 KB |
Output is correct |
32 |
Correct |
774 ms |
93820 KB |
Output is correct |