#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = (int) 1e9;
vector<int> di = {-1, 0, 0, 1};
vector<int> dj = {0, -1, 1, 0};
void solve() {
int h, w;
cin >> h >> w;
vector<vector<char>> g(h, vector<char>(w));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> g[i][j];
}
}
vector<vector<int>> d(h, vector<int>(w, INF));
d[0][0] = 0;
deque<pair<int, int>> dq;
dq.push_front({0, 0});
while (!dq.empty()) {
auto p = dq.front();
dq.pop_front();
int i = p.first, j = p.second;
for (int e = 0; e < 4; e++) {
int ni = i + di[e], nj = j + dj[e];
if (ni < 0 || ni >= h || nj < 0 || nj >= w || g[ni][nj] == '.') continue;
int w = g[i][j] != g[ni][nj] ? 1 : 0;
if (d[i][j] + w < d[ni][nj]) {
d[ni][nj] = d[i][j] + w;
if (w == 1) dq.push_back({ni, nj});
else dq.push_front({ni, nj});
}
}
}
int ans = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (d[i][j] != INF && d[i][j] > ans) ans = d[i][j];
}
}
cout << ans + 1 << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
solve();
return 0;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
1880 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
6 ms |
1372 KB |
Output is correct |
5 |
Correct |
3 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 |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
600 KB |
Output is correct |
12 |
Correct |
4 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
1112 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
9 ms |
1884 KB |
Output is correct |
16 |
Correct |
11 ms |
1976 KB |
Output is correct |
17 |
Correct |
8 ms |
2136 KB |
Output is correct |
18 |
Correct |
6 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
856 KB |
Output is correct |
2 |
Correct |
40 ms |
9564 KB |
Output is correct |
3 |
Correct |
281 ms |
94672 KB |
Output is correct |
4 |
Correct |
77 ms |
22592 KB |
Output is correct |
5 |
Correct |
168 ms |
53484 KB |
Output is correct |
6 |
Correct |
664 ms |
109632 KB |
Output is correct |
7 |
Correct |
2 ms |
856 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
984 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
41 ms |
9708 KB |
Output is correct |
14 |
Correct |
26 ms |
5724 KB |
Output is correct |
15 |
Correct |
18 ms |
6284 KB |
Output is correct |
16 |
Correct |
20 ms |
4184 KB |
Output is correct |
17 |
Correct |
106 ms |
24404 KB |
Output is correct |
18 |
Correct |
79 ms |
23888 KB |
Output is correct |
19 |
Correct |
75 ms |
22416 KB |
Output is correct |
20 |
Correct |
64 ms |
20608 KB |
Output is correct |
21 |
Correct |
174 ms |
55376 KB |
Output is correct |
22 |
Correct |
165 ms |
53344 KB |
Output is correct |
23 |
Correct |
218 ms |
46376 KB |
Output is correct |
24 |
Correct |
165 ms |
54028 KB |
Output is correct |
25 |
Correct |
507 ms |
94676 KB |
Output is correct |
26 |
Correct |
473 ms |
122772 KB |
Output is correct |
27 |
Correct |
645 ms |
135224 KB |
Output is correct |
28 |
Correct |
718 ms |
109784 KB |
Output is correct |
29 |
Correct |
673 ms |
106728 KB |
Output is correct |
30 |
Correct |
653 ms |
109924 KB |
Output is correct |
31 |
Correct |
455 ms |
61000 KB |
Output is correct |
32 |
Correct |
582 ms |
117708 KB |
Output is correct |