Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n)                                                          \\
  for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n)                                                         \\
  for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout << d << endl;
#define coutd(d) cout << std::setprecision(10) << d << endl;
#define cinline(n) getline(cin, n);
#define replace_all(s, b, a) replace(s.begin(), s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll, ll, ll>>;
using vb = vector<bool>;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll INF = 1e9 + 10;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;

const ll dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
const ll dx[] = {1, 0, -1, 0, 1, 1, -1, -1};

inline bool inside(ll y, ll x, ll H, ll W) {
  return (y >= 0 && x >= 0 && y < H && x < W);
}

char G[1005][1005];
int cost[1005][1005];

int visited[1005][1005];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w;
  cin >> h >> w;
  int sy, sx, gy, gx;
  cin >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  rep(i, h) rep(j, w) cin >> G[i][j];

  int id = 1;

  rep(i, h) {
    rep(j, w) {
      if (G[i][j] != '#' && visited[i][j] == 0) {
        queue<pll> q;
        q.emplace(i, j);
        visited[i][j] = id;
        id++;
        while (!q.empty()) {
          auto p = q.front();
          q.pop();
          ll y = p.first, x = p.second;
          rep(k, 4) {
            ll ni = y + dy[k], nj = x + dx[k];
            if (!inside(ni, nj, h, w))
              continue;
            if (G[ni][nj] != '#' && visited[ni][nj] == 0) {
              visited[ni][nj] = id;
              q.emplace(ni, nj);
            }
          }
        }
      }
    }
  }

  set<int> ids;
  rep(i, h) rep(j, w) cost[i][j] = INF;
  priority_queue<pair<ll, pll>, vector<pair<ll, pll>>, greater<pair<ll, pll>>>
      q;
  q.emplace(0, pll(sy, sx));
  cost[sy][sx] = 0;
  ids.insert(visited[sy][sx]);
  while (!q.empty()) {
    auto p = q.top();
    q.pop();
    ll c = p.first, y = p.second.first, x = p.second.second;
    if (c > cost[y][x])
      continue;
    rep(i, 4) {
      ll ny = y + dy[i], nx = x + dx[i];
      if (!inside(ny, nx, h, w))
        continue;
      if (G[ny][nx] != '#' && cost[ny][nx] > cost[y][x]) {
        cost[ny][nx] = cost[y][x];
        q.emplace(cost[ny][nx], pll(ny, nx));
      }
    }
    for (ll i = -2; i <= 2; i++) {
      for (ll j = -2; j <= 2; j++) {
        ll ny = y + i, nx = x + j;
        if (!inside(ny, nx, h, w))
          continue;
        if (G[ny][nx] != '#' && ids.count(G[ny][nx]) == 0) {
          if (cost[ny][nx] > cost[y][x] + 1) {
            cost[ny][nx] = cost[y][x] + 1;
            q.emplace(cost[ny][nx], pll(ny, nx));
          }
        }
      }
    }
  }

  ll ans = cost[gy][gx];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n)                                                          \\
  for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n)                                                         \\
  for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout << d << endl;
#define coutd(d) cout << std::setprecision(10) << d << endl;
#define cinline(n) getline(cin, n);
#define replace_all(s, b, a) replace(s.begin(), s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll, ll, ll>>;
using vb = vector<bool>;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll INF = 1e9 + 10;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;

const ll dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
const ll dx[] = {1, 0, -1, 0, 1, 1, -1, -1};

inline bool inside(ll y, ll x, ll H, ll W) {
  return (y >= 0 && x >= 0 && y < H && x < W);
}

char G[1005][1005];
int cost[1005][1005];
int visited[1005][1005];
int ids[1005][1005];

vll g[1005];
ll res[1005];
void dfs(ll v, ll p) {
  for (auto nv : g[v]) {
    if (nv == p)
      continue;
    chmin(res[nv], res[v] + 1);
    dfs(nv, v);
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w;
  cin >> h >> w;
  int sy, sx, gy, gx;
  cin >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  rep(i, h) rep(j, w) cin >> G[i][j];

  int id = 1;

  rep(i, h) {
    rep(j, w) {
      if (G[i][j] != '#' && ids[i][j] == 0) {
        queue<pll> q;
        q.emplace(i, j);
        ids[i][j] = id;
        id++;
        while (!q.empty()) {
          auto p = q.front();
          q.pop();
          ll y = p.first, x = p.second;
          rep(k, 4) {
            ll ni = y + dy[k], nj = x + dx[k];
            if (!inside(ni, nj, h, w))
              continue;
            if (G[ni][nj] != '#' && ids[ni][nj] == 0) {
              ids[ni][nj] = ids[i][j];
              q.emplace(ni, nj);
            }
          }
        }
      }
    }
  }

  // rep(i,h){
  //   rep(j,w) cout<<ids[i][j]<<' ';
  //   cout<<endl;
  // }

  rep(i, h) rep(j, w) cost[i][j] = INF;
  queue<pll> q;
  q.emplace(sy, sx);
  visited[sy][sx] = 1;

  set<pll> edge;
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int y = p.first, x = p.second;

    rep(i, 4) {
      int ny = y + dy[i], nx = x + dx[i];
      if (G[ny][nx] != '#' && inside(ny, nx, h, w)) {
        if (!visited[ny][nx]) {
          visited[ny][nx] = 1;
          q.emplace(ny, nx);
        }
      }
    }

    for (ll i = -2; i <= 2; i++) {
      for (ll j = -2; j <= 2; j++) {
        int ny = y + i, nx = x + j;
        if (!inside(ny, nx, h, w))
          continue;
        int id1 = ids[y][x], id2 = ids[ny][nx];
        if (id1 > id2)
          swap(id1, id2);
        if (G[ny][nx] != '#' && id1 != id2 && !edge.count(pll(id1, id2))) {
          if (!visited[ny][nx]) {
            visited[ny][nx] = 1;
            edge.insert(pll(id1, id2));
            q.emplace(ny, nx);
          }
        }
      }
    }
  }

  for (auto e : edge) {
    ll a = e.first, b = e.second;
    g[a].push_back(b);
    g[b].push_back(a);
    // cout<<a<<' '<<b<<endl;
  }

  rep(i, 1001) res[i] = INF;
  res[ids[sy][sx]] = 0;
  dfs(ids[sy][sx], -1);

  ll ans = res[ids[gy][gx]];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}