Problem p02579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class c, class cmp = less<c>>
using ordered_set =
    tree<c, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <typename T, typename U>
pair<T, U> operator+(const pair<T, U> &l, const std::pair<T, U> &r) {
  return {l.first + r.first, l.second + r.second};
}
typedef long long int ll;
const ll mod = 998244353;
const int maxn = 3005;
#define endl '\n'
#define ld long double
#define int ll
#define all(x) (x).begin(), (x).end()
typedef long double dbl;
#define pw(x) (1LL << (x))
#define forn(i, n) for (int i = 0; i < (n); i++)
string s[maxn];
int h, w;
int get(int i, int j) { return i * w + j; }
int isvalid(int i, int j) {
  return (i >= 0 and j >= 0 and i < h and j < w and s[i][j] != '#');
}
vector<pair<int, int>> g[maxn];
vector<int> djikstra(ll src, ll n) {
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      pq;
  vector<ll> dist(n + 1, 1e18), vis(n + 1, 0);
  pq.push(make_pair(0, src));
  dist[src] = 0;
  while (!pq.empty()) {
    ll u = pq.top().second;
    pq.pop();
    if (vis[u])
      continue;
    vis[u] = 1;
    for (auto i : g[u]) {
      ll v = i.first;
      ll weight = i.second;
      if (dist[v] > dist[u] + weight) {
        dist[v] = dist[u] + weight;
        pq.push(make_pair(dist[v], v));
      }
    }
  }
  return dist;
}

int32_t main() {
  IOS

          cin >>
      h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;

  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int ii = max(0ll, i - 2); ii <= min(h - 1, i + 2); ii++) {
        for (int jj = max(0ll, j - 2); jj <= min(w - 1, j + 2); jj++) {
          if (isvalid(i, j) and isvalid(ii, jj))
            g[get(i, j)].push_back({get(ii, jj), 1});
        }
      }

      if (isvalid(i, j) and isvalid(i - 1, j))
        g[get(i, j)].push_back({get(i - 1, j), 0});

      if (isvalid(i, j) and isvalid(i, j - 1))
        g[get(i, j)].push_back({get(i, j - 1), 0});

      if (isvalid(i, j) and isvalid(i + 1, j))
        g[get(i, j)].push_back({get(i + 1, j), 0});

      if (isvalid(i, j) and isvalid(i, j + 1))
        g[get(i, j)].push_back({get(i, j + 1), 0});
    }
  }
  vector<int> dist = djikstra(get(ch, cw), h * w);
  if (dist[get(dh, dw)] < 1e15) {
    cout << dist[get(dh, dw)] << endl;
  } else {
    cout << -1 << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class c, class cmp = less<c>>
using ordered_set =
    tree<c, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  os << "[ ";
  for (auto v : V)
    os << v << " ";
  return os << "]";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <typename T, typename U>
pair<T, U> operator+(const pair<T, U> &l, const std::pair<T, U> &r) {
  return {l.first + r.first, l.second + r.second};
}
typedef long long int ll;
const ll mod = 998244353;
const int maxn = 3005;
#define endl '\n'
#define ld long double
#define int ll
#define all(x) (x).begin(), (x).end()
typedef long double dbl;
#define pw(x) (1LL << (x))
#define forn(i, n) for (int i = 0; i < (n); i++)
string s[maxn];
int h, w;
int get(int i, int j) { return i * w + j; }
int isvalid(int i, int j) {
  return (i >= 0 and j >= 0 and i < h and j < w and s[i][j] != '#');
}
vector<pair<int, int>> g[maxn * maxn];
vector<int> djikstra(ll src, ll n) {
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      pq;
  vector<ll> dist(n + 1, 1e18), vis(n + 1, 0);
  pq.push(make_pair(0, src));
  dist[src] = 0;
  while (!pq.empty()) {
    ll u = pq.top().second;
    pq.pop();
    if (vis[u])
      continue;
    vis[u] = 1;
    for (auto i : g[u]) {
      ll v = i.first;
      ll weight = i.second;
      if (dist[v] > dist[u] + weight) {
        dist[v] = dist[u] + weight;
        pq.push(make_pair(dist[v], v));
      }
    }
  }
  return dist;
}

int32_t main() {
  IOS

          cin >>
      h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;

  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      for (int ii = max(0ll, i - 2); ii <= min(h - 1, i + 2); ii++) {
        for (int jj = max(0ll, j - 2); jj <= min(w - 1, j + 2); jj++) {
          if (isvalid(i, j) and isvalid(ii, jj))
            g[get(i, j)].push_back({get(ii, jj), 1});
        }
      }

      if (isvalid(i, j) and isvalid(i - 1, j))
        g[get(i, j)].push_back({get(i - 1, j), 0});

      if (isvalid(i, j) and isvalid(i, j - 1))
        g[get(i, j)].push_back({get(i, j - 1), 0});

      if (isvalid(i, j) and isvalid(i + 1, j))
        g[get(i, j)].push_back({get(i + 1, j), 0});

      if (isvalid(i, j) and isvalid(i, j + 1))
        g[get(i, j)].push_back({get(i, j + 1), 0});
    }
  }
  vector<int> dist = djikstra(get(ch, cw), h * w);
  if (dist[get(dh, dw)] < 1e15) {
    cout << dist[get(dh, dw)] << endl;
  } else {
    cout << -1 << endl;
  }
}