Submission #716290

#TimeUsernameProblemLanguageResultExecution timeMemory
716290nima_aryanTracks in the Snow (BOI13_tracks)C++14
Compilation error
0 ms0 KiB
#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);
   };
   auto get = [&](int node) {
      auto [i, j] = di(node);
      return grid[i][j];
   };
   const int N = H * W;
   vector<vector<int>> graph(N);
   auto add_edge = [&](int a, int b) {
      if (get(a) == '.' || get(b) == '.') {
         return;
      }
      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));
         }
      }
   }
   const int inf = (int) 1e9;
   vector<int> dist(N, inf);
   deque<int> dq;
   auto ad = [&](int node, int add, int extra = 0) {
      if (extra) {
         dq.push_back(node);
      }
      else {
         dq.push_front(node);
      }
      dist[node] = add + extra;
   };
   ad(0, 1);
   int ans = 1;
   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;
         }
         ad(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
 */

Compilation message (stderr)

tracks.cpp: In function 'int main()':
tracks.cpp:16:11: error: missing template arguments before 'grid'
   16 |    vector grid(H, vector<char>(W));
      |           ^~~~
tracks.cpp:19:17: error: 'grid' was not declared in this scope
   19 |          cin >> grid[i][j];
      |                 ^~~~
tracks.cpp: In lambda function:
tracks.cpp:29:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   29 |       auto [i, j] = di(node);
      |            ^
tracks.cpp:30:14: error: 'grid' was not declared in this scope; did you mean 'id'?
   30 |       return grid[i][j];
      |              ^~~~
      |              id