#include <bits/stdc++.h>
using namespace std;
const int INF = 2147483647;
int main() {
int H, W;
cin >> H >> W;
vector<string> meadow(H);
for (int i = 0; i < H; i++) cin >> meadow[i];
vector<vector<int>> dists(H, vector<int>(W, INF));
dists[0][0] = 1;
deque<pair<int, int>> q; q.push_back({0, 0});
pair<int, int> cur; char curC, newC;
while (!q.empty()) {
cur = q.front();
q.pop_front();
curC = meadow[cur.first][cur.second];
if (cur.first > 0) {
newC = meadow[cur.first - 1][cur.second];
if (newC == curC) {
if (dists[cur.first][cur.second] < dists[cur.first - 1][cur.second]) {
dists[cur.first - 1][cur.second] = dists[cur.first][cur.second];
q.push_front({cur.first - 1, cur.second});
}
} else if (newC != '.') {
if (dists[cur.first][cur.second] + 1 < dists[cur.first - 1][cur.second]) {
dists[cur.first - 1][cur.second] = dists[cur.first][cur.second] + 1;
q.push_back({cur.first - 1, cur.second});
}
}
}
if (cur.first < H - 1) {
newC = meadow[cur.first + 1][cur.second];
if (newC == curC) {
if (dists[cur.first][cur.second] < dists[cur.first + 1][cur.second]) {
dists[cur.first + 1][cur.second] = dists[cur.first][cur.second];
q.push_front({cur.first + 1, cur.second});
}
} else if (newC != '.') {
if (dists[cur.first][cur.second] + 1 < dists[cur.first + 1][cur.second]) {
dists[cur.first + 1][cur.second] = dists[cur.first][cur.second] + 1;
q.push_back({cur.first + 1, cur.second});
}
}
}
if (cur.second > 0) {
newC = meadow[cur.first][cur.second - 1];
if (newC == curC) {
if (dists[cur.first][cur.second] < dists[cur.first][cur.second - 1]) {
dists[cur.first][cur.second - 1] = dists[cur.first][cur.second];
q.push_front({cur.first, cur.second - 1});
}
} else if (newC != '.') {
if (dists[cur.first][cur.second] + 1 < dists[cur.first][cur.second - 1]) {
dists[cur.first][cur.second - 1] = dists[cur.first][cur.second] + 1;
q.push_back({cur.first, cur.second - 1});
}
}
}
if (cur.second < W - 1) {
newC = meadow[cur.first][cur.second + 1];
if (newC == curC) {
if (dists[cur.first][cur.second] < dists[cur.first][cur.second + 1]) {
dists[cur.first][cur.second + 1] = dists[cur.first][cur.second];
q.push_front({cur.first, cur.second + 1});
}
} else if (newC != '.') {
if (dists[cur.first][cur.second] + 1 < dists[cur.first][cur.second + 1]) {
dists[cur.first][cur.second + 1] = dists[cur.first][cur.second] + 1;
q.push_back({cur.first, cur.second + 1});
}
}
}
}
int ans = 1;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
if (dists[i][j] != INF) ans = max(ans, dists[i][j]);
}
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
2140 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
7 ms |
1628 KB |
Output is correct |
5 |
Correct |
3 ms |
760 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
3 ms |
708 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
5 ms |
812 KB |
Output is correct |
13 |
Correct |
3 ms |
860 KB |
Output is correct |
14 |
Correct |
3 ms |
804 KB |
Output is correct |
15 |
Correct |
12 ms |
2152 KB |
Output is correct |
16 |
Correct |
13 ms |
2184 KB |
Output is correct |
17 |
Correct |
10 ms |
1880 KB |
Output is correct |
18 |
Correct |
7 ms |
1624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
860 KB |
Output is correct |
2 |
Correct |
50 ms |
10524 KB |
Output is correct |
3 |
Correct |
374 ms |
109012 KB |
Output is correct |
4 |
Correct |
102 ms |
26212 KB |
Output is correct |
5 |
Correct |
217 ms |
55784 KB |
Output is correct |
6 |
Correct |
772 ms |
122352 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
864 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
49 ms |
10520 KB |
Output is correct |
14 |
Correct |
35 ms |
6740 KB |
Output is correct |
15 |
Correct |
24 ms |
7256 KB |
Output is correct |
16 |
Correct |
25 ms |
4376 KB |
Output is correct |
17 |
Correct |
135 ms |
27792 KB |
Output is correct |
18 |
Correct |
102 ms |
27472 KB |
Output is correct |
19 |
Correct |
101 ms |
26008 KB |
Output is correct |
20 |
Correct |
82 ms |
23888 KB |
Output is correct |
21 |
Correct |
218 ms |
57720 KB |
Output is correct |
22 |
Correct |
232 ms |
55892 KB |
Output is correct |
23 |
Correct |
249 ms |
47696 KB |
Output is correct |
24 |
Correct |
221 ms |
56820 KB |
Output is correct |
25 |
Correct |
651 ms |
109464 KB |
Output is correct |
26 |
Correct |
478 ms |
124048 KB |
Output is correct |
27 |
Correct |
599 ms |
126460 KB |
Output is correct |
28 |
Correct |
754 ms |
122096 KB |
Output is correct |
29 |
Correct |
743 ms |
122588 KB |
Output is correct |
30 |
Correct |
677 ms |
121300 KB |
Output is correct |
31 |
Correct |
574 ms |
61128 KB |
Output is correct |
32 |
Correct |
563 ms |
124652 KB |
Output is correct |