#include <bits/stdc++.h>
using namespace std;
int dr[]={+1, 0, -1, 0};
int dc[]={0, +1, 0, -1};
int main()
{
ios_base::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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
1624 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
4 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 |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
4 ms |
856 KB |
Output is correct |
13 |
Correct |
2 ms |
856 KB |
Output is correct |
14 |
Correct |
2 ms |
856 KB |
Output is correct |
15 |
Correct |
7 ms |
1460 KB |
Output is correct |
16 |
Correct |
8 ms |
1696 KB |
Output is correct |
17 |
Correct |
8 ms |
1624 KB |
Output is correct |
18 |
Correct |
4 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
34 ms |
8024 KB |
Output is correct |
3 |
Correct |
237 ms |
78672 KB |
Output is correct |
4 |
Correct |
59 ms |
18768 KB |
Output is correct |
5 |
Correct |
169 ms |
44308 KB |
Output is correct |
6 |
Correct |
509 ms |
92492 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
33 ms |
8028 KB |
Output is correct |
14 |
Correct |
20 ms |
4696 KB |
Output is correct |
15 |
Correct |
17 ms |
5308 KB |
Output is correct |
16 |
Correct |
16 ms |
3376 KB |
Output is correct |
17 |
Correct |
92 ms |
20292 KB |
Output is correct |
18 |
Correct |
72 ms |
19792 KB |
Output is correct |
19 |
Correct |
61 ms |
18768 KB |
Output is correct |
20 |
Correct |
57 ms |
17124 KB |
Output is correct |
21 |
Correct |
169 ms |
45888 KB |
Output is correct |
22 |
Correct |
171 ms |
44372 KB |
Output is correct |
23 |
Correct |
185 ms |
38224 KB |
Output is correct |
24 |
Correct |
142 ms |
44908 KB |
Output is correct |
25 |
Correct |
264 ms |
78676 KB |
Output is correct |
26 |
Correct |
285 ms |
110436 KB |
Output is correct |
27 |
Correct |
409 ms |
113112 KB |
Output is correct |
28 |
Correct |
499 ms |
92616 KB |
Output is correct |
29 |
Correct |
504 ms |
90444 KB |
Output is correct |
30 |
Correct |
449 ms |
96204 KB |
Output is correct |
31 |
Correct |
370 ms |
50904 KB |
Output is correct |
32 |
Correct |
362 ms |
102920 KB |
Output is correct |