Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define all(n) begin(n), end(n)
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
int main() {
  int H, W;
  cin >> H >> W;
  int Ch, Cw, Dh, Dw;
  cin >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  auto S = make_v<char>(H, W);
  auto dist = make_v<int>(H, W);
  fill_v(dist, 1000000000);
  dist[Ch][Cw] = 0;
  for (size_t i = 0; i < H; i++) {
    for (size_t j = 0; j < W; j++) {
      cin >> S[i][j];
    }
  }
  using P = pair<int, int>;
  using T = pair<P, int>;
  deque<T> deq;
  deq.push_back({{Ch, Cw}, 0});
  const int dx[] = {0, 1, 0, -1};
  const int dy[] = {1, 0, -1, 0};
  while (!deq.empty()) {
    auto [cur, cost] = deq.front();
    deq.pop_front();
    auto [y, x] = cur;
    if (dist[y][x] < cost)
      continue;
    //        dist[y][x]=cost;
    for (size_t i = 0; i < 4; i++) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (ny < H and 0 <= ny and nx < W and 0 <= nx and S[ny][nx] != '#') {
        if (dist[ny][nx] <= cost)
          continue;

        dist[ny][nx] = dist[y][x];
        deq.push_front({{ny, nx}, dist[y][x]});
      }
    }
    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        int ny = y + i;
        int nx = x + j;
        if (0 <= ny and ny < H and 0 <= nx and nx < W and S[ny][nx] != '#') {
          if (dist[ny][nx] <= cost + 1)
            continue;
          dist[ny][nx] = dist[y][x] + 1;
          deq.push_front({{ny, nx}, dist[y][x] + 1});
        }
      }
    }
  }
  cout << ((dist[Dh][Dw] < 1000000000) ? dist[Dh][Dw] : -1) << endl;
  /*    for (size_t i = 0; i < H; i++)
      {
          for (size_t j = 0; j < W; j++)
          {
              cout<<dist[i][j]<<" ";
          }
          cout<<endl;
      }*/

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define all(n) begin(n), end(n)
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef unsigned long long ull;
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 (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> vector<T> make_v(size_t a) { return vector<T>(a); }

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T &t, const V &v) {
  t = v;
}

template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T &t, const V &v) {
  for (auto &e : t)
    fill_v(e, v);
}
int main() {
  int H, W;
  cin >> H >> W;
  int Ch, Cw, Dh, Dw;
  cin >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;
  auto S = make_v<char>(H, W);
  auto dist = make_v<int>(H, W);
  fill_v(dist, 1000000000);
  dist[Ch][Cw] = 0;
  for (size_t i = 0; i < H; i++) {
    for (size_t j = 0; j < W; j++) {
      cin >> S[i][j];
    }
  }
  using P = pair<int, int>;
  using T = pair<P, int>;
  deque<T> deq;
  deq.push_back({{Ch, Cw}, 0});
  const int dx[] = {0, 1, 0, -1};
  const int dy[] = {1, 0, -1, 0};
  while (!deq.empty()) {
    auto [cur, cost] = deq.front();
    deq.pop_front();
    auto [y, x] = cur;
    if (dist[y][x] < cost)
      continue;
    //        dist[y][x]=cost;
    for (size_t i = 0; i < 4; i++) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (ny < H and 0 <= ny and nx < W and 0 <= nx and S[ny][nx] != '#') {
        if (dist[ny][nx] <= cost)
          continue;

        dist[ny][nx] = dist[y][x];
        deq.push_front({{ny, nx}, dist[y][x]});
      }
    }
    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        int ny = y + i;
        int nx = x + j;
        if (0 <= ny and ny < H and 0 <= nx and nx < W and S[ny][nx] != '#') {
          if (dist[ny][nx] <= cost + 1)
            continue;
          dist[ny][nx] = dist[y][x] + 1;
          deq.push_back({{ny, nx}, dist[y][x] + 1});
        }
      }
    }
  }
  cout << ((dist[Dh][Dw] < 1000000000) ? dist[Dh][Dw] : -1) << endl;
  /*    for (size_t i = 0; i < H; i++)
      {
          for (size_t j = 0; j < W; j++)
          {
              cout<<dist[i][j]<<" ";
          }
          cout<<endl;
      }*/

  return 0;
}