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>;

// const int di[] = {-1,0,1,0};
// const int dj[] = {0,-1,0,1};

pair<int, int> dhw[] = { // <h, w>
    {1, 0},   {0, -1}, {0, 1},  {-1, 0},  {2, -2},  {2, -1}, {2, 0},  {2, 1},
    {2, 2},   {1, -2}, {1, -1}, {1, 1},   {1, 2},   {0, -2}, {0, 2},  {-1, -2},
    {-1, -1}, {-1, 1}, {-1, 2}, {-2, -2}, {-2, -1}, {-2, 0}, {-2, 1}, {-2, 2}};

int main() {
  int h, w;
  cin >> h >> w;
  int si, sj;
  cin >> si >> sj;
  int ti, tj;
  cin >> ti >> tj;
  --si;
  --sj;
  --ti;
  --tj;
  vector<string> s(h);
  rep(i, h) cin >> s[i];

  const int INF = 1e9;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  dist[si][sj] = 0;
  deque<P> q;
  q.emplace_back(si, sj);

  while (!q.empty()) {
    int i = q.front().first;
    int j = q.front().second;
    int d = dist[i][j];
    q.pop_front();
    // if (dist[i][j] != d) continue;

    rep(v, 4) {
      // int ni = i+di[v], nj = j+dj[v];
      int ni = i + dhw[v].first, nj = j + dhw[v].second;
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d)
        continue;
      dist[ni][nj] = d;
      q.emplace_front(ni, nj);
    }

    // for (int ei = -2; ei <= 2; ++ei) {
    //   for (int ej = -2; ej <= 2; ++ej) {
    for (int v = 4; v < 24; ++v) {
      // int ni = i+ei, nj = j+ej;
      int ni = i + dhw[v].first, nj = j + dhw[v].second;
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d + 1)
        continue;
      dist[ni][nj] = d + 1;
      q.emplace_back(ni, nj);
      // }
    }
  }

  int ans = dist[ti][tj];
  if (ans == INF)
    ans = -1;
  cout << ans << endl;
}


Error: -6
free(): invalid pointer


Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <bitset>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define exout(x) printf("%.10f\\n", x)
const double pi = acos(-1.0);
const ll MOD = 1000000007;
const ll INF = 1e18;
const ll MAX_N = 201010;

// 最大公約数
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }

ll lcm(ll x, ll y) {
  if (x == 0 || y == 0)
    return 0;
  return (x / gcd(x, y) * y);
}

ll dx[4] = {0, 0, -1, 1};
ll dy[4] = {-1, 1, 0, 0};
ll ans[110][110];

// long longしか使わない
// 素数は1より大きい
// lower_boundは指定したkey以上の要素の一番左のイテレータをかえす
// upper_boundは指定したkeyより大きい要素の一番左のイテレータをかえす
int main() {
  ll h, w;
  cin >> h >> w;
  ll si, sj;
  cin >> si >> sj;
  ll ti, tj;
  cin >> ti >> tj;
  si--;
  sj--;
  ti--;
  sj--;
  vector<string> s(h);
  rep(i, h) { cin >> s[i]; }
  vector<vector<ll>> dist(h, vector<ll>(w, INF));
  deque<pair<ll, ll>> q;
  dist[si][sj] = 0;
  q.emplace_back(make_pair(si, sj));
  while (!q.empty()) {
    ll i = q.front().first;
    ll j = q.front().second;
    q.pop_front();
    ll d = dist[i][j];
    rep(v, 4) {
      ll ni = i + dx[v], nj = j + dy[v];
      if (ni < 0 || ni >= h || nj < 0 || nj >= w)
        continue;
      if (s[ni][nj] == '#')
        continue;
      if (dist[ni][nj] <= d)
        continue;
      dist[ni][nj] = d;
      q.emplace_front(make_pair(ni, nj));
    }
    for (ll ei = -2; ei <= 2; ++ei) {
      for (ll ej = -2; ej <= 2; ++ej) {
        ll ni = i + ei, nj = j + ej;
        if (ni < 0 || ni >= h || nj < 0 || nj >= w)
          continue;
        if (s[ni][nj] == '#')
          continue;
        if (dist[ni][nj] <= d + 1)
          continue;
        dist[ni][nj] = d + 1;
        q.emplace_back(make_pair(ni, nj));
      }
    }
  }
  ll ans = dist[ti][tj];
  if (ans == INF) {
    cout << -1 << endl;
    return 0;
  }
  cout << ans << endl;
  return 0;
}