Problem p02803 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }
template<typename T> void print(T a) { cout << a << endl; }
template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
class Range {
    using T = long long;
    class Iterator {
        T val, step, diff;
    public:
        Iterator(T v, T s, T d): val(v), step(s), diff(d) {}
        T operator*() { return val + diff; }
        bool operator!=(const Iterator &itr) { return step > 0 ? val < itr.val : val > itr.val; }
        Iterator operator++() { val += step; return *this; }
    };
    Iterator _begin, _end;
public:
    Range(T b, T e, T s, T d): _begin(b, s, d), _end(e, s, d) {
        if (s < 0) { _begin = Iterator(e - 1, s, d); _end = Iterator(b - 1, s, d); }
    }
    Range(T b, T e, T s): Range(b, e, s, 0) {}
    Range(T b, T e): Range(b, e, 1, 0) {}
    Range(T e): Range(0, e, 1, 0) {}
    Iterator begin() { return _begin; }
    Iterator end() { return _end; }
};
// clang-format on

#include <queue>
using T = tuple<int, int, int>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w;
  cin, h, w;
  auto mp = make_v(h + 2, w + 2, '#');
  for (auto i : Range(0, h, 1, 1)) {
    for (auto j : Range(0, w, 1, 1)) {
      cin, mp[i][j];
    }
  }

  const int d[5] = {0, 1, 0, -1, 0};

  auto bfs = [&](int x, int y) -> int {
    const ll inf = 1LL << 60;
    auto cost = make_v(h + 2, w + 2, inf);
    queue<T> q;
    q.emplace(x + 1, y + 1, 0);
    cost[y + 1][x + 1] = 0;
    int ans = 0;
    while (q.size()) {
      int nx, ny, ncost;
      tie(nx, ny, ncost) = q.front();
      q.pop();
      if (cost[ny][nx] < ncost)
        continue;
      chmax(ans, ncost);
      cost[ny][nx] = ncost;
      for (ll k : Range(4)) {
        int dx = nx + d[k];
        int dy = ny + d[k + 1];
        if (mp[dy][dx] == '#')
          continue;
        q.emplace(dx, dy, ncost + 1);
      }
    }
    return ans;
  };
  int ans = 0;
  for (auto i : Range(h)) {
    for (auto j : Range(w)) {
      if (mp[i + 1][j + 1] != '#') {
        chmax(ans, bfs(j, i));
      }
    }
  }
  print(ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
using namespace std;
using ll = long long int;
using P = pair<ll, ll>;

// clang-format off
#ifdef _DEBUG_
#define dump(...) do{ cerr << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; PPPPP(__VA_ARGS__); cerr << endl; } while(false)
template<typename T> void PPPPP(T t) { cerr << t; }
template<typename T, typename... S> void PPPPP(T t, S... s) { cerr << t << ", "; PPPPP(s...); }
#else
#define dump(...) do{ } while(false)
#endif
template<typename T> vector<T> make_v(size_t a, T b) { return vector<T>(a, b); }
template<typename... Ts> auto make_v(size_t a, Ts... ts) { return vector<decltype(make_v(ts...))>(a, make_v(ts...)); }
template<typename T> bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template<typename T> bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }
template<typename T> void print(T a) { cout << a << endl; }
template<typename T, typename... Ts> void print(T a, Ts... ts) { cout << a << ' '; print(ts...); }
template<typename T> istream &operator,(istream &in, T &t) { return in >> t; }
class Range {
    using T = long long;
    class Iterator {
        T val, step, diff;
    public:
        Iterator(T v, T s, T d): val(v), step(s), diff(d) {}
        T operator*() { return val + diff; }
        bool operator!=(const Iterator &itr) { return step > 0 ? val < itr.val : val > itr.val; }
        Iterator operator++() { val += step; return *this; }
    };
    Iterator _begin, _end;
public:
    Range(T b, T e, T s, T d): _begin(b, s, d), _end(e, s, d) {
        if (s < 0) { _begin = Iterator(e - 1, s, d); _end = Iterator(b - 1, s, d); }
    }
    Range(T b, T e, T s): Range(b, e, s, 0) {}
    Range(T b, T e): Range(b, e, 1, 0) {}
    Range(T e): Range(0, e, 1, 0) {}
    Iterator begin() { return _begin; }
    Iterator end() { return _end; }
};
// clang-format on

#include <queue>
using T = tuple<int, int, int>;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int h, w;
  cin, h, w;
  auto mp = make_v(h + 2, w + 2, '#');
  for (auto i : Range(0, h, 1, 1)) {
    for (auto j : Range(0, w, 1, 1)) {
      cin, mp[i][j];
    }
  }

  const int d[5] = {0, 1, 0, -1, 0};

  auto bfs = [&](int x, int y) -> int {
    const ll inf = 1LL << 60;
    auto cost = make_v(h + 2, w + 2, inf);
    queue<T> q;
    q.emplace(x + 1, y + 1, 0);
    cost[y + 1][x + 1] = 0;
    int ans = 0;
    while (q.size()) {
      int nx, ny, ncost;
      tie(nx, ny, ncost) = q.front();
      q.pop();
      if (cost[ny][nx] < ncost)
        continue;
      chmax(ans, ncost);
      cost[ny][nx] = ncost;
      for (ll k : Range(4)) {
        int dx = nx + d[k];
        int dy = ny + d[k + 1];
        if (mp[dy][dx] == '#')
          continue;
        if (cost[dy][dx] > ncost + 1) {
          cost[dy][dx] = ncost + 1;
          q.emplace(dx, dy, ncost + 1);
        }
      }
    }
    return ans;
  };
  int ans = 0;
  for (auto i : Range(h)) {
    for (auto j : Range(w)) {
      if (mp[i + 1][j + 1] != '#') {
        chmax(ans, bfs(j, i));
      }
    }
  }
  print(ans);
  return 0;
}