#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define pii pair <int, int>
#define pb push_back
#define ff first
#define ss second
int X[] = {-1, 1, 0, 0};
int Y[] = {0, 0, -1, 1};
int main ()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<char>> c(n+1, vector<char>(m+1));
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++) cin >> c[i][j];
}
int ans = 0;
vector<vector<int>> dis(n+1, vector<int>(m+1, 0));
deque<pii> q = {{1, 1}};
dis[1][1] = 1;
while(!q.empty()){
int x = q.front().ff, y = q.front().ss;
q.pop_front();
ans = max(ans, dis[x][y]);
for (int i = 0; i < 4; i++){
int a = x+X[i], b = y+Y[i];
if (a > 0 and b > 0 and a <= n and b <= m and !dis[a][b] and c[a][b] != '.'){
dis[a][b] = dis[x][y];
if (c[x][y] != c[a][b]){
dis[a][b]++;
q.pb({a, b});
}else q.push_front({a, b});
}
}
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
1844 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
5 ms |
1484 KB |
Output is correct |
5 |
Correct |
3 ms |
712 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
328 KB |
Output is correct |
10 |
Correct |
2 ms |
712 KB |
Output is correct |
11 |
Correct |
2 ms |
584 KB |
Output is correct |
12 |
Correct |
4 ms |
852 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
3 ms |
724 KB |
Output is correct |
15 |
Correct |
10 ms |
1860 KB |
Output is correct |
16 |
Correct |
10 ms |
1748 KB |
Output is correct |
17 |
Correct |
9 ms |
1776 KB |
Output is correct |
18 |
Correct |
6 ms |
1416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
840 KB |
Output is correct |
2 |
Correct |
41 ms |
9084 KB |
Output is correct |
3 |
Correct |
281 ms |
79928 KB |
Output is correct |
4 |
Correct |
84 ms |
20084 KB |
Output is correct |
5 |
Correct |
170 ms |
45896 KB |
Output is correct |
6 |
Correct |
522 ms |
93188 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
3 ms |
852 KB |
Output is correct |
9 |
Correct |
2 ms |
696 KB |
Output is correct |
10 |
Correct |
1 ms |
516 KB |
Output is correct |
11 |
Correct |
2 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
41 ms |
9088 KB |
Output is correct |
14 |
Correct |
22 ms |
5316 KB |
Output is correct |
15 |
Correct |
19 ms |
5908 KB |
Output is correct |
16 |
Correct |
19 ms |
3980 KB |
Output is correct |
17 |
Correct |
115 ms |
21608 KB |
Output is correct |
18 |
Correct |
74 ms |
21328 KB |
Output is correct |
19 |
Correct |
73 ms |
20028 KB |
Output is correct |
20 |
Correct |
67 ms |
18560 KB |
Output is correct |
21 |
Correct |
169 ms |
47396 KB |
Output is correct |
22 |
Correct |
173 ms |
45860 KB |
Output is correct |
23 |
Correct |
229 ms |
39756 KB |
Output is correct |
24 |
Correct |
151 ms |
46316 KB |
Output is correct |
25 |
Correct |
403 ms |
79948 KB |
Output is correct |
26 |
Correct |
311 ms |
111688 KB |
Output is correct |
27 |
Correct |
415 ms |
97396 KB |
Output is correct |
28 |
Correct |
525 ms |
93072 KB |
Output is correct |
29 |
Correct |
538 ms |
93428 KB |
Output is correct |
30 |
Correct |
469 ms |
92896 KB |
Output is correct |
31 |
Correct |
416 ms |
52188 KB |
Output is correct |
32 |
Correct |
373 ms |
95420 KB |
Output is correct |