#include <bits/stdc++.h>
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
using namespace std;
#define endl '\n'
#define ll long long
#define all(x) x.begin(), x.end()
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
int n, m;
inline bool valid(int x, int y) {
return (x >= 0 && x < n && y >= 0 && y < m);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
vector<string> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
deque<pair<int, int>> q;
q.push_front({0, 0});
bool visited[n + 2][m + 2];
int dist[n + 2][m + 2];
memset(visited, 0, sizeof(visited));
memset(dist, 0, sizeof(dist));
visited[0][0] = 1;
dist[0][0] = 1;
int ans = 1;
while (q.size() > 0) {
auto [x, y] = q.front();
q.pop_front();
ans = max(ans, dist[x][y]);
for (int d = 0; d < 4; d++) {
int nx = x + dx[d], ny = y + dy[d];
if (valid(nx, ny) && !visited[nx][ny] && v[nx][ny] != '.') {
visited[nx][ny] = 1;
dist[nx][ny] = dist[x][y] + (v[nx][ny] != v[x][y]);
if (v[nx][ny] != v[x][y]) q.push_back({nx, ny});
else q.push_front({nx, ny});
}
}
}
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
2076 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
5 ms |
1628 KB |
Output is correct |
5 |
Correct |
2 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
852 KB |
Output is correct |
13 |
Correct |
2 ms |
916 KB |
Output is correct |
14 |
Correct |
2 ms |
836 KB |
Output is correct |
15 |
Correct |
8 ms |
2096 KB |
Output is correct |
16 |
Correct |
9 ms |
2072 KB |
Output is correct |
17 |
Correct |
6 ms |
2004 KB |
Output is correct |
18 |
Correct |
5 ms |
1620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
724 KB |
Output is correct |
2 |
Correct |
28 ms |
11184 KB |
Output is correct |
3 |
Correct |
164 ms |
112044 KB |
Output is correct |
4 |
Correct |
47 ms |
26276 KB |
Output is correct |
5 |
Correct |
94 ms |
63064 KB |
Output is correct |
6 |
Correct |
587 ms |
125036 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
2 ms |
716 KB |
Output is correct |
9 |
Correct |
2 ms |
724 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
716 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
28 ms |
11092 KB |
Output is correct |
14 |
Correct |
16 ms |
6608 KB |
Output is correct |
15 |
Correct |
11 ms |
7252 KB |
Output is correct |
16 |
Correct |
17 ms |
4676 KB |
Output is correct |
17 |
Correct |
76 ms |
28500 KB |
Output is correct |
18 |
Correct |
52 ms |
27980 KB |
Output is correct |
19 |
Correct |
47 ms |
26316 KB |
Output is correct |
20 |
Correct |
39 ms |
24096 KB |
Output is correct |
21 |
Correct |
97 ms |
65064 KB |
Output is correct |
22 |
Correct |
95 ms |
63128 KB |
Output is correct |
23 |
Correct |
156 ms |
54312 KB |
Output is correct |
24 |
Correct |
94 ms |
63592 KB |
Output is correct |
25 |
Correct |
355 ms |
111976 KB |
Output is correct |
26 |
Correct |
619 ms |
135936 KB |
Output is correct |
27 |
Correct |
551 ms |
131772 KB |
Output is correct |
28 |
Correct |
582 ms |
124992 KB |
Output is correct |
29 |
Correct |
572 ms |
123896 KB |
Output is correct |
30 |
Correct |
572 ms |
123040 KB |
Output is correct |
31 |
Correct |
351 ms |
72136 KB |
Output is correct |
32 |
Correct |
552 ms |
124984 KB |
Output is correct |