#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int h, w;
cin >> h >> w;
vector<string> s (h);
for (int i = 0; i < h; i++) {
cin >> s[i];
}
deque<pair<pair<short, short>, int>> q;
vector<vector<bool>> odw (h, vector<bool>(w, 0));
q.push_back({{0, 0}, 1});
int maxd = 0;
while (!q.empty()) {
auto x = q.front();
short i = x.first.first, j = x.first.second;
q.pop_front();
if (odw[i][j]) {
continue;
}
maxd = max(maxd, x.second);
odw[i][j] = 1;
if (i != 0 && !odw[i-1][j] && s[i-1][j] != '.') {
if (s[i][j] == s[i-1][j]) {
q.push_front({{i-1, j}, x.second});
}
else {
q.push_back({{i-1, j}, x.second+1});
}
}
if (j != 0 && !odw[i][j-1] && s[i][j-1] != '.') {
if (s[i][j] == s[i][j-1]) {
q.push_front({{i, j-1}, x.second});
}
else {
q.push_back({{i, j-1}, x.second+1});
}
}
if (i != h-1 && !odw[i+1][j] && s[i+1][j] != '.') {
if (s[i][j] == s[i+1][j]) {
q.push_front({{i+1, j}, x.second});
}
else {
q.push_back({{i+1, j}, x.second+1});
}
}
if (j != w-1 && !odw[i][j+1] && s[i][j+1] != '.') {
if (s[i][j] == s[i][j+1]) {
q.push_front({{i, j+1}, x.second});
}
else {
q.push_back({{i, j+1}, x.second+1});
}
}
}
cout << maxd << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
1116 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
4 ms |
1112 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
3 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
6 ms |
860 KB |
Output is correct |
16 |
Correct |
8 ms |
1112 KB |
Output is correct |
17 |
Correct |
4 ms |
860 KB |
Output is correct |
18 |
Correct |
4 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1116 KB |
Output is correct |
2 |
Correct |
22 ms |
3776 KB |
Output is correct |
3 |
Correct |
110 ms |
36056 KB |
Output is correct |
4 |
Correct |
23 ms |
8536 KB |
Output is correct |
5 |
Correct |
78 ms |
20284 KB |
Output is correct |
6 |
Correct |
455 ms |
61608 KB |
Output is correct |
7 |
Correct |
1 ms |
856 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
856 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
26 ms |
3960 KB |
Output is correct |
14 |
Correct |
12 ms |
2520 KB |
Output is correct |
15 |
Correct |
7 ms |
2668 KB |
Output is correct |
16 |
Correct |
14 ms |
1880 KB |
Output is correct |
17 |
Correct |
55 ms |
9408 KB |
Output is correct |
18 |
Correct |
26 ms |
9104 KB |
Output is correct |
19 |
Correct |
23 ms |
8536 KB |
Output is correct |
20 |
Correct |
26 ms |
7772 KB |
Output is correct |
21 |
Correct |
64 ms |
20764 KB |
Output is correct |
22 |
Correct |
81 ms |
20308 KB |
Output is correct |
23 |
Correct |
111 ms |
17820 KB |
Output is correct |
24 |
Correct |
63 ms |
20300 KB |
Output is correct |
25 |
Correct |
120 ms |
35924 KB |
Output is correct |
26 |
Correct |
338 ms |
127756 KB |
Output is correct |
27 |
Correct |
436 ms |
100584 KB |
Output is correct |
28 |
Correct |
452 ms |
61404 KB |
Output is correct |
29 |
Correct |
444 ms |
60056 KB |
Output is correct |
30 |
Correct |
424 ms |
70680 KB |
Output is correct |
31 |
Correct |
332 ms |
23832 KB |
Output is correct |
32 |
Correct |
361 ms |
82008 KB |
Output is correct |