#include "bits/stdc++.h"
using namespace std;
const int N=4000;
int n, m;
short dx[]={0, 0, 1, -1};
short dy[]={1, -1, 0, 0};
vector<int> adj[N*N];
short grid[N][N];
int cmp[N][N];
int vis[N*N];
void dfs(int u, int c){
cmp[u/m][u%m]=c;
int i=u/m;
int j=u%m;
for(int k=0; k<4; k++){
int x=i+dx[k];
int y=j+dy[k];
if(x<0 || x==n || y<0 || y==m || grid[x][y]!=grid[i][j] || cmp[x][y]) continue;
dfs(m*x+y, c);
}
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
char c; cin >> c;
grid[i][j]=(c=='F'?2:c=='R'?1:0);
}
}
int c=0;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(grid[i][j]==0 || cmp[i][j]) continue;
dfs(m*i+j, ++c);
}
}
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
if(grid[i][j]==0) continue;
for(int k=0; k<4; k++){
int x=i+dx[k];
int y=j+dy[k];
if(x<0 || x==n || y<0 || y==m || grid[x][y]==grid[i][j] || grid[x][y]==0) continue;
adj[cmp[i][j]].push_back(cmp[x][y]);
adj[cmp[x][y]].push_back(cmp[i][j]);
}
}
}
for(int i=1; i<=c; i++){
sort(adj[i].begin(), adj[i].end());
adj[i].erase(unique(adj[i].begin(), adj[i].end()), adj[i].end());
}
queue<int> q;
int r=cmp[0][0];
q.push(r);
for(; !q.empty(); q.pop()){
int x=q.front();
for(int k:adj[x]){
if(!vis[k] && k!=r){
vis[k]=vis[x]+1;
q.push(k);
}
}
}
int mx=0;
for(int i=1; i<=c; i++) mx=max(mx, vis[i]);
cout << mx+1;
}
// when its been a long time since goodbye
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
223 ms |
386968 KB |
Output is correct |
2 |
Correct |
178 ms |
376224 KB |
Output is correct |
3 |
Correct |
213 ms |
376460 KB |
Output is correct |
4 |
Correct |
204 ms |
382712 KB |
Output is correct |
5 |
Correct |
192 ms |
379156 KB |
Output is correct |
6 |
Correct |
194 ms |
376164 KB |
Output is correct |
7 |
Correct |
205 ms |
376488 KB |
Output is correct |
8 |
Correct |
183 ms |
376672 KB |
Output is correct |
9 |
Correct |
196 ms |
376936 KB |
Output is correct |
10 |
Correct |
228 ms |
378912 KB |
Output is correct |
11 |
Correct |
188 ms |
378580 KB |
Output is correct |
12 |
Correct |
189 ms |
380940 KB |
Output is correct |
13 |
Correct |
211 ms |
379128 KB |
Output is correct |
14 |
Correct |
184 ms |
379228 KB |
Output is correct |
15 |
Correct |
218 ms |
385568 KB |
Output is correct |
16 |
Correct |
262 ms |
386956 KB |
Output is correct |
17 |
Correct |
197 ms |
383500 KB |
Output is correct |
18 |
Correct |
206 ms |
382716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
187 ms |
407508 KB |
Output is correct |
2 |
Correct |
402 ms |
406540 KB |
Output is correct |
3 |
Correct |
1091 ms |
526468 KB |
Output is correct |
4 |
Correct |
365 ms |
423240 KB |
Output is correct |
5 |
Correct |
1072 ms |
590504 KB |
Output is correct |
6 |
Correct |
1772 ms |
603944 KB |
Output is correct |
7 |
Correct |
183 ms |
409036 KB |
Output is correct |
8 |
Correct |
184 ms |
407512 KB |
Output is correct |
9 |
Correct |
178 ms |
377344 KB |
Output is correct |
10 |
Correct |
174 ms |
376548 KB |
Output is correct |
11 |
Correct |
185 ms |
408008 KB |
Output is correct |
12 |
Correct |
172 ms |
378028 KB |
Output is correct |
13 |
Correct |
327 ms |
406480 KB |
Output is correct |
14 |
Correct |
259 ms |
395548 KB |
Output is correct |
15 |
Correct |
240 ms |
395084 KB |
Output is correct |
16 |
Correct |
249 ms |
389956 KB |
Output is correct |
17 |
Correct |
570 ms |
443828 KB |
Output is correct |
18 |
Correct |
446 ms |
435264 KB |
Output is correct |
19 |
Correct |
382 ms |
423244 KB |
Output is correct |
20 |
Correct |
364 ms |
423008 KB |
Output is correct |
21 |
Correct |
688 ms |
477792 KB |
Output is correct |
22 |
Correct |
1022 ms |
590708 KB |
Output is correct |
23 |
Correct |
952 ms |
491608 KB |
Output is correct |
24 |
Correct |
682 ms |
481904 KB |
Output is correct |
25 |
Correct |
1399 ms |
550816 KB |
Output is correct |
26 |
Runtime error |
609 ms |
1048576 KB |
Execution killed with signal 9 |
27 |
Correct |
1073 ms |
696196 KB |
Output is correct |
28 |
Correct |
1784 ms |
603844 KB |
Output is correct |
29 |
Correct |
1665 ms |
581748 KB |
Output is correct |
30 |
Correct |
1453 ms |
638284 KB |
Output is correct |
31 |
Execution timed out |
2116 ms |
686640 KB |
Time limit exceeded |
32 |
Correct |
1041 ms |
817848 KB |
Output is correct |