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

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
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 dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
const int INF = 1e9;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> dp(h, vector<int>(w, INF));
  rep(i, h) rep(j, w) {
    if (s[i][j] == '#')
      dp[i][j] = -1;
  }
  queue<P> que, que2;
  que.push(P(ch, cw));
  dp[ch][cw] = 0;
  while (!que.empty()) {
    int y = que.front().first;
    int x = que.front().second;
    que.pop();
    que2.push(P(y, x));
    rep(i, 4) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (ny < 0 || h <= ny)
        continue;
      if (nx < 0 || w <= nx)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (dp[ny][nx] <= dp[y][x])
        continue;
      dp[ny][nx] = dp[y][x];
      que.push(P(ny, nx));
    }
    if (que.empty()) {
      while (!que2.empty()) {
        int y = que2.front().first;
        int x = que2.front().second;
        que2.pop();
        for (int i = -2; i <= 2; ++i) {
          int ny = y + i;
          if (ny < 0 || h <= ny)
            continue;
          for (int j = -2; j <= 2; ++j) {
            int nx = x + j;
            if (nx < 0 || w <= nx)
              continue;
            if (s[ny][nx] == '#')
              continue;
            if (dp[ny][nx] <= dp[y][x] + 1)
              continue;
            dp[ny][nx] = dp[y][x] + 1;
            que.push(P(ny, nx));
          }
        }
      }
    }
  }
  if (dp[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << dp[dh][dw] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
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 dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
const int INF = 1e9;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  vector<vector<int>> dp(h, vector<int>(w, INF));
  queue<P> que;
  que.push(P(ch, cw));
  dp[ch][cw] = 0;
  while (!que.empty()) {
    int y = que.front().first;
    int x = que.front().second;
    que.pop();
    rep(i, 4) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (ny < 0 || h <= ny)
        continue;
      if (nx < 0 || w <= nx)
        continue;
      if (s[ny][nx] == '#')
        continue;
      if (dp[ny][nx] <= dp[y][x])
        continue;
      dp[ny][nx] = dp[y][x];
      que.push(P(ny, nx));
    }
    for (int i = -2; i <= 2; ++i) {
      int ny = y + i;
      if (ny < 0 || h <= ny)
        continue;
      for (int j = -2; j <= 2; ++j) {
        int nx = x + j;
        if (nx < 0 || w <= nx)
          continue;
        if (s[ny][nx] == '#')
          continue;
        if (dp[ny][nx] <= dp[y][x] + 1)
          continue;
        dp[ny][nx] = dp[y][x] + 1;
        que.push(P(ny, nx));
      }
    }
  }
  if (dp[dh][dw] == INF)
    cout << -1 << endl;
  else
    cout << dp[dh][dw] << endl;
  return 0;
}