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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;

int main() {
  int H, W, ch, dh, cw, dw;
  cin >> H >> W >> ch >> cw >> dh >> dw;
  vector<vector<int>> S(H, vector<int>(W));
  char c;
  rep(i, H) rep(j, W) {
    cin >> c;
    S.at(i).at(j) = c == '.' ? -1 : -2;
  }

  deque<T> dq;
  dq.push_front(T(ch - 1, cw - 1, 0));

  int i, j, cost;
  while (!dq.empty()) {
    auto tp = dq.front();
    tie(i, j, cost) = tp;
    dq.pop_front();
    if (S.at(i).at(j) != -1)
      continue;

    S.at(i).at(j) = cost;
    if (i < H - 1 && S.at(i + 1).at(j) == -1)
      dq.push_front(T(i + 1, j, cost));
    if (j < W - 1 && S.at(i).at(j + 1) == -1)
      dq.push_front(T(i, j + 1, cost));
    if (i > 0 && S.at(i - 1).at(j) == -1)
      dq.push_front(T(i - 1, j, cost));
    if (j > 0 && S.at(i).at(j - 1) == -1)
      dq.push_front(T(i, j - 1, cost));

    for (int k = max(0, i - 2); k <= min(H - 1, i + 2); k++)
      for (int l = max(0, j - 2); l <= min(W - 1, j + 2); l++)
        if (S.at(k).at(l) == -1)
          dq.push_back(T(k, l, cost + 1));
  }

  cout << S.at(dh - 1).at(dw - 1) << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int cnt = 0;
int H, W;

void paint(vector<string> &v, int i, int j) {
  queue<P> que;
  que.push(P(i, j));

  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    v.at(p.first).at(p.second) = cnt + '0';
    if (p.first < H - 1 && v.at(p.first + 1).at(p.second) == '.')
      que.push(P(p.first + 1, p.second));
    if (p.second < W - 1 && v.at(p.first).at(p.second + 1) == '.')
      que.push(P(p.first, p.second + 1));
    if (p.first > 0 && v.at(p.first - 1).at(p.second) == '.')
      que.push(P(p.first - 1, p.second));
    if (p.second > 0 && v.at(p.first).at(p.second - 1) == '.')
      que.push(P(p.first, p.second - 1));
  }

  cnt++;
}

void find(vector<string> &s, vector<set<int>> &v, int i, int j) {
  char c = s.at(i).at(j);
  for (int k = max(0, i - 2); k <= min(H - 1, i + 2); k++)
    for (int l = max(0, j - 2); l <= min(W - 1, j + 2); l++) {
      char tmp = s.at(k).at(l);
      if (tmp != c && tmp != '#')
        v.at(c - '0').insert(tmp - '0');
    }
}

int main() {
  int ch, dh, cw, dw;
  cin >> H >> W >> ch >> cw >> dh >> dw;
  vector<string> S(H);
  rep(i, H) cin >> S.at(i);
  // int ans = -1;

  rep(i, H) rep(j, W) {
    if (S.at(i).at(j) == '.')
      paint(S, i, j);
  }

  vector<set<int>> V(cnt);
  rep(i, H) rep(j, W) {
    if (S.at(i).at(j) != '#')
      find(S, V, i, j);
  }

  int C = S.at(ch - 1).at(cw - 1) - '0';
  int D = S.at(dh - 1).at(dw - 1) - '0';

  vector<int> L(cnt, -1);
  int ans = 0;
  queue<int> q;
  q.push(C);
  L.at(C) = ans;
  while (!q.empty()) {
    int n = q.front();
    q.pop();
    set<int> st = V.at(n);
    for (auto itr = st.begin(); itr != st.end(); ++itr) {
      if (L.at(*itr) < 0) {
        L.at(*itr) = ans + 1;
        q.push(*itr);
      }
    }
    ans++;
  }
  cout << L.at(D) << endl;
}