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>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

struct Edge {
  int d;
  int hight;
  int wide;
  Edge(int d, int h, int w) : d(d), hight(h), wide(w){};
};

bool operator<(const Edge &e1, const Edge &e2) { return e1.d > e2.d; }

int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;
  int gh, gw;
  cin >> gh >> gw;
  gh--;
  gw--;

  int dy[] = {0, -1, 0, 1};
  int dx[] = {-1, 0, 1, 0};

  vector<string> s(h);
  rep(i, h) cin >> s[i];

  int INF = 1001001001;
  vector<vector<int>> dist(h, vector<int>(w, INF));

  priority_queue<Edge> q;

  q.push(Edge(0, ch, cw));

  while (!q.empty()) {
    Edge e = q.top();
    q.pop();
    int nd = e.d;
    int nh = e.hight;
    int nw = e.wide;
    if (dist[nh][nw] != INF)
      continue;
    dist[nh][nw] = nd;
    rep(i, 4) {
      int dh = nh + dy[i];
      int dw = nw + dx[i];
      if (dh < 0 || dh >= h)
        continue;
      if (dw < 0 || dw >= w)
        continue;
      if (dist[dh][dw] != INF)
        continue;
      if (s[dh][dw] == '.') {
        // dist[dh][dw] = nd;
        q.push(Edge(nd, dh, dw));
      }
    }

    nd++;
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int dh = nh + i;
        int dw = nw + j;
        if (dh < 0 || dh >= h)
          continue;
        if (dw < 0 || dw >= w)
          continue;
        if (dist[dh][dw] != INF)
          continue;
        if (s[dh][dw] == '.') {
          // dist[dh][dw] = nd;
          q.push(Edge(nd, dh, dw));
        }
      }
    }
  }
  if (dist[gh][gw] == INF)
    cout << -1 << endl;
  else
    cout << dist[gh][gw] << endl;

  return 0;
}

Error: WA

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>;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int h, w;
int ch, cw;
int gh, gw;
ll inf = 1001001001;
ll ans = 1001001001;
// ll wp = 0;

vector<vector<char>> s(1005, vector<char>(1005));
vector<vector<bool>> seen(1005, vector<bool>(1005, false));
vector<vector<ll>> dp(1005, vector<ll>(1005, inf));
vector<int> dx = {0, -1, 1, 0};
vector<int> dy = {-1, 0, 0, 1};

int wpmove[20][2] = {{-2, -2}, {-1, -2}, {0, -2}, {1, -2}, {2, -2},
                     {-2, -1}, {-1, -1}, {1, -1}, {2, -1}, {-2, 0},
                     {2, 0},   {-2, 1},  {-1, 1}, {1, 1},  {2, 1},
                     {-2, 2},  {-1, 2},  {0, 2},  {1, 2},  {2, 2}};

void dfs(int i, int j, bool flag, ll wp) {
  /*
  if(seen[i][j]==true) {
      //if(flag) wp--;
      return;
  }
  */
  chmin(dp[i][j], wp);
  seen[i][j] = true;
  if (seen[gh][gw] == true) {
    // chmin(ans, wp);
    // if(flag) wp--;
    return;
  }

  for (int k = 0; k < 4; k++) {
    int dw, dh;
    dw = j + dx[k];
    dh = i + dy[k];
    if (dw < 0 || dw >= w)
      continue;
    if (dh < 0 || dh >= h)
      continue;
    if (s[dh][dw] == '.' && seen[dh][dw] == false) {
      dfs(dh, dw, false, dp[i][j]);
      // seen[i][j] = false;
    }
  }
  seen[i][j] = false;

  for (int k = 0; k < 20; k++) {
    int dw, dh;
    dw = j + wpmove[k][0];
    dh = i + wpmove[k][1];
    if (dw < 0 || dw >= w)
      continue;
    if (dh < 0 || dh >= h)
      continue;
    if (s[dh][dw] == '.' && seen[dh][dw] == false) {
      // wp++;
      dfs(dh, dw, true, dp[i][j] + 1);
      // seen[i][j] = false;
      // wp--;
    }
  }
  // seen[i][j] = false;
  return;
}

int main() {
  // int h, w;
  cin >> h >> w;

  // int ch, cw;
  cin >> ch >> cw;
  ch--;
  cw--;

  // int gh, gw;
  cin >> gh >> gw;
  gh--;
  gw--;

  // ll ans = 1001001001;
  /*
  vector<vector<char>> s(h, vector<char>(w));
  vector<vector<bool>> seen(h, vector<bool>(w, false));

  vector<int> dx = {0,-1,1,0};
  vector<int> dy = {-1,0,0,1};

  int wpmove[20][2] = {
      {-2, -2},
      {-1, -2},
      {0, -2},
      {1, -2},
      {2, -2},
      {-2, -1},
      {-1, -1},
      {1, -1},
      {2, -1},
      {-2, 0},
      {2, 0},
      {-2, 1},
      {-1, 1},
      {1, 1},
      {2, 1},
      {-2, 2},
      {-1, 2},
      {0, 2},
      {1, 2},
      {2, 2}
  };
  */

  rep(i, h) rep(j, w) cin >> s[i][j];

  ll wp = 0;
  /*
  void dfs = [&](int i, int j){
      seen[i][j] = true;
      if(seen[gh][gw]==true){
          chmin(ans, wp);
          return;
      }

      for(int k=0; k<4; k++){
          int dw, dh;
          dw = j + dx[k];
          dh = i + dy[k];
          if(dw<0 && dw>=w) continue;
          if(dh<0 && dh>=h) continue;
          if(s[dh][dw]=='.') dfs(dh,dw);
      }

      for(int k=0; k<20; k++){
          int dw, dh;
          dw = j + wpmove[k][0];
          dh = i + wpmove[k][1];
          if(dw<0 && dw>=w) continue;
          if(dh<0 && dh>=h) continue;
          if(s[dh][dw]=='.') {
              wp++;
              dfs(dh,dw);
          }
      }
  };
  */
  dfs(ch, cw, false, 0);
  if (dp[gh][gw] == inf)
    cout << -1 << endl;
  else
    cout << dp[gh][gw] << endl;
  return 0;
}