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;
using ll = long long;
using vi = vector<int>;
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
using pi = pair<int, int>;
#define f first
#define s second
#define mp make_pair
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '"' << x << '"'; }
void __print(const string &x) { cerr << '"' << x << '"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
cerr << '{';
__print(x.first);
cerr << ',';
__print(x.second);
cerr << '}';
}
template <typename T>
void __print(const T &x) {
int f = 0;
cerr << '{';
for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
__print(t);
if (sizeof...(v)) cerr << ", ";
_print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...) \
cerr << "[" << #x << "] = ["; \
_print(x)
#else
#define debug(x...)
#endif
void setIO(string s = "") {
ios_base::sync_with_stdio(0);
cin.tie(0);
if ((int)s.size()) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
}
int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};
int depth[4000][4000];
string snow[4000];
int main() {
// 0/1 bfs
setIO();
int h, w;
cin >> h >> w;
for (int i = 0; i < h; i++) {
cin >> snow[i];
}
deque<pi> dq;
dq.push_back({0, 0});
depth[0][0] = 1;
int ans = 0;
while (!dq.empty()) {
pi top = dq.front();
dq.pop_front();
ans = max(ans, depth[top.f][top.s]);
for (int k = 0; k < 4; k++) {
int nx = top.f + dx[k], ny = top.s + dy[k];
if (nx < 0 || nx >= h || ny < 0 || ny >= w || depth[nx][ny] != 0 || snow[nx][ny] == '.') {
continue;
}
if (snow[nx][ny] == snow[top.f][top.s]) {
depth[nx][ny] = depth[top.f][top.s];
dq.push_front({nx, ny});
} else {
depth[nx][ny] = depth[top.f][top.s] + 1;
dq.push_back({nx, ny});
}
}
}
cout << ans;
return 0;
}
Compilation message (stderr)
tracks.cpp: In function 'void setIO(std::string)':
tracks.cpp:64:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tracks.cpp:65:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |