#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
#define F first
#define S second
#define allv(v) v.begin(), v.end()
#define alla(a, N) a, a + N
inline void fastIO() {
cin.tie(nullptr) -> sync_with_stdio(false);
}
const int dx[4] = { 0, 1, 0, -1};
const int dy[4] = { 1, 0, -1, 0};
const int arraysize = 5005;
char grid[arraysize][arraysize];
bool vis[arraysize][arraysize];
int dis[arraysize][arraysize];
inline string YNEOS(bool b) {
return b ? "YES" : "NO";
}
inline string YNeos(bool b) {
return b ? "Yes" : "No";
}
void solve() {
int N, M; cin >> N >> M;
for (int i = 0; i < N; ++i)
for (int j = 0; j < M; ++j)
cin >> grid[i][j];
int mx = 0;
deque<pair<pii, int>> q;
// {{i, j}, dis}
q.emplace_front(pii{0, 0}, 1);
dis[0][0] = 0, vis[0][0] = true;
while (!q.empty()) {
auto [p, d] = q.front();
auto &[i, j] = p; q.pop_front();
mx = max(mx, (dis[i][j] = d));
for (int k = 0; k < 4; ++k) {
int x = i + dx[k], y = j + dy[k];
if (x < 0 || x >= N) continue;
if (y < 0 || y >= M) continue;
if (grid[x][y]=='.') continue;
if (vis[x][y]==true) continue;
vis[x][y] = true;
if (grid[x][y] == grid[i][j])
q.emplace_front(pii{x, y}, d);
else q.emplace_back(pii{x, y}, d+1);
}
}
cout << mx << '\n';
}
int main() {
fastIO();
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
7884 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
1 ms |
844 KB |
Output is correct |
4 |
Correct |
10 ms |
7628 KB |
Output is correct |
5 |
Correct |
5 ms |
4300 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
844 KB |
Output is correct |
8 |
Correct |
1 ms |
1100 KB |
Output is correct |
9 |
Correct |
1 ms |
1484 KB |
Output is correct |
10 |
Correct |
4 ms |
3660 KB |
Output is correct |
11 |
Correct |
4 ms |
3020 KB |
Output is correct |
12 |
Correct |
7 ms |
4436 KB |
Output is correct |
13 |
Correct |
5 ms |
4300 KB |
Output is correct |
14 |
Correct |
5 ms |
4300 KB |
Output is correct |
15 |
Correct |
14 ms |
7752 KB |
Output is correct |
16 |
Correct |
16 ms |
7860 KB |
Output is correct |
17 |
Correct |
13 ms |
7632 KB |
Output is correct |
18 |
Correct |
10 ms |
7568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
46612 KB |
Output is correct |
2 |
Correct |
62 ms |
22132 KB |
Output is correct |
3 |
Correct |
388 ms |
99276 KB |
Output is correct |
4 |
Correct |
120 ms |
43712 KB |
Output is correct |
5 |
Correct |
245 ms |
70648 KB |
Output is correct |
6 |
Correct |
869 ms |
154584 KB |
Output is correct |
7 |
Correct |
27 ms |
48668 KB |
Output is correct |
8 |
Correct |
26 ms |
46576 KB |
Output is correct |
9 |
Correct |
3 ms |
716 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
26 ms |
47760 KB |
Output is correct |
12 |
Correct |
2 ms |
2252 KB |
Output is correct |
13 |
Correct |
65 ms |
22084 KB |
Output is correct |
14 |
Correct |
38 ms |
15480 KB |
Output is correct |
15 |
Correct |
39 ms |
18976 KB |
Output is correct |
16 |
Correct |
27 ms |
8012 KB |
Output is correct |
17 |
Correct |
161 ms |
40336 KB |
Output is correct |
18 |
Correct |
170 ms |
47392 KB |
Output is correct |
19 |
Correct |
122 ms |
43728 KB |
Output is correct |
20 |
Correct |
95 ms |
35660 KB |
Output is correct |
21 |
Correct |
238 ms |
70936 KB |
Output is correct |
22 |
Correct |
239 ms |
70604 KB |
Output is correct |
23 |
Correct |
302 ms |
58296 KB |
Output is correct |
24 |
Correct |
228 ms |
67140 KB |
Output is correct |
25 |
Correct |
632 ms |
133520 KB |
Output is correct |
26 |
Correct |
1029 ms |
182728 KB |
Output is correct |
27 |
Correct |
882 ms |
171428 KB |
Output is correct |
28 |
Correct |
822 ms |
154584 KB |
Output is correct |
29 |
Correct |
818 ms |
150880 KB |
Output is correct |
30 |
Correct |
856 ms |
158336 KB |
Output is correct |
31 |
Correct |
601 ms |
95300 KB |
Output is correct |
32 |
Correct |
945 ms |
163476 KB |
Output is correct |