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

#include <bits/stdc++.h>

using namespace std;
int x, y, a, b;
int h, w;
const int INF = 10001000;
string s[1001];
vector<pair<int, int>> g[1000001];
#define mp make_pair
#define pb push_back
const int L = 1000;
int main() {

  cin >> h >> w;

  cin >> x >> y >> a >> b;
  x--;
  y--;
  a--;
  b--;

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

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int dx[] = {-1, 0, 1, 0};
      int dy[] = {0, 1, 0, -1};
      int v = i * L + j;
      for (int ii = 0; ii < 4; ii++) {
        int iii = i + dx[ii];
        int jjj = j + dy[ii];
        if (iii < 0 or jjj < 0 or iii >= h or jjj >= w)
          continue;
        if (s[iii][jjj] == '#')
          continue;
        int u = iii * L + jjj;
        g[v].pb(mp(0, u));
      }

      for (int p = -2; p < 3; p++) {
        for (int q = -2; q < 3; q++) {
          if (p == 0 and q == 0)
            continue;
          int ni = i + p;
          int nj = j + q;
          if (ni < 0 or nj < 0 or ni >= h or nj >= w)
            continue;
          if (s[ni][nj] == '#')
            continue;
          int u = ni * L + nj;
          g[v].pb(mp(1, u));
        }
      }
    }
  }

  int n = 1001 * 1000 + 1000;
  vector<int> d, p;
  d.assign(n, INF);

  int s = x * L + y;
  d[s] = 0;
  set<pair<int, int>> q;
  q.insert(mp(0, s));
  while (!q.empty()) {
    int v = q.begin()->second;
    q.erase(q.begin());
    for (auto edge : g[v]) {
      int to = edge.second;
      int len = edge.first;
      if (d[v] + len < d[to]) {
        q.erase(mp(d[to], to));
        d[to] = d[v] + len;
        q.insert(mp(d[to], to));
      }
    }
  }

  if (d[a * L + b] >= INF)
    cout << -1;
  else
    cout << d[a * L + b] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
int x, y, a, b;
int h, w;
const int INF = 10001000;
string s[1001];
vector<pair<int, int>> g[1000001];
#define mp make_pair
#define pb push_back
const int L = 1001;
int main() {

  cin >> h >> w;

  cin >> x >> y >> a >> b;
  x--;
  y--;
  a--;
  b--;

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

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      int dx[] = {-1, 0, 1, 0};
      int dy[] = {0, 1, 0, -1};
      int v = i * L + j;
      for (int ii = 0; ii < 4; ii++) {
        int iii = i + dx[ii];
        int jjj = j + dy[ii];
        if (iii < 0 or jjj < 0 or iii >= h or jjj >= w)
          continue;
        if (s[iii][jjj] == '#')
          continue;
        int u = iii * L + jjj;
        g[v].pb(mp(0, u));
      }

      for (int p = -2; p < 3; p++) {
        for (int q = -2; q < 3; q++) {
          if (p == 0 and q == 0)
            continue;
          int ni = i + p;
          int nj = j + q;
          if (ni < 0 or nj < 0 or ni >= h or nj >= w)
            continue;
          if (s[ni][nj] == '#')
            continue;
          int u = ni * L + nj;
          g[v].pb(mp(1, u));
        }
      }
    }
  }

  int n = 1000001;
  vector<int> d, p;
  d.assign(n, INF);

  int s = x * L + y;
  d[s] = 0;
  set<pair<int, int>> q;
  q.insert(mp(0, s));
  while (!q.empty()) {
    int v = q.begin()->second;
    q.erase(q.begin());
    for (auto edge : g[v]) {
      int to = edge.second;
      int len = edge.first;
      if (d[v] + len < d[to]) {
        q.erase(mp(d[to], to));
        d[to] = d[v] + len;
        q.insert(mp(d[to], to));
      }
    }
  }

  if (d[a * L + b] >= INF)
    cout << -1;
  else
    cout << d[a * L + b] << endl;
}