#include<bits/stdc++.h>
using namespace std;
const int N = 4005, INF = 2e9, dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
int n, m, d[N][N];
char a[N][N];
inline bool check(int x, int y){
return 1 <= x && x <= n && 1 <= y && y <= m;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> a[i][j];
d[i][j] = INF;
}
}
deque < pair < short, short > > q;
q.push_back(make_pair(1, 1));
d[1][1] = 1;
while(!q.empty()){
int x = q.front().first,
y = q.front().second;
q.pop_front();
for(int i = 0; i < 4; i++){
int xx = x + dx[i],
yy = y + dy[i];
if(check(xx, yy) && a[xx][yy] != '.'){
int val = d[x][y] + (a[x][y] != a[xx][yy]);
if(d[xx][yy] > val){
d[xx][yy] = val;
if(a[x][y] == a[xx][yy]){
q.push_front(make_pair(xx, yy));
}
else{
q.push_back(make_pair(xx, yy));
}
}
}
}
}
int ans = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(a[i][j] != '.'){
ans = max(ans, d[i][j]);
}
}
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
5248 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
768 KB |
Output is correct |
4 |
Correct |
12 ms |
4960 KB |
Output is correct |
5 |
Correct |
6 ms |
2916 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
3 ms |
768 KB |
Output is correct |
8 |
Correct |
3 ms |
896 KB |
Output is correct |
9 |
Correct |
4 ms |
1124 KB |
Output is correct |
10 |
Correct |
5 ms |
2560 KB |
Output is correct |
11 |
Correct |
4 ms |
2148 KB |
Output is correct |
12 |
Correct |
8 ms |
2944 KB |
Output is correct |
13 |
Correct |
5 ms |
2944 KB |
Output is correct |
14 |
Correct |
6 ms |
2916 KB |
Output is correct |
15 |
Correct |
14 ms |
5348 KB |
Output is correct |
16 |
Correct |
16 ms |
5248 KB |
Output is correct |
17 |
Correct |
13 ms |
5120 KB |
Output is correct |
18 |
Correct |
12 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
30840 KB |
Output is correct |
2 |
Correct |
59 ms |
16436 KB |
Output is correct |
3 |
Correct |
399 ms |
78828 KB |
Output is correct |
4 |
Correct |
120 ms |
30000 KB |
Output is correct |
5 |
Correct |
232 ms |
59180 KB |
Output is correct |
6 |
Correct |
1897 ms |
85832 KB |
Output is correct |
7 |
Correct |
25 ms |
32248 KB |
Output is correct |
8 |
Correct |
26 ms |
30848 KB |
Output is correct |
9 |
Correct |
5 ms |
640 KB |
Output is correct |
10 |
Correct |
3 ms |
512 KB |
Output is correct |
11 |
Correct |
30 ms |
31736 KB |
Output is correct |
12 |
Correct |
4 ms |
1664 KB |
Output is correct |
13 |
Correct |
65 ms |
16376 KB |
Output is correct |
14 |
Correct |
43 ms |
11136 KB |
Output is correct |
15 |
Correct |
39 ms |
12160 KB |
Output is correct |
16 |
Correct |
29 ms |
6016 KB |
Output is correct |
17 |
Correct |
166 ms |
32248 KB |
Output is correct |
18 |
Correct |
143 ms |
31864 KB |
Output is correct |
19 |
Correct |
111 ms |
30044 KB |
Output is correct |
20 |
Correct |
108 ms |
27768 KB |
Output is correct |
21 |
Correct |
216 ms |
61176 KB |
Output is correct |
22 |
Correct |
234 ms |
59128 KB |
Output is correct |
23 |
Correct |
329 ms |
49436 KB |
Output is correct |
24 |
Correct |
234 ms |
60920 KB |
Output is correct |
25 |
Correct |
621 ms |
78840 KB |
Output is correct |
26 |
Correct |
459 ms |
94236 KB |
Output is correct |
27 |
Correct |
726 ms |
93108 KB |
Output is correct |
28 |
Correct |
879 ms |
85692 KB |
Output is correct |
29 |
Correct |
961 ms |
86272 KB |
Output is correct |
30 |
Correct |
867 ms |
87784 KB |
Output is correct |
31 |
Correct |
742 ms |
63480 KB |
Output is correct |
32 |
Correct |
647 ms |
87872 KB |
Output is correct |