#include <bits/stdc++.h>
using namespace std;
struct dir{
int vt,hz;
}a[8]={{0,1},{0,-1},{1,0},{-1,0}};
int h,w,ct,mv,ans,dis[4005][4005],len,x,y,newx,newy;
char s[4005][4005],c;
bool vis[4005][4005];
queue <pair<int,pair<pair<int,int>,char> > > q1,q2;
int main(){
ios::sync_with_stdio(false);
cin >> h >> w;
for (int i=1;i<=h;i++){
for (int j=1;j<=w;j++){
cin >> s[i][j];
if (s[i][j] != '.')
ct++;
}
}
q1.push(make_pair(-1,make_pair(make_pair(1,1),s[1][1])));
while (!q1.empty() || !q2.empty()){
if (q2.empty() || (!q1.empty() && -q2.front().first >= -q1.front().first)){
x=q1.front().second.first.first; y=q1.front().second.first.second;
len=-q1.front().first; c=q1.front().second.second;
q1.pop();
}
else
{
x=q2.front().second.first.first; y=q2.front().second.first.second;
len=-q2.front().first; c=q2.front().second.second;
q2.pop();
}
if (!vis[x][y]){
vis[x][y]=true; dis[x][y]=len;
for (int i=0;i<4;i++){
newx=x+a[i].vt; newy=y+a[i].hz;
if (newx > 0 && newx <= h && newy > 0 && newy <= w && !vis[newx][newy] && s[newx][newy] != '.'){
if (c != s[newx][newy])
q1.push(make_pair(-len-1,make_pair(make_pair(newx,newy),s[newx][newy])));
else
q2.push(make_pair(-len,make_pair(make_pair(newx,newy),s[newx][newy])));
}
}
}
}
for (int i=1;i<=h;i++){
for (int j=1;j<=w;j++){
ans=max(ans,dis[i][j]);
}
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
7508 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
16 ms |
7356 KB |
Output is correct |
5 |
Correct |
5 ms |
4024 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
1116 KB |
Output is correct |
9 |
Correct |
2 ms |
1492 KB |
Output is correct |
10 |
Correct |
5 ms |
3480 KB |
Output is correct |
11 |
Correct |
5 ms |
2964 KB |
Output is correct |
12 |
Correct |
10 ms |
4244 KB |
Output is correct |
13 |
Correct |
5 ms |
4116 KB |
Output is correct |
14 |
Correct |
5 ms |
4052 KB |
Output is correct |
15 |
Correct |
23 ms |
7244 KB |
Output is correct |
16 |
Correct |
27 ms |
7524 KB |
Output is correct |
17 |
Correct |
17 ms |
7120 KB |
Output is correct |
18 |
Correct |
15 ms |
7356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
45732 KB |
Output is correct |
2 |
Correct |
93 ms |
20112 KB |
Output is correct |
3 |
Correct |
471 ms |
79876 KB |
Output is correct |
4 |
Correct |
137 ms |
40068 KB |
Output is correct |
5 |
Correct |
275 ms |
65052 KB |
Output is correct |
6 |
Correct |
1999 ms |
136216 KB |
Output is correct |
7 |
Correct |
23 ms |
47956 KB |
Output is correct |
8 |
Correct |
24 ms |
45788 KB |
Output is correct |
9 |
Correct |
4 ms |
724 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
24 ms |
46924 KB |
Output is correct |
12 |
Correct |
2 ms |
2132 KB |
Output is correct |
13 |
Correct |
87 ms |
20036 KB |
Output is correct |
14 |
Correct |
48 ms |
13912 KB |
Output is correct |
15 |
Correct |
47 ms |
17032 KB |
Output is correct |
16 |
Correct |
40 ms |
7244 KB |
Output is correct |
17 |
Correct |
238 ms |
36748 KB |
Output is correct |
18 |
Correct |
158 ms |
43468 KB |
Output is correct |
19 |
Correct |
130 ms |
40140 KB |
Output is correct |
20 |
Correct |
113 ms |
32536 KB |
Output is correct |
21 |
Correct |
287 ms |
64668 KB |
Output is correct |
22 |
Correct |
280 ms |
65028 KB |
Output is correct |
23 |
Correct |
428 ms |
53704 KB |
Output is correct |
24 |
Correct |
258 ms |
62128 KB |
Output is correct |
25 |
Correct |
721 ms |
100792 KB |
Output is correct |
26 |
Correct |
821 ms |
89996 KB |
Output is correct |
27 |
Correct |
1274 ms |
107384 KB |
Output is correct |
28 |
Correct |
1758 ms |
136380 KB |
Output is correct |
29 |
Correct |
1658 ms |
128532 KB |
Output is correct |
30 |
Correct |
1482 ms |
125164 KB |
Output is correct |
31 |
Correct |
1215 ms |
87328 KB |
Output is correct |
32 |
Correct |
1058 ms |
105256 KB |
Output is correct |