#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int h,w;
cin>>h>>w;
string s[h+5];
int used[h+5][w+5];
for(int i=0;i<h;i++){
cin>>s[i];
for(int j=0;j<w;j++){
used[i][j]=0;
}
}
used[0][0]=1;
deque<pair<int,int>>q;
q.pb({0,0});
int ans=0;
while(!q.empty()){
int x=q.front().ff,y=q.front().ss;
ans=max(ans,used[x][y]);
q.pop_front();
if(x>0&&used[x-1][y]==0&&s[x-1][y]!='.'){
if(s[x-1][y]==s[x][y]){
used[x-1][y]=used[x][y];
q.push_front({x-1,y});
}
else{
used[x-1][y]=used[x][y]+1;
q.pb({x-1,y});
}
}
if(x<h-1&&used[x+1][y]==0&&s[x+1][y]!='.'){
if(s[x+1][y]==s[x][y]){
used[x+1][y]=used[x][y];
q.push_front({x+1,y});
}
else{
used[x+1][y]=used[x][y]+1;
q.pb({x+1,y});
}
}
if(y>0&&used[x][y-1]==0&&s[x][y-1]!='.'){
if(s[x][y-1]==s[x][y]){
used[x][y-1]=used[x][y];
q.push_front({x,y-1});
}
else{
used[x][y-1]=used[x][y]+1;
q.pb({x,y-1});
}
}
if(y<w-1&&used[x][y+1]==0&&s[x][y+1]!='.'){
if(s[x][y+1]==s[x][y]){
used[x][y+1]=used[x][y];
q.push_front({x,y+1});
}
else{
used[x][y+1]=used[x][y]+1;
q.pb({x,y+1});
}
}
}
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
1748 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
5 ms |
1364 KB |
Output is correct |
5 |
Correct |
2 ms |
716 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
2 ms |
588 KB |
Output is correct |
12 |
Correct |
3 ms |
840 KB |
Output is correct |
13 |
Correct |
3 ms |
724 KB |
Output is correct |
14 |
Correct |
2 ms |
724 KB |
Output is correct |
15 |
Correct |
11 ms |
1816 KB |
Output is correct |
16 |
Correct |
9 ms |
1800 KB |
Output is correct |
17 |
Correct |
7 ms |
1740 KB |
Output is correct |
18 |
Correct |
5 ms |
1364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
724 KB |
Output is correct |
2 |
Correct |
37 ms |
9552 KB |
Output is correct |
3 |
Correct |
163 ms |
81956 KB |
Output is correct |
4 |
Correct |
49 ms |
20492 KB |
Output is correct |
5 |
Correct |
127 ms |
46600 KB |
Output is correct |
6 |
Correct |
468 ms |
95148 KB |
Output is correct |
7 |
Correct |
2 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
724 KB |
Output is correct |
9 |
Correct |
2 ms |
716 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
712 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
30 ms |
9488 KB |
Output is correct |
14 |
Correct |
18 ms |
5748 KB |
Output is correct |
15 |
Correct |
12 ms |
6228 KB |
Output is correct |
16 |
Correct |
14 ms |
4180 KB |
Output is correct |
17 |
Correct |
79 ms |
21940 KB |
Output is correct |
18 |
Correct |
48 ms |
21632 KB |
Output is correct |
19 |
Correct |
44 ms |
20428 KB |
Output is correct |
20 |
Correct |
40 ms |
18880 KB |
Output is correct |
21 |
Correct |
104 ms |
48140 KB |
Output is correct |
22 |
Correct |
127 ms |
46516 KB |
Output is correct |
23 |
Correct |
162 ms |
40536 KB |
Output is correct |
24 |
Correct |
114 ms |
46960 KB |
Output is correct |
25 |
Correct |
310 ms |
82032 KB |
Output is correct |
26 |
Correct |
291 ms |
113156 KB |
Output is correct |
27 |
Correct |
362 ms |
101000 KB |
Output is correct |
28 |
Correct |
446 ms |
96624 KB |
Output is correct |
29 |
Correct |
450 ms |
97036 KB |
Output is correct |
30 |
Correct |
414 ms |
95208 KB |
Output is correct |
31 |
Correct |
347 ms |
53548 KB |
Output is correct |
32 |
Correct |
322 ms |
97612 KB |
Output is correct |