#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
#define fi first
#define se second
const int INF = 1e9;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, M;
cin >> N >> M;
vector<vector<char>> v(N, vector<char>(M));
for (int i = 0; i < N; i++) {
string s;
cin >> s;
for (int j = 0; j < M; j++) {
v[i][j] = s[j];
}
}
vector<vector<int>> d(N, vector<int>(M, INF));
d[0][0] = 1;
deque<pii> q;
q.push_front(pii(0, 0));
while (!q.empty()) {
pii cur = q.front();
q.pop_front();
for (int i = 0; i < 4; i++) {
int x = cur.fi + dx[i], y = cur.se + dy[i];
if (x < 0 || x >= N || y < 0 || y >= M || v[x][y] == '.') continue;
bool weight = (v[x][y] != v[cur.fi][cur.se]);
if (d[cur.fi][cur.se] + weight < d[x][y]) {
d[x][y] = d[cur.fi][cur.se] + weight;
if (weight) q.push_back(pii(x, y));
else q.push_front(pii(x, y));
}
}
}
int sol = 0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (v[i][j] != '.') sol = max(sol, d[i][j]);
}
}
cout << sol << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
1748 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
5 ms |
1412 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
320 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
720 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
4 ms |
852 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
2 ms |
836 KB |
Output is correct |
15 |
Correct |
8 ms |
1876 KB |
Output is correct |
16 |
Correct |
9 ms |
1856 KB |
Output is correct |
17 |
Correct |
6 ms |
1708 KB |
Output is correct |
18 |
Correct |
5 ms |
1484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
852 KB |
Output is correct |
2 |
Correct |
32 ms |
9608 KB |
Output is correct |
3 |
Correct |
190 ms |
94700 KB |
Output is correct |
4 |
Correct |
52 ms |
22496 KB |
Output is correct |
5 |
Correct |
101 ms |
53384 KB |
Output is correct |
6 |
Correct |
558 ms |
106244 KB |
Output is correct |
7 |
Correct |
2 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
848 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
30 ms |
9564 KB |
Output is correct |
14 |
Correct |
18 ms |
5712 KB |
Output is correct |
15 |
Correct |
12 ms |
6280 KB |
Output is correct |
16 |
Correct |
15 ms |
4052 KB |
Output is correct |
17 |
Correct |
92 ms |
24316 KB |
Output is correct |
18 |
Correct |
54 ms |
23908 KB |
Output is correct |
19 |
Correct |
52 ms |
22500 KB |
Output is correct |
20 |
Correct |
42 ms |
20640 KB |
Output is correct |
21 |
Correct |
106 ms |
55392 KB |
Output is correct |
22 |
Correct |
113 ms |
53256 KB |
Output is correct |
23 |
Correct |
156 ms |
45908 KB |
Output is correct |
24 |
Correct |
101 ms |
53868 KB |
Output is correct |
25 |
Correct |
362 ms |
94616 KB |
Output is correct |
26 |
Correct |
353 ms |
122648 KB |
Output is correct |
27 |
Correct |
479 ms |
121072 KB |
Output is correct |
28 |
Correct |
622 ms |
106252 KB |
Output is correct |
29 |
Correct |
564 ms |
107080 KB |
Output is correct |
30 |
Correct |
518 ms |
111356 KB |
Output is correct |
31 |
Correct |
374 ms |
61260 KB |
Output is correct |
32 |
Correct |
443 ms |
110524 KB |
Output is correct |