#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int H, W, dx[4]{0, 1, 0, -1}, dy[4]{1, 0, -1, 0};
cin >> H >> W;
char grid[H][W];
for(int i=0; i<H; ++i) {
for(int j=0; j<W; ++j) {
cin >> grid[i][j];
}
}
deque<pair<int, int>> dq;
int dist[H][W], ans=0;
memset(dist, 0, sizeof(dist));
dist[0][0]=1;
dq.push_front({0, 0});
while(!dq.empty()) {
pair<int, int> p=dq.front();
dq.pop_front();
int r=p.first, c=p.second;
ans=max(ans, dist[r][c]);
for(int i=0; i<4; ++i) {
int u=r+dx[i], v=c+dy[i];
if(u<0||u>=H||v<0||v>=W||dist[u][v]||grid[u][v]=='.')
continue;
if(grid[u][v]!=grid[r][c]) {
dist[u][v]=dist[r][c]+1;
dq.push_back({u, v});
}
else {
dist[u][v]=dist[r][c];
dq.push_front({u, v});
}
}
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
1748 KB |
Output is correct |
2 |
Correct |
0 ms |
324 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
6 ms |
1356 KB |
Output is correct |
5 |
Correct |
2 ms |
716 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
852 KB |
Output is correct |
13 |
Correct |
2 ms |
712 KB |
Output is correct |
14 |
Correct |
2 ms |
724 KB |
Output is correct |
15 |
Correct |
8 ms |
1692 KB |
Output is correct |
16 |
Correct |
9 ms |
1748 KB |
Output is correct |
17 |
Correct |
9 ms |
1620 KB |
Output is correct |
18 |
Correct |
5 ms |
1364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
36 ms |
9384 KB |
Output is correct |
3 |
Correct |
272 ms |
82796 KB |
Output is correct |
4 |
Correct |
71 ms |
20884 KB |
Output is correct |
5 |
Correct |
148 ms |
48504 KB |
Output is correct |
6 |
Correct |
607 ms |
96896 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
35 ms |
9416 KB |
Output is correct |
14 |
Correct |
24 ms |
5588 KB |
Output is correct |
15 |
Correct |
17 ms |
6228 KB |
Output is correct |
16 |
Correct |
18 ms |
4076 KB |
Output is correct |
17 |
Correct |
108 ms |
22064 KB |
Output is correct |
18 |
Correct |
76 ms |
21724 KB |
Output is correct |
19 |
Correct |
68 ms |
20548 KB |
Output is correct |
20 |
Correct |
63 ms |
19040 KB |
Output is correct |
21 |
Correct |
161 ms |
49696 KB |
Output is correct |
22 |
Correct |
156 ms |
48296 KB |
Output is correct |
23 |
Correct |
197 ms |
42080 KB |
Output is correct |
24 |
Correct |
148 ms |
48520 KB |
Output is correct |
25 |
Correct |
388 ms |
82164 KB |
Output is correct |
26 |
Correct |
585 ms |
112284 KB |
Output is correct |
27 |
Correct |
539 ms |
106024 KB |
Output is correct |
28 |
Correct |
576 ms |
96528 KB |
Output is correct |
29 |
Correct |
580 ms |
93844 KB |
Output is correct |
30 |
Correct |
571 ms |
99284 KB |
Output is correct |
31 |
Correct |
390 ms |
54716 KB |
Output is correct |
32 |
Correct |
542 ms |
106180 KB |
Output is correct |