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;
using ll = long long;
using P = pair<int, int>;
using vv = vector<vector<ll>>;
template <class _t> inline bool chmax(_t &a, _t b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class _t> inline bool chmin(_t &a, _t b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int INF = 2147483647;
const int max_N = 1000000000;
const long long inf = 1ll << 60;
const long long mod = 1000000007;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
bool table[1025][1025];
int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vector<string> S(h);
  vector<vector<int>> dis(h, vector<int>(w));
  rep(i, h) cin >> S[i];
  rep(i, h) rep(j, w) {
    dis[i][j] = -1;
    if (S[i][j] == '#')
      dis[i][j] = -2;
  }
  dis[ch][cw] = 0;
  deque<P> que;
  que.push_front(P(ch, cw));
  while (!que.empty()) {
    int th = que.front().first, tw = que.front().second;
    que.pop_front();
    if (table[th][tw])
      continue;
    table[th][tw] = true;
    for (int i = 0; i < 4; i++) {
      int nh = th + dx[i];
      int nw = tw + dy[i];
      if (nh < 0 || nw < 0 || nh >= h || nw >= w)
        continue;
      if (S[nh][nw] == '#')
        continue;
      if (dis[nh][nw] == -1 || dis[nh][nw] > dis[th][tw]) {
        dis[nh][nw] = dis[th][tw];
        que.push_front(P(nh, nw));
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nh = th + i;
        int nw = tw + j;
        if (nh < 0 || nw < 0 || nh >= h || nw >= w)
          continue;
        if (S[nh][nw] == '#')
          continue;
        if (dis[nh][nw] == -1 || dis[nh][nw] > dis[th][tw]) {
          dis[nh][nw] = dis[th][tw] + 1;
          que.push_back(P(nh, nw));
        }
      }
    }
  }
  // rep(i,h){
  //     rep(j,w){
  //         cout << dis[i][j];
  //     }
  //     cout << endl;
  // }
  cout << dis[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;
using ll = long long;
using P = pair<int, int>;
using vv = vector<vector<ll>>;
template <class _t> inline bool chmax(_t &a, _t b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class _t> inline bool chmin(_t &a, _t b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int INF = 2147483647;
const int max_N = 1000000000;
const long long inf = 1ll << 60;
const long long mod = 1000000007;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
int main() {
  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vector<string> S(h);
  vector<vector<int>> dis(h, vector<int>(w));
  rep(i, h) cin >> S[i];
  rep(i, h) rep(j, w) {
    dis[i][j] = -1;
    if (S[i][j] == '#')
      dis[i][j] = -2;
  }
  dis[ch][cw] = 0;
  queue<P> que;
  que.push(P(ch, cw));
  while (!que.empty()) {
    int th = que.front().first, tw = que.front().second;
    que.pop();
    for (int i = 0; i < 4; i++) {
      int nh = th + dx[i];
      int nw = tw + dy[i];
      if (nh < 0 || nw < 0 || nh >= h || nw >= w)
        continue;
      if (S[nh][nw] == '#')
        continue;
      if (dis[nh][nw] == -1 || dis[nh][nw] > dis[th][tw]) {
        dis[nh][nw] = dis[th][tw];
        que.push(P(nh, nw));
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nh = th + i;
        int nw = tw + j;
        if (nh < 0 || nw < 0 || nh >= h || nw >= w)
          continue;
        if (S[nh][nw] == '#')
          continue;
        if (dis[nh][nw] == -1 || dis[nh][nw] > dis[th][tw]) {
          dis[nh][nw] = dis[th][tw] + 1;
          que.push(P(nh, nw));
        }
      }
    }
  }
  // rep(i,h){
  //     rep(j,w){
  //         cout << dis[i][j];
  //     }
  //     cout << endl;
  // }
  cout << dis[dh][dw] << endl;
  return 0;
}