#include<bits/stdc++.h>
#define db double
#define fo(i,d,c) for(int i=d;i<=c;i++)
#define fod(i,c,d) for(int i=c;i>=d;i--)
#define maxn 1000010
#define N 5010
#define pb push_back
#define en cout<<"\n";
#define inf 1e9
#define pii pair<int,int>
#define vii vector<pii>
#define bit(i,j) ((i>>j)&1)
#define offbit(i,j) (i^(1<<j))
#define onbit(i,j) (i|(j<<1))
#define vi vector<int>
#define vvi vector<vector<int>>
#define checkfile(FiLeNaMe) { if(fopen(FiLeNaMe".inp","r")) freopen(FiLeNaMe".inp","r",stdin),freopen(FiLeNaMe".out","w",stdout); }
using namespace std;
int n,m,ans,d[N][N];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
char a[N][N];
bool safe(int i,int j)
{
return i>0&&j>0&&i<=n&&j<=m;
}
void process()
{
cin>>n>>m;
fo(i,1,n) fo(j,1,m) cin>>a[i][j];
queue<pii> q[2];
int cur=0;//tmp queue
d[1][1]=1;
q[cur].push({1,1});
while(q[0].size()||q[1].size())
{
int u=q[cur].front().first;
int v=q[cur].front().second;
q[cur].pop();
ans=max(ans,d[u][v]);
fo(k,0,3)
{
int U=u+dx[k];
int V=v+dy[k];
if(safe(U,V)&&a[U][V]==a[u][v]&&!d[U][V])
{
d[U][V]=d[u][v];
q[cur].push({U,V});
}
if(safe(U,V)&&a[U][V]==('R'+'F'-a[u][v])&&!d[U][V])
{
d[U][V]=d[u][v]+1;
q[1-cur].push({U,V});
}
}
if(!q[cur].size()) cur^=1;
}
cout<<ans;
}
signed main()
{
ios_base::sync_with_stdio(false);cin.tie(NULL);
process();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
9564 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
10 ms |
9308 KB |
Output is correct |
5 |
Correct |
3 ms |
5976 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
5724 KB |
Output is correct |
11 |
Correct |
3 ms |
5468 KB |
Output is correct |
12 |
Correct |
7 ms |
5976 KB |
Output is correct |
13 |
Correct |
3 ms |
6232 KB |
Output is correct |
14 |
Correct |
4 ms |
5976 KB |
Output is correct |
15 |
Correct |
17 ms |
9308 KB |
Output is correct |
16 |
Correct |
18 ms |
9584 KB |
Output is correct |
17 |
Correct |
11 ms |
9308 KB |
Output is correct |
18 |
Correct |
10 ms |
9448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
38492 KB |
Output is correct |
2 |
Correct |
58 ms |
16980 KB |
Output is correct |
3 |
Correct |
335 ms |
67128 KB |
Output is correct |
4 |
Correct |
85 ms |
34156 KB |
Output is correct |
5 |
Correct |
224 ms |
51680 KB |
Output is correct |
6 |
Correct |
1257 ms |
111684 KB |
Output is correct |
7 |
Correct |
11 ms |
39256 KB |
Output is correct |
8 |
Correct |
11 ms |
38488 KB |
Output is correct |
9 |
Correct |
4 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
11 ms |
38924 KB |
Output is correct |
12 |
Correct |
2 ms |
5224 KB |
Output is correct |
13 |
Correct |
57 ms |
16980 KB |
Output is correct |
14 |
Correct |
34 ms |
14428 KB |
Output is correct |
15 |
Correct |
23 ms |
16472 KB |
Output is correct |
16 |
Correct |
32 ms |
7764 KB |
Output is correct |
17 |
Correct |
146 ms |
29304 KB |
Output is correct |
18 |
Correct |
101 ms |
36436 KB |
Output is correct |
19 |
Correct |
86 ms |
34132 KB |
Output is correct |
20 |
Correct |
78 ms |
27732 KB |
Output is correct |
21 |
Correct |
217 ms |
49748 KB |
Output is correct |
22 |
Correct |
207 ms |
51540 KB |
Output is correct |
23 |
Correct |
308 ms |
40880 KB |
Output is correct |
24 |
Correct |
192 ms |
47416 KB |
Output is correct |
25 |
Correct |
508 ms |
101448 KB |
Output is correct |
26 |
Correct |
803 ms |
82820 KB |
Output is correct |
27 |
Correct |
958 ms |
103096 KB |
Output is correct |
28 |
Correct |
1248 ms |
111340 KB |
Output is correct |
29 |
Correct |
1233 ms |
109840 KB |
Output is correct |
30 |
Correct |
1103 ms |
107492 KB |
Output is correct |
31 |
Correct |
920 ms |
72316 KB |
Output is correct |
32 |
Correct |
906 ms |
102104 KB |
Output is correct |