#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 4e3 + 2;
int a[N][N], c[N][N], n, m, iden;
bool Can(int x, int y) {return 0 <= x && x < n && 0 <= y && y < m && c[x][y] == -1;}
void Dfs(int i, int j) {
c[i][j] = iden;
if (Can(i - 1, j) && a[i - 1][j] == a[i][j]) Dfs(i - 1, j);
if (Can(i, j - 1) && a[i][j - 1] == a[i][j]) Dfs(i, j - 1);
if (Can(i + 1, j) && a[i + 1][j] == a[i][j]) Dfs(i + 1, j);
if (Can(i, j + 1) && a[i][j + 1] == a[i][j]) Dfs(i, j + 1);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
string s; cin >> s;
for (int j = 0; j < m; j++) {
c[i][j] = -1;
if (s[j] == 'F') a[i][j] = 1;
if (s[j] == 'R') a[i][j] = 2;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (c[i][j] == -1 && a[i][j] > 0) {
Dfs(i, j);
iden += 1;
}
}
}
vector<set<int>> g(iden);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (c[i][j] == -1) continue;
if (i < n - 1 && c[i + 1][j] != c[i][j] && c[i + 1][j] != -1) g[c[i][j]].insert(c[i + 1][j]);
if (j < m - 1 && c[i][j + 1] != c[i][j] && c[i][j + 1] != -1) g[c[i][j]].insert(c[i][j + 1]);
}
}
vector<int> dist(iden, 1e9);
queue<int> q;
dist[0] = 0;
q.push(0);
while (!q.empty()) {
int s = q.front(); q.pop();
for (int u : g[s]) {
if (dist[u] > dist[s] + 1) {
dist[u] = dist[s] + 1;
q.push(u);
}
}
}
int ans = 0;
for (int i = 0; i < iden; i++) {
smax(ans, dist[i] + 1);
}
cout << ans << en;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
27 ms |
12164 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
468 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
716 KB |
Output isn't correct |
4 |
Incorrect |
10 ms |
7892 KB |
Output isn't correct |
5 |
Incorrect |
4 ms |
4184 KB |
Output isn't correct |
6 |
Incorrect |
1 ms |
452 KB |
Output isn't correct |
7 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Incorrect |
1 ms |
1236 KB |
Output isn't correct |
10 |
Incorrect |
5 ms |
3664 KB |
Output isn't correct |
11 |
Correct |
3 ms |
3028 KB |
Output is correct |
12 |
Incorrect |
10 ms |
5460 KB |
Output isn't correct |
13 |
Incorrect |
4 ms |
4280 KB |
Output isn't correct |
14 |
Incorrect |
4 ms |
4148 KB |
Output isn't correct |
15 |
Incorrect |
22 ms |
11536 KB |
Output isn't correct |
16 |
Incorrect |
27 ms |
12092 KB |
Output isn't correct |
17 |
Incorrect |
16 ms |
10524 KB |
Output isn't correct |
18 |
Incorrect |
9 ms |
8020 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
32276 KB |
Output isn't correct |
2 |
Incorrect |
73 ms |
45244 KB |
Output isn't correct |
3 |
Incorrect |
397 ms |
279988 KB |
Output isn't correct |
4 |
Incorrect |
107 ms |
74300 KB |
Output isn't correct |
5 |
Incorrect |
477 ms |
528472 KB |
Output isn't correct |
6 |
Incorrect |
972 ms |
318636 KB |
Output isn't correct |
7 |
Incorrect |
16 ms |
33576 KB |
Output isn't correct |
8 |
Incorrect |
14 ms |
32204 KB |
Output isn't correct |
9 |
Incorrect |
3 ms |
1748 KB |
Output isn't correct |
10 |
Incorrect |
1 ms |
724 KB |
Output isn't correct |
11 |
Incorrect |
19 ms |
32724 KB |
Output isn't correct |
12 |
Incorrect |
2 ms |
2772 KB |
Output isn't correct |
13 |
Incorrect |
74 ms |
45136 KB |
Output isn't correct |
14 |
Incorrect |
42 ms |
28108 KB |
Output isn't correct |
15 |
Incorrect |
37 ms |
31080 KB |
Output isn't correct |
16 |
Incorrect |
37 ms |
19908 KB |
Output isn't correct |
17 |
Incorrect |
184 ms |
106444 KB |
Output isn't correct |
18 |
Incorrect |
131 ms |
105604 KB |
Output isn't correct |
19 |
Incorrect |
112 ms |
74572 KB |
Output isn't correct |
20 |
Incorrect |
93 ms |
75756 KB |
Output isn't correct |
21 |
Incorrect |
235 ms |
184288 KB |
Output isn't correct |
22 |
Incorrect |
496 ms |
528516 KB |
Output isn't correct |
23 |
Incorrect |
335 ms |
192464 KB |
Output isn't correct |
24 |
Incorrect |
258 ms |
232360 KB |
Output isn't correct |
25 |
Incorrect |
498 ms |
364580 KB |
Output isn't correct |
26 |
Runtime error |
559 ms |
1048576 KB |
Execution killed with signal 9 |
27 |
Correct |
791 ms |
684116 KB |
Output is correct |
28 |
Incorrect |
965 ms |
318528 KB |
Output isn't correct |
29 |
Incorrect |
868 ms |
295680 KB |
Output isn't correct |
30 |
Incorrect |
836 ms |
409528 KB |
Output isn't correct |
31 |
Incorrect |
1192 ms |
349128 KB |
Output isn't correct |
32 |
Incorrect |
807 ms |
683204 KB |
Output isn't correct |