#include <iostream>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
int main() {
int h, w;
cin >> h >> w;
vector<vector<int>> v(h, vector<int>(w, -1));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
char c;
cin >> c;
switch (c) {
case 'F':
v[i][j] = 1;
break;
case 'R':
v[i][j] = 0;
break;
default:
v[i][j] = -1;
break;
}
}
}
deque<pair<int, int>> dq;
vector<vector<int>> dist(h, vector<int>(w));
dist[0][0] = 1;
dq.push_back({0, 0});
int ans = 0;
while (!dq.empty()) {
int x = dq.front().first;
int y = dq.front().second;
dq.pop_front();
ans = max(ans, dist[x][y]);
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if (nx < 0 || nx >= h || ny < 0 || ny >= w || dist[nx][ny] || v[nx][ny] == -1) continue;
if (v[x][y] == v[nx][ny]) {
dist[nx][ny] = dist[x][y];
dq.push_front({nx, ny});
} else {
dist[nx][ny] = dist[x][y] + 1;
dq.push_back({nx, ny});
}
}
}
cout << ans << '\n';
/*
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cout << dist[i][j] << ' ';
}
cout << endl;
}
*/
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
8 ms |
1884 KB |
Output is correct |
5 |
Correct |
4 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
452 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
528 KB |
Output is correct |
10 |
Correct |
4 ms |
860 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
6 ms |
1116 KB |
Output is correct |
13 |
Correct |
4 ms |
1192 KB |
Output is correct |
14 |
Correct |
4 ms |
1112 KB |
Output is correct |
15 |
Correct |
14 ms |
2704 KB |
Output is correct |
16 |
Correct |
15 ms |
2396 KB |
Output is correct |
17 |
Correct |
12 ms |
2392 KB |
Output is correct |
18 |
Correct |
8 ms |
2048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1112 KB |
Output is correct |
2 |
Correct |
83 ms |
14120 KB |
Output is correct |
3 |
Correct |
729 ms |
141396 KB |
Output is correct |
4 |
Correct |
157 ms |
33364 KB |
Output is correct |
5 |
Correct |
379 ms |
79780 KB |
Output is correct |
6 |
Correct |
939 ms |
155840 KB |
Output is correct |
7 |
Correct |
3 ms |
1116 KB |
Output is correct |
8 |
Correct |
3 ms |
1116 KB |
Output is correct |
9 |
Correct |
4 ms |
708 KB |
Output is correct |
10 |
Correct |
1 ms |
860 KB |
Output is correct |
11 |
Correct |
2 ms |
1076 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
72 ms |
14124 KB |
Output is correct |
14 |
Correct |
44 ms |
8272 KB |
Output is correct |
15 |
Correct |
38 ms |
9044 KB |
Output is correct |
16 |
Correct |
32 ms |
5968 KB |
Output is correct |
17 |
Correct |
202 ms |
36176 KB |
Output is correct |
18 |
Correct |
167 ms |
35668 KB |
Output is correct |
19 |
Correct |
165 ms |
33584 KB |
Output is correct |
20 |
Correct |
134 ms |
30800 KB |
Output is correct |
21 |
Correct |
363 ms |
82456 KB |
Output is correct |
22 |
Correct |
358 ms |
79696 KB |
Output is correct |
23 |
Correct |
411 ms |
68704 KB |
Output is correct |
24 |
Correct |
382 ms |
80468 KB |
Output is correct |
25 |
Correct |
700 ms |
141508 KB |
Output is correct |
26 |
Correct |
576 ms |
158560 KB |
Output is correct |
27 |
Correct |
1132 ms |
167168 KB |
Output is correct |
28 |
Correct |
901 ms |
155892 KB |
Output is correct |
29 |
Correct |
899 ms |
153148 KB |
Output is correct |
30 |
Correct |
827 ms |
157076 KB |
Output is correct |
31 |
Correct |
655 ms |
91072 KB |
Output is correct |
32 |
Correct |
750 ms |
165636 KB |
Output is correct |