#include <bits/stdc++.h>
using namespace std;
int dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
const int MAXN = 4000;
int n, m;
char a[MAXN][MAXN];
bool check (int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m && a[x][y] != '.';
}
int dist[MAXN][MAXN];
deque <pair <int, int>> cur;
signed main () {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> a[i][j];
cur.push_back({0, 0});
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) dist[i][j] = 1e9;
dist[0][0] = 1;
int ans = 1;
while (!cur.empty()) {
auto k = cur.front(); cur.pop_front();
ans = max(ans, dist[k.first][k.second]);
for (int i = 0; i < 4; i++) {
int x = k.first + dx[i], y = k.second + dy[i];
if (!check(x, y)) continue;
if (a[x][y] == a[k.first][k.second]) {
if (dist[k.first][k.second] < dist[x][y]) {
dist[x][y] = dist[k.first][k.second];
cur.push_front({x, y});
}
} else {
if (dist[k.first][k.second] + 1 < dist[x][y]) {
dist[x][y] = dist[k.first][k.second] + 1;
cur.push_back({x, y});
}
}
}
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
12888 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
2 ms |
4440 KB |
Output is correct |
4 |
Correct |
7 ms |
12888 KB |
Output is correct |
5 |
Correct |
3 ms |
8656 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4696 KB |
Output is correct |
10 |
Correct |
2 ms |
6492 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
8 ms |
8540 KB |
Output is correct |
13 |
Correct |
3 ms |
8540 KB |
Output is correct |
14 |
Correct |
3 ms |
8540 KB |
Output is correct |
15 |
Correct |
11 ms |
12864 KB |
Output is correct |
16 |
Correct |
13 ms |
12892 KB |
Output is correct |
17 |
Correct |
9 ms |
12632 KB |
Output is correct |
18 |
Correct |
7 ms |
12888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
78416 KB |
Output is correct |
2 |
Correct |
43 ms |
27224 KB |
Output is correct |
3 |
Correct |
251 ms |
78716 KB |
Output is correct |
4 |
Correct |
64 ms |
41620 KB |
Output is correct |
5 |
Correct |
160 ms |
62416 KB |
Output is correct |
6 |
Correct |
568 ms |
92004 KB |
Output is correct |
7 |
Correct |
10 ms |
78424 KB |
Output is correct |
8 |
Correct |
11 ms |
78480 KB |
Output is correct |
9 |
Correct |
3 ms |
4952 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
12 ms |
78476 KB |
Output is correct |
12 |
Correct |
1 ms |
6488 KB |
Output is correct |
13 |
Correct |
43 ms |
27100 KB |
Output is correct |
14 |
Correct |
24 ms |
20828 KB |
Output is correct |
15 |
Correct |
18 ms |
22872 KB |
Output is correct |
16 |
Correct |
21 ms |
10844 KB |
Output is correct |
17 |
Correct |
112 ms |
43612 KB |
Output is correct |
18 |
Correct |
77 ms |
43660 KB |
Output is correct |
19 |
Correct |
65 ms |
41420 KB |
Output is correct |
20 |
Correct |
60 ms |
37476 KB |
Output is correct |
21 |
Correct |
149 ms |
64200 KB |
Output is correct |
22 |
Correct |
173 ms |
62140 KB |
Output is correct |
23 |
Correct |
195 ms |
51956 KB |
Output is correct |
24 |
Correct |
138 ms |
64080 KB |
Output is correct |
25 |
Correct |
369 ms |
78928 KB |
Output is correct |
26 |
Correct |
430 ms |
122464 KB |
Output is correct |
27 |
Correct |
449 ms |
108852 KB |
Output is correct |
28 |
Correct |
591 ms |
92036 KB |
Output is correct |
29 |
Correct |
580 ms |
90196 KB |
Output is correct |
30 |
Correct |
536 ms |
96728 KB |
Output is correct |
31 |
Correct |
489 ms |
66548 KB |
Output is correct |
32 |
Correct |
435 ms |
100148 KB |
Output is correct |