#include <iostream>
#include <deque>
#include <utility>
using namespace std;
int n,m,di[4] = {-1,0,1,0},dj[4] = {0,1,0,-1},ans = 0;
char ma[4003][4003];
int viz[4003][4003];
void bfs(int x,int y) {
viz[x][y] = 1;
deque<pair<int,int>> dq;
dq.emplace_front(x,y);
ans = max(ans,viz[x][y]);
while(!dq.empty()) {
pair<int,int> pi = dq.front();
dq.pop_front();
int x = pi.first,y = pi.second;
for (int k = 0;k<4;++k) {
int crtx = x+di[k],crty = y+dj[k];
if (crtx>=1&&crtx<=n&&crty>=1&&crty<=m&& ma[crtx][crty]!='.'&&viz[crtx][crty]==0) {
if (ma[crtx][crty]==ma[x][y]) {
viz[crtx][crty] = viz[x][y];
ans = max(ans,viz[crtx][crty]);
dq.emplace_front(crtx,crty);
} else {
dq.emplace_back(crtx,crty);
viz[crtx][crty] = viz[x][y]+1;
ans = max(ans,viz[crtx][crty]);
}
}
}
}
}
int main() {
cin>>n>>m;
for (int i = 1;i<=n;++i) {
for (int j = 1;j<=m;++j) {
cin>>ma[i][j];
}
}
bfs(1,1);
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
7248 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2640 KB |
Output is correct |
4 |
Correct |
10 ms |
7248 KB |
Output is correct |
5 |
Correct |
5 ms |
5968 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2640 KB |
Output is correct |
8 |
Correct |
1 ms |
2640 KB |
Output is correct |
9 |
Correct |
2 ms |
3068 KB |
Output is correct |
10 |
Correct |
5 ms |
3664 KB |
Output is correct |
11 |
Correct |
4 ms |
3408 KB |
Output is correct |
12 |
Correct |
7 ms |
5968 KB |
Output is correct |
13 |
Correct |
6 ms |
5980 KB |
Output is correct |
14 |
Correct |
5 ms |
5968 KB |
Output is correct |
15 |
Correct |
15 ms |
7328 KB |
Output is correct |
16 |
Correct |
15 ms |
7248 KB |
Output is correct |
17 |
Correct |
14 ms |
7248 KB |
Output is correct |
18 |
Correct |
10 ms |
7288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
32336 KB |
Output is correct |
2 |
Correct |
73 ms |
14664 KB |
Output is correct |
3 |
Correct |
568 ms |
58184 KB |
Output is correct |
4 |
Correct |
155 ms |
31816 KB |
Output is correct |
5 |
Correct |
320 ms |
47284 KB |
Output is correct |
6 |
Correct |
870 ms |
93344 KB |
Output is correct |
7 |
Correct |
16 ms |
32848 KB |
Output is correct |
8 |
Correct |
16 ms |
32336 KB |
Output is correct |
9 |
Correct |
4 ms |
2640 KB |
Output is correct |
10 |
Correct |
2 ms |
2552 KB |
Output is correct |
11 |
Correct |
16 ms |
32592 KB |
Output is correct |
12 |
Correct |
3 ms |
3156 KB |
Output is correct |
13 |
Correct |
71 ms |
14776 KB |
Output is correct |
14 |
Correct |
42 ms |
12224 KB |
Output is correct |
15 |
Correct |
41 ms |
14412 KB |
Output is correct |
16 |
Correct |
29 ms |
7504 KB |
Output is correct |
17 |
Correct |
167 ms |
27324 KB |
Output is correct |
18 |
Correct |
155 ms |
34120 KB |
Output is correct |
19 |
Correct |
160 ms |
31944 KB |
Output is correct |
20 |
Correct |
127 ms |
23636 KB |
Output is correct |
21 |
Correct |
346 ms |
45756 KB |
Output is correct |
22 |
Correct |
323 ms |
47176 KB |
Output is correct |
23 |
Correct |
344 ms |
38592 KB |
Output is correct |
24 |
Correct |
307 ms |
43080 KB |
Output is correct |
25 |
Correct |
589 ms |
78772 KB |
Output is correct |
26 |
Correct |
564 ms |
133720 KB |
Output is correct |
27 |
Correct |
772 ms |
123644 KB |
Output is correct |
28 |
Correct |
870 ms |
93344 KB |
Output is correct |
29 |
Correct |
844 ms |
89256 KB |
Output is correct |
30 |
Correct |
807 ms |
98240 KB |
Output is correct |
31 |
Correct |
575 ms |
65352 KB |
Output is correct |
32 |
Correct |
781 ms |
113900 KB |
Output is correct |