/**
* In the name of GOD
* author : Behradm
**/
#include "bits/stdc++.h"
using namespace std;
#define sz(x) (int) ((x).size())
const int N = 4e3 + 5;
char s[N][N];
int tag[N][N];
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int n, m;
bool Good(int x, int y) {
return (0 <= x && x < n && 0 <= y && y < m && s[x][y] != '.');
}
void dfs(int i, int j, int c) {
tag[i][j] = c;
for (int k = 0; k < 4; k++) {
int x = i + dx[k], y = j + dy[k];
if (Good(x, y) && s[x][y] == s[i][j] && tag[x][y] == -1) {
dfs(x, y, c);
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> s[i][j];
tag[i][j] = -1;
}
}
int c = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (tag[i][j] == -1 && s[i][j] != '.')
dfs(i, j, c++);
}
}
//[0, c - 1]
vector<vector<int>> g(c);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (s[i][j] != 'R')
continue;
for (int k = 0; k < 4; k++) {
int x = i + dx[k], y = j + dy[k];
if (Good(x, y) && (s[i][j] == 'R' && s[x][y] == 'F')) {
int p = tag[i][j], q = tag[x][y];
g[p].push_back(q);
g[q].push_back(p);
}
}
}
}
for (int i = 0; i < c; i++) {
sort(g[i].begin(), g[i].end());
g[i].resize(unique(g[i].begin(), g[i].end()) - g[i].begin());
}
vector<int> h(c);
queue<int> q;
q.push(0), h[0] = 1;
int ans = 1;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v : g[u]) {
if (h[v] == 0) {
h[v] = h[u] + 1;
ans = max(ans, h[v]);
q.push(v);
}
}
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
9080 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
14 ms |
6228 KB |
Output is correct |
5 |
Correct |
6 ms |
3360 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
852 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
6 ms |
3156 KB |
Output is correct |
11 |
Correct |
4 ms |
2644 KB |
Output is correct |
12 |
Correct |
14 ms |
4332 KB |
Output is correct |
13 |
Correct |
6 ms |
3412 KB |
Output is correct |
14 |
Correct |
6 ms |
3412 KB |
Output is correct |
15 |
Correct |
37 ms |
8524 KB |
Output is correct |
16 |
Correct |
47 ms |
9024 KB |
Output is correct |
17 |
Correct |
24 ms |
7380 KB |
Output is correct |
18 |
Correct |
13 ms |
6180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
31408 KB |
Output is correct |
2 |
Correct |
123 ms |
30572 KB |
Output is correct |
3 |
Correct |
673 ms |
172324 KB |
Output is correct |
4 |
Correct |
168 ms |
45476 KB |
Output is correct |
5 |
Correct |
695 ms |
323428 KB |
Output is correct |
6 |
Correct |
1188 ms |
182088 KB |
Output is correct |
7 |
Correct |
16 ms |
32724 KB |
Output is correct |
8 |
Correct |
15 ms |
31444 KB |
Output is correct |
9 |
Correct |
5 ms |
1344 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
15 ms |
32084 KB |
Output is correct |
12 |
Correct |
3 ms |
2260 KB |
Output is correct |
13 |
Correct |
144 ms |
30516 KB |
Output is correct |
14 |
Correct |
76 ms |
19276 KB |
Output is correct |
15 |
Correct |
56 ms |
20628 KB |
Output is correct |
16 |
Correct |
60 ms |
13112 KB |
Output is correct |
17 |
Correct |
308 ms |
68604 KB |
Output is correct |
18 |
Correct |
220 ms |
64572 KB |
Output is correct |
19 |
Correct |
183 ms |
45472 KB |
Output is correct |
20 |
Correct |
166 ms |
49360 KB |
Output is correct |
21 |
Correct |
393 ms |
117120 KB |
Output is correct |
22 |
Correct |
662 ms |
323516 KB |
Output is correct |
23 |
Correct |
568 ms |
120268 KB |
Output is correct |
24 |
Correct |
431 ms |
149028 KB |
Output is correct |
25 |
Correct |
1061 ms |
213240 KB |
Output is correct |
26 |
Correct |
1051 ms |
1027820 KB |
Output is correct |
27 |
Correct |
956 ms |
302432 KB |
Output is correct |
28 |
Correct |
1392 ms |
181888 KB |
Output is correct |
29 |
Correct |
1212 ms |
159576 KB |
Output is correct |
30 |
Correct |
989 ms |
225620 KB |
Output is correct |
31 |
Correct |
1588 ms |
229104 KB |
Output is correct |
32 |
Correct |
731 ms |
427000 KB |
Output is correct |