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

#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

struct edge {
  int to, cost;
  edge(int x, int y) : to(x), cost(y) {}
};
typedef pair<int, int> P;
int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;
  int dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;
  vector<string> s(h);
  for (int i = 0; i < h; ++i) {
    cin >> s.at(i);
  }
  // id := i * w + j
  vector<vector<edge>> g(h * w);
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (s.at(i).at(j) != '.')
        continue;
      int n = i * w + j;
      int b = n + w;
      int r = n + 1;
      if (i < h - 1 && s.at(i + 1).at(j) == '.') {
        g.at(n).push_back(edge(b, 0));
        g.at(b).push_back(edge(n, 0));
      }
      if (j < w - 1 && s.at(i).at(j + 1) == '.') {
        g.at(n).push_back(edge(r, 0));
        g.at(r).push_back(edge(n, 0));
      }
      for (int k = -2; k <= 2; ++k) {
        for (int l = -2; l <= 2; ++l) {
          if (k == 0 && l == 0)
            continue;
          if (i + k < h && j + l < w && i + k >= 0 && j + l >= 0 &&
              s.at(i + k).at(j + l) == '.') {
            int x = (i + k) * w + j + l;
            g.at(n).push_back(edge(x, 1));
            g.at(x).push_back(edge(n, 1));
          }
        }
      }
    }
  }

  priority_queue<P, vector<P>, greater<P>> que;
  vector<int> d(h * w, 1000000);
  d.at(ch * w + cw) = 0;
  que.push(P(0, ch * w + cw));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d.at(v) < p.first)
      continue;
    for (int i = 0; i < g.at(v).size(); ++i) {
      edge e = g.at(v).at(i);
      if (d.at(e.to) > d.at(v) + e.cost) {
        d.at(e.to) = d.at(v) + e.cost;
        que.push(P(d.at(e.to), e.to));
      }
    }
  }
  if (d.at(dh * w + dw) >= 1000000) {
    cout << -1 << endl;
  } else {
    cout << d.at(dh * w + dw) << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;
  int dh, dw;
  cin >> dh >> dw;
  dh--;
  dw--;
  vector<string> s(h);
  for (int i = 0; i < h; ++i) {
    cin >> s.at(i);
  }
  // id := i * w + j
  vector<vector<int>> g(h * w, vector<int>(h * w, 1000000));
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (s.at(i).at(j) != '.')
        continue;
      int n = i * w + j;
      int b = n + w;
      int r = n + 1;
      if (i < h - 1 && s.at(i + 1).at(j) == '.') {
        g.at(n).at(b) = 0;
        g.at(b).at(n) = 0;
      }
      if (j < w - 1 && s.at(i).at(j + 1) == '.') {
        g.at(n).at(r) = 0;
        g.at(r).at(n) = 0;
      }
      for (int k = -2; k <= 2; ++k) {
        for (int l = -2; l <= 2; ++l) {
          if (k == 0 && l == 0)
            continue;
          if (i + k < h && j + l < w && i + k >= 0 && j + l >= 0 &&
              s.at(i + k).at(j + l) == '.') {
            int x = (i + k) * w + j + l;
            g.at(n).at(x) = min(g.at(n).at(x), 1);
            g.at(x).at(n) = min(g.at(x).at(n), 1);
          }
        }
      }
    }
  }

  vector<bool> used(h * w, false);
  vector<int> d(h * w, 1000000);

  d.at(ch * w + cw) = 0;

  while (true) {
    int v = -1;
    for (int u = 0; u < h * w; ++u) {
      if (!used.at(u) && (v == -1 || d.at(u) < d.at(v)))
        v = u;
    }
    // cout << v << endl;
    if (v == -1)
      break;
    used.at(v) = true;
    for (int u = 0; u < h * w; ++u) {
      d.at(u) = min(d.at(u), d.at(v) + g.at(v).at(u));
    }
  }
  if (d.at(dh * w + dw) >= 1000000) {
    cout << -1 << endl;
  } else {
    cout << d.at(dh * w + dw) << endl;
  }

  // for (auto x : g) {
  //   for (auto y : x) {
  //     printf("%7d  ", y);
  //   }
  //   cout << endl;
  // }
  //   cout << endl;

  return 0;
}