#include <bits/stdc++.h>
using namespace std;
int rows[]{1, -1, 0, 0};
int cols[]{0, 0, 1, -1};
struct inf {
int r, c, d;
inf() {}
inf(int r, int c, int d) : r(r), c(c), d(d) {}
};
void bfs(vector<vector<char>> &grid, vector<vector<int>> &distances) {
deque<inf> dq;
dq.push_front(inf(0, 0, 1));
while (!dq.empty()) {
auto top = dq.front();
dq.pop_front();
if (distances[top.r][top.c] != 1e9)
continue;
distances[top.r][top.c] = top.d;
for (int i = 0; i < 4; ++i) {
int nrow = top.r + rows[i];
int ncol = top.c + cols[i];
if (nrow < 0 || ncol < 0 || nrow == grid.size() ||
ncol == grid[0].size())
continue;
if (grid[nrow][ncol] == '.')
continue;
if (grid[nrow][ncol] != grid[top.r][top.c]) {
dq.push_back(inf(nrow, ncol, top.d + 1));
} else {
dq.push_front(inf(nrow, ncol, top.d));
}
}
}
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<char>> grid(n, vector<char>(m, '.'));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
cin >> grid[i][j];
}
}
vector<vector<int>> distances(n, vector<int>(m, 1e9));
bfs(grid, distances);
int max_value = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if(grid[i][j] == '.') continue;
max_value = max(max_value, distances[i][j]);
}
}
cout << max_value << "\n";
}
Compilation message
tracks.cpp: In function 'void bfs(std::vector<std::vector<char> >&, std::vector<std::vector<int> >&)':
tracks.cpp:28:46: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | if (nrow < 0 || ncol < 0 || nrow == grid.size() ||
| ~~~~~^~~~~~~~~~~~~~
tracks.cpp:29:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | ncol == grid[0].size())
| ~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
2128 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
13 ms |
1832 KB |
Output is correct |
5 |
Correct |
5 ms |
848 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
508 KB |
Output is correct |
8 |
Correct |
2 ms |
336 KB |
Output is correct |
9 |
Correct |
2 ms |
396 KB |
Output is correct |
10 |
Correct |
4 ms |
848 KB |
Output is correct |
11 |
Correct |
4 ms |
848 KB |
Output is correct |
12 |
Correct |
8 ms |
1360 KB |
Output is correct |
13 |
Correct |
5 ms |
848 KB |
Output is correct |
14 |
Correct |
6 ms |
860 KB |
Output is correct |
15 |
Correct |
22 ms |
2128 KB |
Output is correct |
16 |
Correct |
21 ms |
2128 KB |
Output is correct |
17 |
Correct |
15 ms |
1940 KB |
Output is correct |
18 |
Correct |
14 ms |
2044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
848 KB |
Output is correct |
2 |
Correct |
80 ms |
9552 KB |
Output is correct |
3 |
Correct |
664 ms |
94560 KB |
Output is correct |
4 |
Correct |
171 ms |
22600 KB |
Output is correct |
5 |
Correct |
362 ms |
53320 KB |
Output is correct |
6 |
Correct |
1555 ms |
146224 KB |
Output is correct |
7 |
Correct |
3 ms |
848 KB |
Output is correct |
8 |
Correct |
3 ms |
1020 KB |
Output is correct |
9 |
Correct |
4 ms |
848 KB |
Output is correct |
10 |
Correct |
3 ms |
592 KB |
Output is correct |
11 |
Correct |
3 ms |
1016 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
82 ms |
9540 KB |
Output is correct |
14 |
Correct |
46 ms |
5704 KB |
Output is correct |
15 |
Correct |
40 ms |
6164 KB |
Output is correct |
16 |
Correct |
40 ms |
4176 KB |
Output is correct |
17 |
Correct |
219 ms |
24392 KB |
Output is correct |
18 |
Correct |
171 ms |
23864 KB |
Output is correct |
19 |
Correct |
171 ms |
22600 KB |
Output is correct |
20 |
Correct |
147 ms |
20728 KB |
Output is correct |
21 |
Correct |
415 ms |
55368 KB |
Output is correct |
22 |
Correct |
372 ms |
53320 KB |
Output is correct |
23 |
Correct |
463 ms |
46252 KB |
Output is correct |
24 |
Correct |
398 ms |
53832 KB |
Output is correct |
25 |
Correct |
682 ms |
94536 KB |
Output is correct |
26 |
Correct |
1300 ms |
445712 KB |
Output is correct |
27 |
Correct |
1463 ms |
235596 KB |
Output is correct |
28 |
Correct |
1523 ms |
146396 KB |
Output is correct |
29 |
Correct |
1512 ms |
142248 KB |
Output is correct |
30 |
Correct |
1441 ms |
176736 KB |
Output is correct |
31 |
Correct |
880 ms |
63048 KB |
Output is correct |
32 |
Correct |
1272 ms |
188352 KB |
Output is correct |