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

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int NIL = -1;
const int INF = (1 << 21);
const long long MOD = 1e9 + 7;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
  int h, w, sx, sy, gx, gy;
  cin >> h >> w >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vector<string> field(h);
  for (int i = 0; i < h; ++i)
    cin >> field[i];
  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<pair<int, int>> q;
  q.emplace_back(make_pair(sx, sy));
  dist[sx][sy] = 0;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop_front();
    int x = p.first;
    int y = p.second;
    for (int i = 0; i < 4; ++i) {
      int nx = x + di[i];
      int ny = y + dj[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (field[nx][ny] == '#')
        continue;
      if (dist[nx][ny] <= dist[x][y])
        continue;
      dist[nx][ny] = dist[x][y];
      q.emplace_front(make_pair(nx, ny));
    }
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        int nx = x + i;
        int ny = y + j;
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          continue;
        if (field[nx][ny] == '#')
          continue;
        if (dist[nx][ny] <= dist[x][y] + 1)
          continue;
        dist[nx][ny] = dist[x][y] + 1;
        q.emplace_back(make_pair(nx, ny));
      }
    }
  }
  if (dist[gx][gy] == INF)
    cout << -1 << endl;
  else
    cout << dist[gx][gy] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int NIL = -1;
const int INF = (1 << 21);
const long long MOD = 1e9 + 7;
const int di[] = {-1, 0, 1, 0};
const int dj[] = {0, -1, 0, 1};

int main() {
  int h, w, sx, sy, gx, gy;
  cin >> h >> w >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vector<string> field(h);
  for (int i = 0; i < h; ++i)
    cin >> field[i];
  vector<vector<int>> dist(h, vector<int>(w, INF));
  deque<pair<int, int>> q;
  q.emplace_back(make_pair(sx, sy));
  dist[sx][sy] = 0;
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop_front();
    int x = p.first;
    int y = p.second;
    for (int i = 0; i < 4; ++i) {
      int nx = x + di[i];
      int ny = y + dj[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w)
        continue;
      if (field[nx][ny] == '#')
        continue;
      if (dist[nx][ny] <= dist[x][y])
        continue;
      dist[nx][ny] = dist[x][y];
      q.emplace_front(make_pair(nx, ny));
    }
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        int nx = x + i;
        int ny = y + j;
        if (nx < 0 || nx >= h || ny < 0 || ny >= w)
          continue;
        if (field[nx][ny] == '#')
          continue;
        if (dist[nx][ny] <= dist[x][y] + 1)
          continue;
        dist[nx][ny] = dist[x][y] + 1;
        q.emplace_front(make_pair(nx, ny));
      }
    }
  }
  if (dist[gx][gy] == INF)
    cout << -1 << endl;
  else
    cout << dist[gx][gy] << endl;
}