# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
716276 | nima_aryan | Tracks in the Snow (BOI13_tracks) | C++14 | 0 ms | 0 KiB |
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 LOCAL
#include "algo/debug.h"
#endif
using i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int H, W;
cin >> H >> W;
vector grid(H, vector<char>(W));
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
cin >> grid[i][j];
}
}
auto id = [&](int i, int j) {
return W * i + j;
};
auto di = [&](int i) {
return make_pair(i / W, i % W);
};
const int N = H * W;
vector<vector<int>> graph(N);
auto add_edge = [&](int a, int b) {
graph[a].push_back(b);
graph[b].push_back(a);
};
for (int i = 0; i < H; ++i) {
for (int j = 0; j < W; ++j) {
if (j + 1 < W) {
add_edge(id(i, j), id(i, j + 1));
}
if (i + 1 < H) {
add_edge(id(i, j), id(i + 1, j));
}
}
}
auto get = [&](int node) {
auto [i, j] = di(node);
return grid[i][j];
};
const int inf = (int) 1e9;
vector<int> dist(N, inf);
deque<int> dq;
auto add = [&](int u, int d, int ex = 0) {
if (ex) {
dq.push_back(u);
}
else {
dq.push_front(u);
}
dist[u] = d + ex;
};
add(0, 0);
int ans = 0;
while (!dq.empty()) {
int node = dq.front();
dq.pop_front();
ans = max(ans, dist[node]);
for (int neighbor : graph[node]) {
if (dist[neighbor] < inf) {
continue;
}
add(neighbor, dist[node], get(node) != get(neighbor));
}
}
cout << ans << '\n';
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
*/