#include <bits/stdc++.h>
#define int long long
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int N=4e3+5;
char a[N][N];
int d[N][N];
int h,w;
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
cin>>h>>w;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
cin>>a[i][j];
}
}
vector <int> x={1,-1,0,0},y={0,0,1,-1};
deque <pair <int,int> > q;
q.push_back({0,0});
d[0][0]=1;
while(!q.empty()){
pair <int,int> v=q.front();
q.pop_front();
int X=v.ff,Y=v.ss;
for(int i=0;i<4;i++){
if(X+x[i]>=0 && X+x[i]<h && Y+y[i]>=0 && Y+y[i]<w && a[X+x[i]][Y+y[i]]!='.' && d[X+x[i]][Y+y[i]]==0){
if(a[X+x[i]][Y+y[i]]==a[X][Y]){
d[X+x[i]][Y+y[i]]=d[X][Y];
q.push_front({X+x[i],Y+y[i]});
}
else{
d[X+x[i]][Y+y[i]]=d[X][Y]+1;
q.push_back({X+x[i],Y+y[i]});
}
}
}
}
int ans=0;
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
ans=max(ans,d[i][j]);
}
}
cout<<ans<<"\n";
}
/*
FFR.....
.FRRR...
.FFFFF..
..RRRFFR
.....FFF
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
8284 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
9 ms |
8028 KB |
Output is correct |
5 |
Correct |
4 ms |
4188 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
3 ms |
3676 KB |
Output is correct |
11 |
Correct |
3 ms |
3676 KB |
Output is correct |
12 |
Correct |
6 ms |
4184 KB |
Output is correct |
13 |
Correct |
4 ms |
4188 KB |
Output is correct |
14 |
Correct |
4 ms |
4188 KB |
Output is correct |
15 |
Correct |
14 ms |
8028 KB |
Output is correct |
16 |
Correct |
16 ms |
8284 KB |
Output is correct |
17 |
Correct |
16 ms |
8004 KB |
Output is correct |
18 |
Correct |
8 ms |
8028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
31360 KB |
Output is correct |
2 |
Correct |
62 ms |
18092 KB |
Output is correct |
3 |
Correct |
481 ms |
75944 KB |
Output is correct |
4 |
Correct |
126 ms |
43728 KB |
Output is correct |
5 |
Correct |
311 ms |
62908 KB |
Output is correct |
6 |
Correct |
802 ms |
168664 KB |
Output is correct |
7 |
Correct |
11 ms |
32604 KB |
Output is correct |
8 |
Correct |
13 ms |
31324 KB |
Output is correct |
9 |
Correct |
3 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
11 ms |
32092 KB |
Output is correct |
12 |
Correct |
2 ms |
3164 KB |
Output is correct |
13 |
Correct |
63 ms |
18104 KB |
Output is correct |
14 |
Correct |
36 ms |
12116 KB |
Output is correct |
15 |
Correct |
46 ms |
18364 KB |
Output is correct |
16 |
Correct |
27 ms |
7240 KB |
Output is correct |
17 |
Correct |
156 ms |
32656 KB |
Output is correct |
18 |
Correct |
144 ms |
47792 KB |
Output is correct |
19 |
Correct |
127 ms |
43640 KB |
Output is correct |
20 |
Correct |
107 ms |
28244 KB |
Output is correct |
21 |
Correct |
302 ms |
56248 KB |
Output is correct |
22 |
Correct |
301 ms |
63056 KB |
Output is correct |
23 |
Correct |
314 ms |
50344 KB |
Output is correct |
24 |
Correct |
287 ms |
55120 KB |
Output is correct |
25 |
Correct |
692 ms |
141488 KB |
Output is correct |
26 |
Correct |
523 ms |
224772 KB |
Output is correct |
27 |
Correct |
720 ms |
194720 KB |
Output is correct |
28 |
Correct |
804 ms |
168656 KB |
Output is correct |
29 |
Correct |
823 ms |
165560 KB |
Output is correct |
30 |
Correct |
748 ms |
174604 KB |
Output is correct |
31 |
Correct |
559 ms |
108872 KB |
Output is correct |
32 |
Correct |
618 ms |
165820 KB |
Output is correct |