#include <bits/stdc++.h>
using namespace std;
void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
}
const int NMAX = 4e3 + 3;
const int di[] = {-1, 0, 1, 0}, dj[] = {0, 1, 0, -1};
int N, M, dp[NMAX][NMAX];
char a[NMAX][NMAX];
bool inside(int lin, int col) {
return lin > 0 && col > 0 && lin <= N && col <= M;
}
void solve() {
cin >> N >> M;
for (int i = 1; i <= N; ++i)
cin >> (a[i] + 1);
dp[1][1] = 1;
deque<pair<int,int>> dq{make_pair(1, 1)};
int ans = 1;
while (!dq.empty()) {
int i, j;
tie(i, j) = dq.front();
dq.pop_front();
if (dp[i][j] > ans)
ans = dp[i][j];
for (int k = 0; k < 4; ++k) {
int iv = i + di[k], jv = j + dj[k];
if (!inside(iv, jv) || a[iv][jv] == '.')
continue;
int cost = a[i][j] != a[iv][jv];
if (dp[iv][jv] == 0) {
dp[iv][jv] = dp[i][j] + cost;
if (cost)
dq.push_back(make_pair(iv, jv));
else dq.push_front(make_pair(iv, jv));
}
}
}
cout << ans << '\n';
}
int main() {
fastIO();
solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
5196 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
7 ms |
5092 KB |
Output is correct |
5 |
Correct |
4 ms |
2764 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 |
3 ms |
2380 KB |
Output is correct |
11 |
Correct |
3 ms |
2128 KB |
Output is correct |
12 |
Correct |
5 ms |
3020 KB |
Output is correct |
13 |
Correct |
3 ms |
2764 KB |
Output is correct |
14 |
Correct |
3 ms |
2764 KB |
Output is correct |
15 |
Correct |
10 ms |
4940 KB |
Output is correct |
16 |
Correct |
12 ms |
5196 KB |
Output is correct |
17 |
Correct |
9 ms |
4940 KB |
Output is correct |
18 |
Correct |
7 ms |
5068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
30700 KB |
Output is correct |
2 |
Correct |
39 ms |
13456 KB |
Output is correct |
3 |
Correct |
166 ms |
57524 KB |
Output is correct |
4 |
Correct |
66 ms |
29116 KB |
Output is correct |
5 |
Correct |
113 ms |
44568 KB |
Output is correct |
6 |
Correct |
577 ms |
93284 KB |
Output is correct |
7 |
Correct |
18 ms |
32192 KB |
Output is correct |
8 |
Correct |
18 ms |
30688 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
18 ms |
31556 KB |
Output is correct |
12 |
Correct |
2 ms |
1540 KB |
Output is correct |
13 |
Correct |
40 ms |
13500 KB |
Output is correct |
14 |
Correct |
23 ms |
9464 KB |
Output is correct |
15 |
Correct |
22 ms |
12128 KB |
Output is correct |
16 |
Correct |
17 ms |
5088 KB |
Output is correct |
17 |
Correct |
101 ms |
24668 KB |
Output is correct |
18 |
Correct |
82 ms |
31692 KB |
Output is correct |
19 |
Correct |
68 ms |
29064 KB |
Output is correct |
20 |
Correct |
45 ms |
22088 KB |
Output is correct |
21 |
Correct |
104 ms |
43424 KB |
Output is correct |
22 |
Correct |
115 ms |
44628 KB |
Output is correct |
23 |
Correct |
181 ms |
35988 KB |
Output is correct |
24 |
Correct |
102 ms |
40888 KB |
Output is correct |
25 |
Correct |
413 ms |
78680 KB |
Output is correct |
26 |
Correct |
304 ms |
118996 KB |
Output is correct |
27 |
Correct |
434 ms |
108044 KB |
Output is correct |
28 |
Correct |
623 ms |
93116 KB |
Output is correct |
29 |
Correct |
558 ms |
89216 KB |
Output is correct |
30 |
Correct |
503 ms |
96968 KB |
Output is correct |
31 |
Correct |
451 ms |
63428 KB |
Output is correct |
32 |
Correct |
418 ms |
98404 KB |
Output is correct |