#include <bits/stdc++.h>
using namespace std;
int H, W;
int grid[4001][4001], cc[4001][4001];
queue<pair<int,int>> q;
vector<pair<int,int>> dir = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
void ff(int cur, int val){
while(!q.empty()){
auto temp = q.front();
int x = temp.first, y = temp.second;
q.pop();
for(auto [dx, dy] : dir){
if((x+dx > -1)&& (x+dx < H) && (y+dy > -1) && (y+dy < W) && (cc[x+dx][y+dy] == -1) && (grid[x+dx][y+dy] == cur)){
cc[x+dx][y+dy] = val;
q.push({x+dx, y+dy});
}
}
}
}
int main(){
cin>>H>>W;
memset(grid, 0, sizeof(grid));
memset(cc, -1, sizeof(cc));
for(int i = 0; i < H; i++){
string s; cin>>s;
for(int j = 0; j < W; j++){
if(s[j] == 'F') grid[i][j] = 1;
else if(s[j] == 'R') grid[i][j] = 2;
}
}
int val = 0;
for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++){
if(cc[i][j] == -1 && grid[i][j] != 0){
cc[i][j] = val;
q.push({i,j});
ff(grid[i][j], val);
val++;
}
}
}
vector<unordered_set<int>> adj(val+1);
for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++){
if(grid[i][j] != 0){
for(auto [dx, dy] : dir){
if(i+dx > -1 && i+dx < H && j+dy > -1 && j+dy < W && grid[i+dx][j+dy] != grid[i][j] && grid[i+dx][j+dy] != 0){
adj[cc[i][j]].insert(cc[i+dx][j+dy]);
adj[cc[i+dx][j+dy]].insert(cc[i][j]);
}
}
}
}
}
queue<int> q1;
q1.push(0);
vector<int> v(val+1, 0), d(val+1, 0);
v[0] = 1;
int ans = 0;
while(!q1.empty()){
auto temp = q1.front();
q1.pop();
for(auto i : adj[temp]){
if(!v[i]){
q1.push(i);
v[i] = 1;
d[i] = d[temp] + 1;
ans = max(ans, d[i]);
}
}
}
cout<<ans+1<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
135248 KB |
Output is correct |
2 |
Correct |
46 ms |
125524 KB |
Output is correct |
3 |
Correct |
47 ms |
125780 KB |
Output is correct |
4 |
Correct |
60 ms |
126548 KB |
Output is correct |
5 |
Correct |
54 ms |
127572 KB |
Output is correct |
6 |
Correct |
45 ms |
125524 KB |
Output is correct |
7 |
Correct |
46 ms |
125676 KB |
Output is correct |
8 |
Correct |
48 ms |
125776 KB |
Output is correct |
9 |
Correct |
46 ms |
125772 KB |
Output is correct |
10 |
Correct |
51 ms |
127636 KB |
Output is correct |
11 |
Correct |
50 ms |
125896 KB |
Output is correct |
12 |
Correct |
62 ms |
129132 KB |
Output is correct |
13 |
Correct |
56 ms |
127572 KB |
Output is correct |
14 |
Correct |
50 ms |
127572 KB |
Output is correct |
15 |
Correct |
72 ms |
135248 KB |
Output is correct |
16 |
Correct |
96 ms |
135340 KB |
Output is correct |
17 |
Correct |
68 ms |
134484 KB |
Output is correct |
18 |
Correct |
54 ms |
126636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
126804 KB |
Output is correct |
2 |
Correct |
165 ms |
175440 KB |
Output is correct |
3 |
Correct |
985 ms |
485204 KB |
Output is correct |
4 |
Correct |
249 ms |
189304 KB |
Output is correct |
5 |
Runtime error |
1417 ms |
1048576 KB |
Execution killed with signal 9 |
6 |
Correct |
1274 ms |
272776 KB |
Output is correct |
7 |
Correct |
49 ms |
126664 KB |
Output is correct |
8 |
Correct |
49 ms |
126980 KB |
Output is correct |
9 |
Correct |
50 ms |
127312 KB |
Output is correct |
10 |
Correct |
47 ms |
126292 KB |
Output is correct |
11 |
Correct |
48 ms |
126184 KB |
Output is correct |
12 |
Correct |
51 ms |
128340 KB |
Output is correct |
13 |
Correct |
177 ms |
175324 KB |
Output is correct |
14 |
Correct |
129 ms |
154708 KB |
Output is correct |
15 |
Correct |
154 ms |
160060 KB |
Output is correct |
16 |
Correct |
104 ms |
148568 KB |
Output is correct |
17 |
Correct |
391 ms |
254800 KB |
Output is correct |
18 |
Correct |
478 ms |
258428 KB |
Output is correct |
19 |
Correct |
305 ms |
189268 KB |
Output is correct |
20 |
Correct |
222 ms |
207596 KB |
Output is correct |
21 |
Correct |
481 ms |
340308 KB |
Output is correct |
22 |
Runtime error |
1453 ms |
1048576 KB |
Execution killed with signal 9 |
23 |
Correct |
811 ms |
374364 KB |
Output is correct |
24 |
Correct |
791 ms |
484084 KB |
Output is correct |
25 |
Correct |
1787 ms |
672848 KB |
Output is correct |
26 |
Correct |
528 ms |
131920 KB |
Output is correct |
27 |
Correct |
673 ms |
138260 KB |
Output is correct |
28 |
Correct |
1227 ms |
268860 KB |
Output is correct |
29 |
Correct |
1110 ms |
214768 KB |
Output is correct |
30 |
Correct |
936 ms |
190620 KB |
Output is correct |
31 |
Correct |
1896 ms |
516640 KB |
Output is correct |
32 |
Correct |
677 ms |
166320 KB |
Output is correct |