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

#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb push_back
#define eb emplace_back
#define fs first
#define sc second
using lint = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vii = vector<pii>;
const int INF = 1e9;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};

int h, w, ch, cw, dh, dw;
char s[1002][1002];

int dst[1002][1002];

int solve() {
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j)
      dst[i][j] = INF;
  dst[ch][cw] = 0;
  priority_queue<pair<int, pii>> pq;
  pq.emplace(0, pii(ch, cw));
  while (!pq.empty()) {
    int x = pq.top().sc.fs;
    int y = pq.top().sc.sc;
    int d = -pq.top().fs;
    pq.pop();
    if (dst[x][y] < d)
      continue;

    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 1 || nx > h || ny < 1 || ny > w || s[nx][ny] == '#')
        continue;
      if (dst[nx][ny] > d)
        dst[nx][ny] = d, pq.emplace(-d, pii(nx, ny));
    }
    for (int dx = -2; dx <= 2; ++dx)
      for (int dy = -2; dy <= 2; ++dy) {
        int nx = x + dx, ny = y + dy;
        if (nx < 1 || nx > h || ny < 1 || ny > w || s[nx][ny] == '#')
          continue;
        if (dst[nx][ny] > d + 1)
          dst[nx][ny] = d + 1, pq.emplace(-(d + 1), pii(nx, ny));
      }
  }
  return dst[dh][dw] == INF ? -1 : dst[dh][dw];
}

int main() {
  scanf("%d%d%d%d%d%d", &h, &w, &ch, &cw, &dh, &dw);
  for (int i = 1; i <= h; ++i)
    scanf("%s", s[i] + 1);

  int ans = solve();
  printf("%d", ans);
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb push_back
#define eb emplace_back
#define fs first
#define sc second
using lint = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vii = vector<pii>;
const int INF = 1e9;
const int dx[] = {-1, 1, 0, 0, -2, -2, -2, -2, -2, -1,
                  -1, 0, 0, 1, 1,  2,  2,  2,  2,  2};
const int dy[] = {0, 0,  -1, 1,  -2, -1, 0,  1, 2, -2,
                  2, -2, 2,  -2, 2,  -2, -1, 0, 1, 2};

int h, w, ch, cw, dh, dw;
char s[1002][1002];

int dst[1002][1002];

int solve() {
  for (int i = 1; i <= h; ++i)
    for (int j = 1; j <= w; ++j)
      dst[i][j] = INF;
  dst[ch][cw] = 0;
  priority_queue<pair<int, pii>> pq;
  pq.emplace(0, pii(ch, cw));
  while (!pq.empty()) {
    int x = pq.top().sc.fs;
    int y = pq.top().sc.sc;
    int d = -pq.top().fs;
    pq.pop();
    if (dst[x][y] < d)
      continue;

    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 1 || nx > h || ny < 1 || ny > w || s[nx][ny] == '#')
        continue;
      if (dst[nx][ny] > d)
        dst[nx][ny] = d, pq.emplace(-d, pii(nx, ny));
    }
    for (int i = 5; i < 21; ++i) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 1 || nx > h || ny < 1 || ny > w || s[nx][ny] == '#')
        continue;
      if (dst[nx][ny] > d + 1)
        dst[nx][ny] = d + 1, pq.emplace(-(d + 1), pii(nx, ny));
    }
  }
  return dst[dh][dw] == INF ? -1 : dst[dh][dw];
}

int main() {
  scanf("%d%d%d%d%d%d", &h, &w, &ch, &cw, &dh, &dw);
  for (int i = 1; i <= h; ++i)
    scanf("%s", s[i] + 1);

  int ans = solve();
  printf("%d", ans);
}