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

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define f first
#define s second

using namespace std;
typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //	freopen("text","r",stdin);

  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vector<string> ar(h);
  for (auto &it : ar) {
    cin >> it;
  }
  int adjx[] = {1, 0, -1, 0};
  int adjy[] = {0, 1, 0, -1};
  vector<int> dp[h];
  for (int i = 0; i < h; i++) {
    dp[i] = vector<int>(w, 1e9);
  }
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  pq.push({0, {ch, cw}});
  dp[ch][cw] = 0;
  while (!pq.empty()) {
    auto nw = pq.top();
    pq.pop();
    for (int i = 0; i < 4; i++) {
      int nx = nw.s.f + adjx[i];
      int ny = nw.s.s + adjy[i];
      if (0 <= nx && nx < h && 0 <= ny && ny < w && ar[nx][ny] == '.' &&
          dp[nx][ny] > dp[nw.s.f][nw.s.s]) {
        dp[nx][ny] = dp[nw.s.f][nw.s.s];
        pq.push({dp[nx][ny], {nx, ny}});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = nw.s.f + i;
        int ny = nw.s.s + j;
        if (0 <= nx && nx < h && 0 <= ny && ny < w && ar[nx][ny] == '.' &&
            dp[nx][ny] > dp[nw.s.f][nw.s.s] + 1) {
          dp[nx][ny] = dp[nw.s.f][nw.s.s] + 1;
          pq.push({dp[nx][ny], {nx, ny}});
        }
      }
    }
  }
  //	for(int i=0;i<h;i++){
  //		for(int j=0;j<w;j++){
  //			cout << dp[i][j] << ' ';
  //		}
  //		cout << '\\n';
  //	}
  cout << (dp[dh][dw] == 1e9 ? -1 : dp[dh][dw]) << '\\n';

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define f first
#define s second

using namespace std;
typedef long long ll;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  //	freopen("text","r",stdin);

  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vector<string> ar(h);
  for (auto &it : ar) {
    cin >> it;
  }
  int adjx[] = {1, 0, -1, 0};
  int adjy[] = {0, 1, 0, -1};
  vector<int> dp[w];
  for (int i = 0; i < h; i++) {
    dp[i] = vector<int>(w, 1e9);
  }
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  pq.push({0, {dh, dw}});
  dp[dh][dw] = 0;
  while (!pq.empty()) {
    auto nw = pq.top();
    pq.pop();
    for (int i = 0; i < 4; i++) {
      int nx = nw.s.f + adjx[i];
      int ny = nw.s.s + adjy[i];
      if (0 <= nx && nx < h && 0 <= nx && nx < w && ar[nx][ny] == '.' &&
          dp[nx][ny] > dp[nw.s.f][nw.s.s]) {
        dp[nx][ny] = dp[nw.s.f][nw.s.s];
        pq.push({dp[nx][ny], {nx, ny}});
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = nw.s.f + i;
        int ny = nw.s.s + j;
        if (0 <= nx && nx < h && 0 <= nx && nx < w && ar[nx][ny] == '.' &&
            dp[nx][ny] > dp[nw.s.f][nw.s.s] + 1) {
          dp[nx][ny] = dp[nw.s.f][nw.s.s] + 1;
          pq.push({dp[nx][ny], {nx, ny}});
        }
      }
    }
  }
  cout << (dp[ch][cw] == 1e9 ? -1 : dp[ch][cw]) << '\\n';

  return 0;
}