#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int, int> ii;
const int maxN = 4000;
const int di[] = {1, -1, 0, 0};
const int dj[] = {0, 0, 1, -1};
int n, m;
char a[maxN + 5][maxN + 5];
int d[maxN + 5][maxN + 5];
bool isInside(int i, int j){
return (i >= 1 && j >= 1 && i <= n && j <= m && a[i][j] != '.');
}
int bfs(){
int ret = 0;
deque<ii> q;
q.push_back({1, 1});
d[1][1] = 1;
while (!q.empty()){
ii t = q.front();
q.pop_front();
ret = max(d[t.x][t.y], ret);
for (int k = 0; k < 4; ++k){
int u = t.x + di[k];
int v = t.y + dj[k];
if (!isInside(u, v)) continue;
if (!d[u][v]){
if (a[u][v] == a[t.x][t.y]){
d[u][v] = d[t.x][t.y];
q.push_front({u, v});
}
else{
d[u][v] = d[t.x][t.y] + 1;
q.push_back({u, v});
}
}
}
}
return ret;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) cin >> a[i][j];
cout << bfs();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
5152 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
10 ms |
4976 KB |
Output is correct |
5 |
Correct |
5 ms |
2892 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
716 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
4 ms |
2376 KB |
Output is correct |
11 |
Correct |
4 ms |
2124 KB |
Output is correct |
12 |
Correct |
7 ms |
2892 KB |
Output is correct |
13 |
Correct |
4 ms |
2904 KB |
Output is correct |
14 |
Correct |
5 ms |
2892 KB |
Output is correct |
15 |
Correct |
14 ms |
4940 KB |
Output is correct |
16 |
Correct |
16 ms |
5208 KB |
Output is correct |
17 |
Correct |
13 ms |
4896 KB |
Output is correct |
18 |
Correct |
12 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
30796 KB |
Output is correct |
2 |
Correct |
64 ms |
13640 KB |
Output is correct |
3 |
Correct |
408 ms |
57756 KB |
Output is correct |
4 |
Correct |
112 ms |
28952 KB |
Output is correct |
5 |
Correct |
281 ms |
44464 KB |
Output is correct |
6 |
Correct |
847 ms |
92160 KB |
Output is correct |
7 |
Correct |
20 ms |
32204 KB |
Output is correct |
8 |
Correct |
19 ms |
30724 KB |
Output is correct |
9 |
Correct |
4 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
18 ms |
31564 KB |
Output is correct |
12 |
Correct |
3 ms |
1484 KB |
Output is correct |
13 |
Correct |
65 ms |
13520 KB |
Output is correct |
14 |
Correct |
35 ms |
9476 KB |
Output is correct |
15 |
Correct |
34 ms |
12016 KB |
Output is correct |
16 |
Correct |
26 ms |
4980 KB |
Output is correct |
17 |
Correct |
171 ms |
24748 KB |
Output is correct |
18 |
Correct |
134 ms |
31720 KB |
Output is correct |
19 |
Correct |
118 ms |
28924 KB |
Output is correct |
20 |
Correct |
97 ms |
22140 KB |
Output is correct |
21 |
Correct |
235 ms |
43460 KB |
Output is correct |
22 |
Correct |
281 ms |
44468 KB |
Output is correct |
23 |
Correct |
284 ms |
36228 KB |
Output is correct |
24 |
Correct |
269 ms |
40844 KB |
Output is correct |
25 |
Correct |
654 ms |
78660 KB |
Output is correct |
26 |
Correct |
505 ms |
119044 KB |
Output is correct |
27 |
Correct |
703 ms |
105272 KB |
Output is correct |
28 |
Correct |
817 ms |
92224 KB |
Output is correct |
29 |
Correct |
828 ms |
90616 KB |
Output is correct |
30 |
Correct |
722 ms |
94956 KB |
Output is correct |
31 |
Correct |
602 ms |
63476 KB |
Output is correct |
32 |
Correct |
635 ms |
90576 KB |
Output is correct |