#include <bits/stdc++.h>
using namespace std;
const int MAX = 4e3+4;
char matriz[MAX][MAX];
int dist[MAX][MAX];
int n,m;
bool inBound(pair<int,int> a){
int x = a.first, y = a.second;
if(x < 0 or x >= n or y < 0 or y >= m or matriz[x][y] == '.' or dist[x][y] > 0){
return false;
}
return true;
}
int main(){ios_base::sync_with_stdio(false); cin.tie(NULL);
int ans = 1;
cin >> n >> m;
for(int i =0;i < n;i++){
for(int j = 0; j < m;j++){
cin >> matriz[i][j];
}
}
deque<pair<int,int>> fila;
dist[0][0] =1;
fila.push_front({0,0});
while(!fila.empty()){
pair<int,int> atual = fila.front();
fila.pop_front();
int x = atual.first, y = atual.second;
ans = max(ans,dist[x][y]);
if(inBound({x+1,y})){
if(matriz[x+1][y] == matriz[x][y]){
dist[x+1][y] = dist[x][y];
fila.push_front({x+1,y});
}
else{
dist[x+1][y] = dist[x][y]+1;
fila.push_back({x+1,y});
}
}
if(inBound({x-1,y})){
if(matriz[x-1][y] == matriz[x][y]){
dist[x-1][y] = dist[x][y];
fila.push_front({x-1,y});
}
else{
dist[x-1][y] = dist[x][y]+1;
fila.push_back({x-1,y});
}
}
if(inBound({x,y+1})){
if(matriz[x][y+1] == matriz[x][y]){
dist[x][y+1] = dist[x][y];
fila.push_front({x,y+1});
}
else{
dist[x][y+1] = dist[x][y]+1;
fila.push_back({x,y+1});
}
}
if(inBound({x,y-1})){
if(matriz[x][y-1] == matriz[x][y]){
dist[x][y-1] = dist[x][y];
fila.push_front({x,y-1});
}
else{
dist[x][y-1] = dist[x][y]+1;
fila.push_back({x,y-1});
}
}
}
cout << max(ans,dist[n-1][m-1]);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
5204 KB |
Output is correct |
2 |
Correct |
0 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
724 KB |
Output is correct |
4 |
Correct |
9 ms |
4988 KB |
Output is correct |
5 |
Correct |
4 ms |
2772 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
3 ms |
2388 KB |
Output is correct |
11 |
Correct |
3 ms |
2132 KB |
Output is correct |
12 |
Correct |
6 ms |
2900 KB |
Output is correct |
13 |
Correct |
3 ms |
2864 KB |
Output is correct |
14 |
Correct |
3 ms |
2900 KB |
Output is correct |
15 |
Correct |
12 ms |
5004 KB |
Output is correct |
16 |
Correct |
13 ms |
5120 KB |
Output is correct |
17 |
Correct |
10 ms |
4948 KB |
Output is correct |
18 |
Correct |
9 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
30752 KB |
Output is correct |
2 |
Correct |
59 ms |
13524 KB |
Output is correct |
3 |
Correct |
331 ms |
57928 KB |
Output is correct |
4 |
Correct |
95 ms |
29016 KB |
Output is correct |
5 |
Correct |
236 ms |
44688 KB |
Output is correct |
6 |
Correct |
736 ms |
92260 KB |
Output is correct |
7 |
Correct |
17 ms |
32084 KB |
Output is correct |
8 |
Correct |
18 ms |
30676 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
17 ms |
31572 KB |
Output is correct |
12 |
Correct |
2 ms |
1492 KB |
Output is correct |
13 |
Correct |
50 ms |
13560 KB |
Output is correct |
14 |
Correct |
30 ms |
9664 KB |
Output is correct |
15 |
Correct |
30 ms |
12152 KB |
Output is correct |
16 |
Correct |
24 ms |
5132 KB |
Output is correct |
17 |
Correct |
126 ms |
24780 KB |
Output is correct |
18 |
Correct |
120 ms |
31788 KB |
Output is correct |
19 |
Correct |
93 ms |
29048 KB |
Output is correct |
20 |
Correct |
85 ms |
22244 KB |
Output is correct |
21 |
Correct |
201 ms |
43540 KB |
Output is correct |
22 |
Correct |
233 ms |
44684 KB |
Output is correct |
23 |
Correct |
244 ms |
36272 KB |
Output is correct |
24 |
Correct |
205 ms |
41080 KB |
Output is correct |
25 |
Correct |
548 ms |
78940 KB |
Output is correct |
26 |
Correct |
474 ms |
119104 KB |
Output is correct |
27 |
Correct |
639 ms |
105420 KB |
Output is correct |
28 |
Correct |
721 ms |
92316 KB |
Output is correct |
29 |
Correct |
715 ms |
90688 KB |
Output is correct |
30 |
Correct |
662 ms |
95096 KB |
Output is correct |
31 |
Correct |
499 ms |
63564 KB |
Output is correct |
32 |
Correct |
555 ms |
90748 KB |
Output is correct |