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;
#ifdef DEBUG
auto&operator<<(auto &o, pair<auto, auto> p) {o << "(" << p.first << ", " << p.second << ")"; return o;}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{"; for(auto e : x) o<<e<<", "; return o<<"}";}
#define debug(X) cerr << "["#X"]: " << X << '\n';
#else
#define cerr if(0)cout
#define debug(X) ;
#endif
using ll = long long;
#define all(v) (v).begin(), (v).end()
#define ssize(x) int(x.size())
#define fi first
#define se second
#define mp make_pair
#define eb emplace_back
const int inf = 1e9;
int bfs(vector<vector<char>> c, char z) {
int n = ssize(c), m = ssize(c[0]);
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
if (c[i][j] != z)
c[i][j] = '.';
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
vector<vector<int>> dist {
n,
vector<int>(m, inf)
};
deque<pair<int, int>> q;
q.push_back({0, 0});
dist[0][0] = 0;
while (ssize(q)) {
auto [x, y] = q.front();
q.pop_front();
for (int i = 0; i < 4; ++i) {
if (!(x + dx[i] >= 0 && x + dx[i] < n && y + dy[i] >= 0 && y+dy[i] < m) || dist[x+dx[i]][y+dy[i]] != inf) continue;
if (c[x][y] != c[x+dx[i]][y+dy[i]]) {
dist[x+dx[i]][y+dy[i]] = dist[x][y] + 1;
q.push_back({x+dx[i], y+dy[i]});
}
else {
dist[x+dx[i]][y+dy[i]] = dist[x][y];
q.push_front({x+dx[i], y+dy[i]});
}
}
}
int mx = -inf;
for (int i = 1; i < n-1; ++i)
for (int j = 1; j < m-1; ++j)
if (c[i][j] == z)
mx = max(mx, dist[i][j]);
return mx;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
int h, w;
cin >> h >> w;
vector<vector<char>> c {
h+2,
vector<char>(w+2, '.')
};
for (int i = 1; i <= h; ++i)
for (int j = 1; j <= w; ++j)
cin >> c[i][j];
int d1 = bfs(c, 'F'), d2 = bfs(c, 'R');
cout << d1 + d2 << '\n';
return 0;
}
Compilation message (stderr)
tracks.cpp: In function 'int bfs(std::vector<std::vector<char> >, char)':
tracks.cpp:32:3: warning: narrowing conversion of 'n' from 'int' to 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wnarrowing]
32 | n,
| ^
tracks.cpp: In function 'int main()':
tracks.cpp:71:4: warning: narrowing conversion of '(h + 2)' from 'int' to 'std::vector<std::vector<char> >::size_type' {aka 'long unsigned int'} [-Wnarrowing]
71 | h+2,
| ~^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |