#include <bits/stdc++.h>
using namespace std;
int dr[]={+1, 0, -1, 0};
int dc[]={0, +1, 0, -1};
int main()
{
iostream::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
char tracks[n][m];
for (int i=0;i<n;i++)
for (int j=0;j<m;j++)
cin >> tracks[i][j];
deque<pair<int,int> > q;
q.push_back({0,0});
int vis[n][m], rez=1;
memset(vis, 0, sizeof(vis));
vis[0][0]=1;
while (!q.empty()) {
int r=q.front().first, c=q.front().second;
q.pop_front();
rez=max(rez, vis[r][c]);
for (int i=0;i<4;i++) {
int nr=r+dr[i], nc=c+dc[i];
if (nr>=0&&nc>=0&&nr<n&&nc<m&&tracks[nr][nc]!='.'&&!vis[nr][nc]) {
if (tracks[nr][nc]!=tracks[r][c]) {
vis[nr][nc]=vis[r][c]+1;
q.push_back({nr, nc});
}
else {
vis[nr][nc]=vis[r][c];
q.push_front({nr, nc});
}
}
}
}
cout << rez;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1624 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
5 ms |
1372 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
908 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
8 ms |
1712 KB |
Output is correct |
16 |
Correct |
10 ms |
1628 KB |
Output is correct |
17 |
Correct |
9 ms |
1624 KB |
Output is correct |
18 |
Correct |
4 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
32 ms |
8020 KB |
Output is correct |
3 |
Correct |
245 ms |
78676 KB |
Output is correct |
4 |
Correct |
63 ms |
18772 KB |
Output is correct |
5 |
Correct |
172 ms |
44300 KB |
Output is correct |
6 |
Correct |
493 ms |
92612 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
2 ms |
764 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
33 ms |
8092 KB |
Output is correct |
14 |
Correct |
18 ms |
4696 KB |
Output is correct |
15 |
Correct |
15 ms |
5212 KB |
Output is correct |
16 |
Correct |
16 ms |
3416 KB |
Output is correct |
17 |
Correct |
85 ms |
20052 KB |
Output is correct |
18 |
Correct |
79 ms |
19976 KB |
Output is correct |
19 |
Correct |
66 ms |
18768 KB |
Output is correct |
20 |
Correct |
58 ms |
17244 KB |
Output is correct |
21 |
Correct |
140 ms |
45908 KB |
Output is correct |
22 |
Correct |
170 ms |
44308 KB |
Output is correct |
23 |
Correct |
182 ms |
38220 KB |
Output is correct |
24 |
Correct |
120 ms |
44880 KB |
Output is correct |
25 |
Correct |
259 ms |
78672 KB |
Output is correct |
26 |
Correct |
274 ms |
110432 KB |
Output is correct |
27 |
Correct |
381 ms |
113224 KB |
Output is correct |
28 |
Correct |
524 ms |
92488 KB |
Output is correct |
29 |
Correct |
505 ms |
90180 KB |
Output is correct |
30 |
Correct |
429 ms |
96204 KB |
Output is correct |
31 |
Correct |
372 ms |
50772 KB |
Output is correct |
32 |
Correct |
363 ms |
103048 KB |
Output is correct |