This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |