#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int MAXN = 4010;
char grid[MAXN][MAXN];
int dist[MAXN][MAXN];
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int H,W; cin >> H >> W;
for(int i = 1;i<=H;++i){
for(int j = 1;j<=W;++j){
cin >> grid[i][j];
}
}
int ans = 0;
int dx[4] = {0,0,1,-1}, dy[4] = {1,-1,0,0};
dist[1][1] = 1;
deque<pair<int,int>> q; q.push_back({1,1});
while(!q.empty()){
int r = q.front().f, c = q.front().s; q.pop_front();
ans = max(ans,dist[r][c]);
//cout << r << " " << c << " " << dist[r][c] << "\n";
for(int i = 0;i<4;++i){
int nr = r+dx[i], nc = c+dy[i];
if(nr < 1 || nc < 1 || nr > H || nc > W) continue;
if(dist[nr][nc] == 0){
if(grid[nr][nc] == '.') continue;
if(grid[r][c] != grid[nr][nc]){
q.push_back({nr,nc});
dist[nr][nc] = dist[r][c]+1;
}
else{
q.push_front({nr,nc});
dist[nr][nc] = dist[r][c];
}
}
}
}
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
7512 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
5 ms |
7264 KB |
Output is correct |
5 |
Correct |
3 ms |
5976 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2908 KB |
Output is correct |
10 |
Correct |
2 ms |
5468 KB |
Output is correct |
11 |
Correct |
2 ms |
3420 KB |
Output is correct |
12 |
Correct |
4 ms |
5980 KB |
Output is correct |
13 |
Correct |
3 ms |
5980 KB |
Output is correct |
14 |
Correct |
3 ms |
6232 KB |
Output is correct |
15 |
Correct |
9 ms |
7260 KB |
Output is correct |
16 |
Correct |
10 ms |
7444 KB |
Output is correct |
17 |
Correct |
8 ms |
7380 KB |
Output is correct |
18 |
Correct |
7 ms |
7260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
32348 KB |
Output is correct |
2 |
Correct |
43 ms |
14932 KB |
Output is correct |
3 |
Correct |
234 ms |
58192 KB |
Output is correct |
4 |
Correct |
64 ms |
31952 KB |
Output is correct |
5 |
Correct |
171 ms |
47284 KB |
Output is correct |
6 |
Correct |
506 ms |
92128 KB |
Output is correct |
7 |
Correct |
9 ms |
33112 KB |
Output is correct |
8 |
Correct |
9 ms |
32344 KB |
Output is correct |
9 |
Correct |
3 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
9 ms |
32860 KB |
Output is correct |
12 |
Correct |
1 ms |
3164 KB |
Output is correct |
13 |
Correct |
37 ms |
14932 KB |
Output is correct |
14 |
Correct |
21 ms |
12112 KB |
Output is correct |
15 |
Correct |
19 ms |
14428 KB |
Output is correct |
16 |
Correct |
17 ms |
7772 KB |
Output is correct |
17 |
Correct |
94 ms |
27092 KB |
Output is correct |
18 |
Correct |
77 ms |
34388 KB |
Output is correct |
19 |
Correct |
64 ms |
31828 KB |
Output is correct |
20 |
Correct |
56 ms |
23636 KB |
Output is correct |
21 |
Correct |
143 ms |
45764 KB |
Output is correct |
22 |
Correct |
175 ms |
47356 KB |
Output is correct |
23 |
Correct |
180 ms |
38600 KB |
Output is correct |
24 |
Correct |
152 ms |
43092 KB |
Output is correct |
25 |
Correct |
370 ms |
79056 KB |
Output is correct |
26 |
Correct |
420 ms |
121952 KB |
Output is correct |
27 |
Correct |
483 ms |
97320 KB |
Output is correct |
28 |
Correct |
495 ms |
92124 KB |
Output is correct |
29 |
Correct |
504 ms |
90120 KB |
Output is correct |
30 |
Correct |
493 ms |
96268 KB |
Output is correct |
31 |
Correct |
383 ms |
65260 KB |
Output is correct |
32 |
Correct |
437 ms |
95248 KB |
Output is correct |