#include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
using namespace std;
int dx[] = { -1,0,1,0 };
int dy[] = { 0,-1,0,1 };
int main() {
int n, m;
cin >> n >> m;
vector<vector<char>> grid(n + 2, vector<char>(m + 2, '.'));
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> grid[i][j];
}
}
vector<vector<int>> bfs(n + 2, vector<int>(m + 2, 1e9));
bfs[1][1] = 1;
deque<pair<int, int>> cur;
cur.push_back(make_pair(1, 1));
while (cur.size()) {
auto c = cur.front();
cur.pop_front();
for (int i = 0; i < 4; i++) {
int nx = c.first + dx[i];
int ny = c.second + dy[i];
int w = 1;
if (grid[nx][ny] == grid[c.first][c.second]) {
w = 0;
}
if (grid[nx][ny] == '.') {
w = 1e9;
}
if (w != 1e9) {
if (bfs[nx][ny] > bfs[c.first][c.second] + w) {
bfs[nx][ny] = bfs[c.first][c.second] + w;
if (w == 1) {
cur.push_back(make_pair(nx, ny));
}
else {
cur.push_front(make_pair(nx, ny));
}
}
}
}
}
int ans = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (grid[i][j] != '.') {
ans = max(ans, bfs[i][j]);
}
}
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
1624 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
7 ms |
1372 KB |
Output is correct |
5 |
Correct |
4 ms |
860 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 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 |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
4 ms |
860 KB |
Output is correct |
13 |
Correct |
3 ms |
860 KB |
Output is correct |
14 |
Correct |
3 ms |
860 KB |
Output is correct |
15 |
Correct |
12 ms |
1628 KB |
Output is correct |
16 |
Correct |
12 ms |
1628 KB |
Output is correct |
17 |
Correct |
10 ms |
1624 KB |
Output is correct |
18 |
Correct |
7 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
860 KB |
Output is correct |
2 |
Correct |
58 ms |
8152 KB |
Output is correct |
3 |
Correct |
520 ms |
78932 KB |
Output is correct |
4 |
Correct |
140 ms |
18768 KB |
Output is correct |
5 |
Correct |
315 ms |
44708 KB |
Output is correct |
6 |
Correct |
979 ms |
92880 KB |
Output is correct |
7 |
Correct |
2 ms |
1112 KB |
Output is correct |
8 |
Correct |
3 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
756 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
59 ms |
8188 KB |
Output is correct |
14 |
Correct |
34 ms |
4932 KB |
Output is correct |
15 |
Correct |
33 ms |
5212 KB |
Output is correct |
16 |
Correct |
27 ms |
3420 KB |
Output is correct |
17 |
Correct |
159 ms |
20340 KB |
Output is correct |
18 |
Correct |
129 ms |
20036 KB |
Output is correct |
19 |
Correct |
131 ms |
18776 KB |
Output is correct |
20 |
Correct |
112 ms |
17236 KB |
Output is correct |
21 |
Correct |
295 ms |
46276 KB |
Output is correct |
22 |
Correct |
311 ms |
44624 KB |
Output is correct |
23 |
Correct |
305 ms |
38484 KB |
Output is correct |
24 |
Correct |
311 ms |
45012 KB |
Output is correct |
25 |
Correct |
563 ms |
78948 KB |
Output is correct |
26 |
Correct |
507 ms |
110624 KB |
Output is correct |
27 |
Correct |
674 ms |
107544 KB |
Output is correct |
28 |
Correct |
786 ms |
92880 KB |
Output is correct |
29 |
Correct |
787 ms |
93660 KB |
Output is correct |
30 |
Correct |
752 ms |
97952 KB |
Output is correct |
31 |
Correct |
538 ms |
51284 KB |
Output is correct |
32 |
Correct |
650 ms |
97052 KB |
Output is correct |