Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
typedef vector<int> vint;
typedef vector<ll> vll;
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec)
    is >> v;
  return is;
}
template <typename A, typename B> inline bool chmin(A &a, const B &b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename A, typename B> inline bool chmax(A &a, const B &b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

int main() {
  int H, W;
  cin >> H >> W;
  int sy, sx;
  cin >> sy >> sx;
  int gy, gx;
  cin >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;
  char s[H][W];
  REP(i, H) {
    REP(j, W) { cin >> s[i][j]; }
  }

  int dist[H][W];
  REP(i, H) {
    REP(j, W) { dist[i][j] = H * W * 2; }
  }
  deque<pair<int, int>> que;
  int dy[] = {-1, 0, 1, 0};
  int dx[] = {0, -1, 0, 1};

  dist[sy][sx] = 0;
  que.push_back({sy, sx});

  while (!que.empty()) {
    auto p = que.front();
    que.pop_front();

    int y = p.first, x = p.second;

    for (int dir = 0; dir < 4; dir++) {
      int ny = y + dy[dir], nx = x + dx[dir];

      if (ny < 0 || nx < 0 || ny >= H || nx >= W || '#' == s[ny][nx])
        continue;
      if (chmin(dist[ny][nx], dist[y][x]))
        que.push_front({ny, nx});
    }
    for (int yy = -2; yy < 3; yy++) {
      for (int xx = -2; xx < 3; xx++) {
        int ny = y + yy, nx = x + xx;

        if (ny < 0 || nx < 0 || ny >= H || nx >= W || '#' == s[ny][nx])
          continue;
        if (chmin(dist[ny][nx], dist[y][x] + 1))
          que.push_back({ny, nx});
      }
    }
  }
  if (dist[gy][gx] == H * W * 2) {
    cout << -1 << endl;
  } else {
    cout << dist[gy][gx] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
#define ALL(x) (x).begin(), (x).end()
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
typedef vector<int> vint;
typedef vector<ll> vll;
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec)
    is >> v;
  return is;
}
template <typename A, typename B> inline bool chmin(A &a, const B &b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <typename A, typename B> inline bool chmax(A &a, const B &b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}

int main() {
  int H, W;
  cin >> H >> W;
  int sy, sx;
  cin >> sy >> sx;
  int gy, gx;
  cin >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;
  char s[H][W];
  REP(i, H) {
    REP(j, W) { cin >> s[i][j]; }
  }

  int dist[H][W];
  REP(i, H) {
    REP(j, W) { dist[i][j] = H * W * 2; }
  }
  queue<pair<int, int>> que;
  int dy[] = {-1, 0, 1, 0};
  int dx[] = {0, -1, 0, 1};

  dist[sy][sx] = 0;
  que.push({sy, sx});

  while (!que.empty()) {
    auto p = que.front();
    que.pop();

    int y = p.first, x = p.second;

    for (int dir = 0; dir < 4; dir++) {
      int ny = y + dy[dir], nx = x + dx[dir];

      if (ny < 0 || nx < 0 || ny >= H || nx >= W || '#' == s[ny][nx])
        continue;
      if (chmin(dist[ny][nx], dist[y][x]))
        que.push({ny, nx});
    }
    for (int yy = -2; yy < 3; yy++) {
      for (int xx = -2; xx < 3; xx++) {
        int ny = y + yy, nx = x + xx;

        if (ny < 0 || nx < 0 || ny >= H || nx >= W || '#' == s[ny][nx])
          continue;
        if (chmin(dist[ny][nx], dist[y][x] + 1))
          que.push({ny, nx});
      }
    }
  }
  if (dist[gy][gx] == H * W * 2) {
    cout << -1 << endl;
  } else {
    cout << dist[gy][gx] << endl;
  }
}