#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <set>
#include <iterator>
#include <stack>
#include <map>
#include <math.h>
#include <bitset>
#include <deque>
#include <string>
#include <tuple>
#include <queue>
#include <numeric>
#include <unordered_set>
#include <unordered_map>
#define pi 3.141592653589793238
#define ll long long
#define ld long double
#define rep(i, a, b) for (long long i = a; i <= b; i++)
#define mod 1000000007
#define INF 1000000000000000000
#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define all(x) (x).begin (), (x).end ()
#define sz(x) (ll) (x).size ()
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
#define rank rnk
#define log lg
#define fast \
ios_base::sync_with_stdio (false); \
cin.tie (NULL); \
cout.tie (NULL)
using namespace std;
string g[4000];
int group[4000][4000];
bool marked[4000][4000];
vector <pair<int,int>> dir={{0,1},{0,-1},{1,0},{-1,0}};
int n,m;
int curr=1;
void dfs(int i, int j)
{
marked[i][j]=true;
group[i][j]=curr;
for(auto mv:dir)
{
ll v1=i+mv.ff;
ll v2=j+mv.ss;
if(v1>=0 && v2>=0 && v1<n && v2<m && !marked[v1][v2])
if(g[i][j]==g[v1][v2])
{
dfs(v1,v2);
}
}
}
signed main()
{
fast;
cin>>n>>m;
rep(i,0,n-1)
cin>>g[i];
rep(i,0,n-1)
rep(j,0,m-1)
marked[i][j]=false, group[i][j]=0;
rep(i,0,n-1)
rep(j,0,m-1)
if(!marked[i][j])
if(g[i][j]=='F' || g[i][j]=='R')
{
dfs(i,j);
curr+=1;
}
unordered_set <int> adj[curr];
rep(i,0,n-1)
rep(j,0,m-1)
{
for(auto mv:dir)
{
ll v1=i+mv.ff;
ll v2=j+mv.ss;
if(v1>=0 && v2>=0 && v1<n && v2<m)
{
ll g1=group[i][j];
ll g2=group[v1][v2];
if(g1>0 && g2>0)
if(!adj[g1].count(g2))
{
adj[g1].insert(g2);
adj[g2].insert(g1);
}
}
}
}
queue <int> q;
vector <bool> used(curr);
vector<int> d(curr), p(curr);
q.push(group[0][0]);
used[group[0][0]] = true;
while (!q.empty()) {
int v = q.front();
q.pop();
for (int u : adj[v]) {
if (!used[u]) {
used[u] = true;
q.push(u);
d[u] = d[v] + 1;
}
}
}
int ans=0;
rep(i,0,curr-1)
ans=max(ans,d[i]);
cout<<1+ans;
// rep(i,0,n-1)
// rep(j,0,m-1)
// cout<<group[i][j]<<" ";
return 0;
}
/*
5 8
FFR.....
.FRRR...
.FFFFF..
..RRRFFR
.....FFF
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
15620 KB |
Output is correct |
2 |
Correct |
1 ms |
588 KB |
Output is correct |
3 |
Correct |
2 ms |
844 KB |
Output is correct |
4 |
Correct |
23 ms |
6676 KB |
Output is correct |
5 |
Correct |
8 ms |
5196 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
1 ms |
832 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1356 KB |
Output is correct |
10 |
Correct |
7 ms |
4812 KB |
Output is correct |
11 |
Correct |
5 ms |
2892 KB |
Output is correct |
12 |
Correct |
17 ms |
6884 KB |
Output is correct |
13 |
Correct |
7 ms |
5196 KB |
Output is correct |
14 |
Correct |
7 ms |
5196 KB |
Output is correct |
15 |
Correct |
45 ms |
15664 KB |
Output is correct |
16 |
Correct |
50 ms |
15624 KB |
Output is correct |
17 |
Correct |
36 ms |
14796 KB |
Output is correct |
18 |
Correct |
20 ms |
6732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
32104 KB |
Output is correct |
2 |
Correct |
205 ms |
71180 KB |
Output is correct |
3 |
Correct |
1190 ms |
471384 KB |
Output is correct |
4 |
Correct |
295 ms |
100164 KB |
Output is correct |
5 |
Runtime error |
1489 ms |
1048580 KB |
Execution killed with signal 9 |
6 |
Correct |
1991 ms |
245940 KB |
Output is correct |
7 |
Correct |
16 ms |
33344 KB |
Output is correct |
8 |
Correct |
18 ms |
32064 KB |
Output is correct |
9 |
Correct |
8 ms |
2516 KB |
Output is correct |
10 |
Correct |
2 ms |
1484 KB |
Output is correct |
11 |
Correct |
14 ms |
32336 KB |
Output is correct |
12 |
Correct |
5 ms |
4300 KB |
Output is correct |
13 |
Correct |
206 ms |
71408 KB |
Output is correct |
14 |
Correct |
121 ms |
43040 KB |
Output is correct |
15 |
Correct |
125 ms |
49888 KB |
Output is correct |
16 |
Correct |
100 ms |
31244 KB |
Output is correct |
17 |
Correct |
545 ms |
172804 KB |
Output is correct |
18 |
Correct |
511 ms |
175576 KB |
Output is correct |
19 |
Correct |
294 ms |
100176 KB |
Output is correct |
20 |
Correct |
282 ms |
117444 KB |
Output is correct |
21 |
Correct |
714 ms |
296484 KB |
Output is correct |
22 |
Runtime error |
1386 ms |
1048580 KB |
Execution killed with signal 9 |
23 |
Correct |
1014 ms |
320612 KB |
Output is correct |
24 |
Correct |
914 ms |
448664 KB |
Output is correct |
25 |
Execution timed out |
2137 ms |
670336 KB |
Time limit exceeded |
26 |
Correct |
1006 ms |
1042820 KB |
Output is correct |
27 |
Correct |
1162 ms |
320156 KB |
Output is correct |
28 |
Correct |
1832 ms |
242708 KB |
Output is correct |
29 |
Correct |
1689 ms |
192576 KB |
Output is correct |
30 |
Correct |
1549 ms |
253852 KB |
Output is correct |
31 |
Execution timed out |
2104 ms |
483316 KB |
Time limit exceeded |
32 |
Correct |
1281 ms |
461868 KB |
Output is correct |