#include <bits/stdc++.h>
using namespace std;
const vector<int> dx = {-1, 1, 0, 0},
dy = {0, 0, -1, 1};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int h, w; cin >> h >> w;
vector<string> grid(h);
for (string& s : grid) cin >> s;
deque<pair<int, pair<int, int>>> dq;
dq.push_front({1, {0, 0}});
vector<vector<bool>> vis(h, vector<bool>(w));
int ans = 1;
while (!dq.empty()) {
int d = dq.front().first;
pair<int, int> pla = dq.front().second;
dq.pop_front();
if (vis[pla.first][pla.second]) continue;
vis[pla.first][pla.second] = true;
ans = max(ans, d);
for (int i = 0; i < 4; ++i) {
pair<int, int> ne = {pla.first + dx[i], pla.second + dy[i]};
if (ne.first < 0 || ne.first >= h || ne.second < 0 || ne.second >= w || grid[ne.first][ne.second] == '.') continue;
if (grid[pla.first][pla.second] == grid[ne.first][ne.second]) {
dq.push_front({d, ne});
} else {
dq.push_back({d + 1, ne});
}
}
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 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 |
10 ms |
1288 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
5 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
348 KB |
Output is correct |
14 |
Correct |
2 ms |
344 KB |
Output is correct |
15 |
Correct |
10 ms |
856 KB |
Output is correct |
16 |
Correct |
13 ms |
1140 KB |
Output is correct |
17 |
Correct |
7 ms |
836 KB |
Output is correct |
18 |
Correct |
10 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
856 KB |
Output is correct |
2 |
Correct |
36 ms |
2396 KB |
Output is correct |
3 |
Correct |
181 ms |
20312 KB |
Output is correct |
4 |
Correct |
42 ms |
5068 KB |
Output is correct |
5 |
Correct |
79 ms |
11480 KB |
Output is correct |
6 |
Correct |
1014 ms |
73752 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
35 ms |
2224 KB |
Output is correct |
14 |
Correct |
20 ms |
1628 KB |
Output is correct |
15 |
Correct |
13 ms |
1628 KB |
Output is correct |
16 |
Correct |
19 ms |
1336 KB |
Output is correct |
17 |
Correct |
103 ms |
5464 KB |
Output is correct |
18 |
Correct |
46 ms |
5212 KB |
Output is correct |
19 |
Correct |
40 ms |
4956 KB |
Output is correct |
20 |
Correct |
47 ms |
4700 KB |
Output is correct |
21 |
Correct |
113 ms |
11868 KB |
Output is correct |
22 |
Correct |
76 ms |
11564 KB |
Output is correct |
23 |
Correct |
188 ms |
10076 KB |
Output is correct |
24 |
Correct |
90 ms |
11612 KB |
Output is correct |
25 |
Correct |
224 ms |
20316 KB |
Output is correct |
26 |
Correct |
988 ms |
387172 KB |
Output is correct |
27 |
Correct |
976 ms |
107004 KB |
Output is correct |
28 |
Correct |
1028 ms |
73012 KB |
Output is correct |
29 |
Correct |
1004 ms |
66248 KB |
Output is correct |
30 |
Correct |
964 ms |
91256 KB |
Output is correct |
31 |
Correct |
598 ms |
15440 KB |
Output is correct |
32 |
Correct |
875 ms |
119332 KB |
Output is correct |