/**
* Solution by 1egend (polarity.sh)
* Date: 2024-10-30
* Contest: BOI 2013
* Problem: tracks
**/
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ull unsigned long long
#define ll long long
const int MAX_N = 1e5 + 1;
const ll MOD = 1e9 + 7;
vector<pair<int, int>> cardinal = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
vector<vector<int>> grid;
vector<vector<int>> component;
void floodfill(int i, int j, int n, int l){
if (grid[i][j] != l || component[i][j] != 0){
return;
}
component[i][j] = n;
for (pair<int, int> t : cardinal){
floodfill(i + t.first, j + t.second, n, l);
}
}
void solve(){
int h, w; cin >> h >> w;
grid = vector<vector<int>>(h + 2, vector<int>(w + 2, 0));
component = grid;
for (int i = 0; i < h; i++){
string s; cin >> s;
for (int j = 0; j < w; j++){
char t = s[j];
if (t == 'F'){
grid[i + 1][j + 1] = 2;
} else if (t == 'R'){
grid[i + 1][j + 1] = 1;
}
}
}
int k = 1;
for (int i = 1; i < h + 1; i++){
for (int j = 1; j < w + 1; j++){
if (component[i][j] == 0 && grid[i][j] != 0){
floodfill(i, j, k, grid[i][j]);
k++;
}
}
}
vector<vector<int>> adj(k + 1);
map<pair<int, int>, int> edge;
for (int i = 1; i < h + 1; i++){
for (int j = 1; j < w + 1; j++){
int x = component[i][j];
for (pair<int, int> t : cardinal){
int y = component[i + t.first][j + t.second];
if (x != 0 && y != 0 && x != y && edge[{x, y}] == 0 && edge[{y,x}] == 0){
edge[{x, y}] = 1;
edge[{y, x}] = 1;
adj[x].pb(y);
adj[y].pb(x);
}
}
}
}
vector<int> dist(k + 1, -1);
int ans = 0;
queue<int> curr;
curr.push(1);
dist[1] = 0;
while(!curr.empty()){
int comp = curr.front();
curr.pop();
for (int node : adj[comp]){
if (dist[node] != -1){
continue;
}
dist[node] = dist[comp] + 1;
ans = max(ans, dist[node]);
curr.push(node);
}
}
cout << ans + 1 << endl;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
11592 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
13 ms |
2756 KB |
Output is correct |
5 |
Correct |
12 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
592 KB |
Output is correct |
10 |
Correct |
10 ms |
2652 KB |
Output is correct |
11 |
Correct |
4 ms |
1360 KB |
Output is correct |
12 |
Correct |
27 ms |
4424 KB |
Output is correct |
13 |
Correct |
10 ms |
2640 KB |
Output is correct |
14 |
Correct |
10 ms |
2640 KB |
Output is correct |
15 |
Correct |
64 ms |
10852 KB |
Output is correct |
16 |
Correct |
89 ms |
11592 KB |
Output is correct |
17 |
Correct |
48 ms |
10240 KB |
Output is correct |
18 |
Correct |
13 ms |
2896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2384 KB |
Output is correct |
2 |
Correct |
275 ms |
55012 KB |
Output is correct |
3 |
Correct |
1802 ms |
413784 KB |
Output is correct |
4 |
Correct |
411 ms |
80712 KB |
Output is correct |
5 |
Execution timed out |
2065 ms |
730208 KB |
Time limit exceeded |
6 |
Correct |
1820 ms |
266980 KB |
Output is correct |
7 |
Correct |
5 ms |
2128 KB |
Output is correct |
8 |
Correct |
5 ms |
2308 KB |
Output is correct |
9 |
Correct |
9 ms |
2384 KB |
Output is correct |
10 |
Correct |
2 ms |
1360 KB |
Output is correct |
11 |
Correct |
3 ms |
1752 KB |
Output is correct |
12 |
Correct |
7 ms |
2640 KB |
Output is correct |
13 |
Correct |
264 ms |
54348 KB |
Output is correct |
14 |
Correct |
155 ms |
31560 KB |
Output is correct |
15 |
Correct |
172 ms |
35656 KB |
Output is correct |
16 |
Correct |
131 ms |
25672 KB |
Output is correct |
17 |
Correct |
716 ms |
139840 KB |
Output is correct |
18 |
Correct |
756 ms |
134476 KB |
Output is correct |
19 |
Correct |
370 ms |
80076 KB |
Output is correct |
20 |
Correct |
371 ms |
94540 KB |
Output is correct |
21 |
Correct |
1075 ms |
251532 KB |
Output is correct |
22 |
Execution timed out |
2122 ms |
758332 KB |
Time limit exceeded |
23 |
Correct |
1443 ms |
274044 KB |
Output is correct |
24 |
Correct |
1086 ms |
354520 KB |
Output is correct |
25 |
Execution timed out |
2066 ms |
473472 KB |
Time limit exceeded |
26 |
Correct |
1103 ms |
868936 KB |
Output is correct |
27 |
Correct |
753 ms |
316164 KB |
Output is correct |
28 |
Correct |
1932 ms |
263756 KB |
Output is correct |
29 |
Correct |
1617 ms |
224648 KB |
Output is correct |
30 |
Correct |
1339 ms |
256156 KB |
Output is correct |
31 |
Execution timed out |
2085 ms |
288864 KB |
Time limit exceeded |
32 |
Correct |
946 ms |
424588 KB |
Output is correct |