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

#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;
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;
}

int bfs(int N, int M, vector<vector<char>> field, int sx, int sy, int gx,
        int gy);

int main() {
  int N, M, sx, sy, gx, gy;

  cin >> N;
  cin >> M;
  cin >> sx >> sy >> gx >> gy;
  vector<vector<char>> field(N, vector<char>(M));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      cin >> field.at(i).at(j);
    }
  }

  int ans = bfs(N, M, field, sx - 1, sy - 1, gx - 1, gy - 1);
  if (ans == INF) {
    cout << -1 << endl;
  } else
    cout << ans << endl;
}

int bfs(int N, int M, vector<vector<char>> field, int sx, int sy, int gx,
        int gy) {
  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  int d[N][M]; // 距離の配列
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      d[i][j] = INF; // 初期化
    }
  }
  deque<P> que;
  que.push_front(P(sx, sy));
  d[sx][sy] = 0;

  // キューが空になるまでループ
  while (que.size()) {
    P p = que.front();
    que.pop_front(); // 先頭を取り出す
    for (int i = 0; i < 4; i++) {
      int nx = p.fs + dx[i], ny = p.sc + dy[i];
      // nx,nyが進めるマスか、行ったことのないマスかどうかを判定
      if (0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] != '#' &&
          d[nx][ny] > d[p.fs][p.sc]) {
        que.push_front(P(nx, ny)); // 追加
        d[nx][ny] = d[p.fs][p.sc]; // 距離を確定
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (0 <= p.fs + i && p.fs + i < N && 0 <= p.sc + j && p.sc + j < M &&
            d[p.fs + i][p.sc + j] > d[p.fs][p.sc] + 1 &&
            field[p.fs + i][p.sc + j] != '#' && (i != 0 || j != 0)) {
          que.push_back(P(p.fs + i, p.sc + j));
          d[p.fs + i][p.sc + j] = d[p.fs][p.sc] + 1;
        }
      }
    }
  }
  return d[gx][gy];
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(), A.end()
#define RALL(A) A.rbegin(), A.rend()
typedef long long ll;
typedef pair<ll, ll> P;
const ll mod = 1000000007;
const ll LINF = 1LL << 60;
const int INF = 1 << 30;
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;
}

int bfs(int N, int M, vector<vector<char>> field, int sx, int sy, int gx,
        int gy);

int main() {
  int N, M, sx, sy, gx, gy;

  cin >> N;
  cin >> M;
  cin >> sx >> sy >> gx >> gy;
  vector<vector<char>> field(N, vector<char>(M));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      cin >> field.at(i).at(j);
    }
  }

  int ans = bfs(N, M, field, sx - 1, sy - 1, gx - 1, gy - 1);
  if (ans == INF) {
    cout << -1 << endl;
  } else
    cout << ans << endl;
}

int bfs(int N, int M, vector<vector<char>> field, int sx, int sy, int gx,
        int gy) {
  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  int d[N][M]; // 距離の配列
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      d[i][j] = INF; // 初期化
    }
  }
  queue<P> que;
  que.push(P(sx, sy));
  d[sx][sy] = 0;

  // キューが空になるまでループ
  while (que.size()) {
    P p = que.front();
    que.pop(); // 先頭を取り出す
    for (int i = 0; i < 4; i++) {
      int nx = p.fs + dx[i], ny = p.sc + dy[i];
      // nx,nyが進めるマスか、行ったことのないマスかどうかを判定
      if (0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] != '#' &&
          d[nx][ny] > d[p.fs][p.sc]) {
        que.push(P(nx, ny));       // 追加
        d[nx][ny] = d[p.fs][p.sc]; // 距離を確定
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (0 <= p.fs + i && p.fs + i < N && 0 <= p.sc + j && p.sc + j < M &&
            d[p.fs + i][p.sc + j] > d[p.fs][p.sc] + 1 &&
            field[p.fs + i][p.sc + j] != '#' && (i != 0 || j != 0)) {
          que.push(P(p.fs + i, p.sc + j));
          d[p.fs + i][p.sc + j] = d[p.fs][p.sc] + 1;
        }
      }
    }
  }
  return d[gx][gy];
}