#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
vector<int> dx = {1, -1, 0, 0};
vector<int> dy = {0, 0, 1, -1};
int32_t main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int N, M; cin >> N >> M;
vector<string> grid(N); for(auto &x : grid) cin >> x;
vector<vector<int>> depth(N, vector<int>(M, 0));
depth[0][0] = 1;
deque<pair<int, int>> dq;
dq.push_back({0, 0});
int mx = 1;
while(!dq.empty()) {
pair<int, int> cur = dq.front(); dq.pop_front();
mx = max(mx, depth[cur.first][cur.second]);
for(int i = 0; i < 4; i++) {
pair<int, int> next = {cur.first + dx[i], cur.second + dy[i]};
if(next.first < 0 || next.first >= N || next.second < 0 || next.second >= M) continue;
if(depth[next.first][next.second] == 0 && grid[next.first][next.second] != '.') {
if(grid[cur.first][cur.second] == grid[next.first][next.second]) {
depth[next.first][next.second] = depth[cur.first][cur.second];
dq.push_front({next.first, next.second});
} else {
depth[next.first][next.second] = depth[cur.first][cur.second] + 1;
dq.push_back({next.first, next.second});
}
}
}
}
cout << mx << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1872 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
5 ms |
1616 KB |
Output is correct |
5 |
Correct |
2 ms |
848 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
804 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
4 ms |
848 KB |
Output is correct |
13 |
Correct |
2 ms |
848 KB |
Output is correct |
14 |
Correct |
2 ms |
848 KB |
Output is correct |
15 |
Correct |
8 ms |
1872 KB |
Output is correct |
16 |
Correct |
9 ms |
1872 KB |
Output is correct |
17 |
Correct |
7 ms |
1872 KB |
Output is correct |
18 |
Correct |
5 ms |
1620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
848 KB |
Output is correct |
2 |
Correct |
29 ms |
9296 KB |
Output is correct |
3 |
Correct |
182 ms |
96864 KB |
Output is correct |
4 |
Correct |
55 ms |
21320 KB |
Output is correct |
5 |
Correct |
130 ms |
54208 KB |
Output is correct |
6 |
Correct |
434 ms |
110160 KB |
Output is correct |
7 |
Correct |
3 ms |
848 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
592 KB |
Output is correct |
11 |
Correct |
2 ms |
1020 KB |
Output is correct |
12 |
Correct |
1 ms |
592 KB |
Output is correct |
13 |
Correct |
29 ms |
9832 KB |
Output is correct |
14 |
Correct |
16 ms |
5788 KB |
Output is correct |
15 |
Correct |
12 ms |
6396 KB |
Output is correct |
16 |
Correct |
18 ms |
4176 KB |
Output is correct |
17 |
Correct |
79 ms |
24712 KB |
Output is correct |
18 |
Correct |
48 ms |
24232 KB |
Output is correct |
19 |
Correct |
47 ms |
22864 KB |
Output is correct |
20 |
Correct |
39 ms |
20928 KB |
Output is correct |
21 |
Correct |
98 ms |
55608 KB |
Output is correct |
22 |
Correct |
124 ms |
54408 KB |
Output is correct |
23 |
Correct |
158 ms |
46664 KB |
Output is correct |
24 |
Correct |
101 ms |
54720 KB |
Output is correct |
25 |
Correct |
233 ms |
96844 KB |
Output is correct |
26 |
Correct |
233 ms |
120508 KB |
Output is correct |
27 |
Correct |
308 ms |
123152 KB |
Output is correct |
28 |
Correct |
432 ms |
110148 KB |
Output is correct |
29 |
Correct |
410 ms |
108464 KB |
Output is correct |
30 |
Correct |
385 ms |
111748 KB |
Output is correct |
31 |
Correct |
350 ms |
62292 KB |
Output is correct |
32 |
Correct |
284 ms |
105904 KB |
Output is correct |