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

#include <bits/stdc++.h>
#define INFTY 10000000000
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
const int MOD = 1000000007;
const int di[4] = {-1, 0, 1, 0};
const int dj[4] = {0, -1, 0, 1};
const int INF = 1e9;
int main() {
  int h, w;
  cin >> h >> w;
  int ch, dh, cw, dw;
  cin >> ch >> cw >> dh >> dw;
  vector<string> s(h);
  int dp[1003][1003], seen[1003][1003];
  rep(i, 1003) {
    rep(j, 1003) {
      dp[i][j] = INF;
      seen[i][j] = 0;
    }
  }
  dp[ch - 1][cw - 1] = 0;
  rep(i, h) cin >> s[i];
  deque<pair<int, int>> q;
  q.emplace_back(make_pair(ch - 1, cw - 1));
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop_front();
    int x = p.first, y = p.second;
    if (seen[x][y] || s[x][y] == '#')
      continue;
    seen[x][y] = 1;
    // cout<<x<<y<<endl;
    rep(i, 4) {
      if (0 <= x + di[i] && 0 <= y + dj[i] && h > x + di[i] && w > y + dj[i] &&
          s[x + di[i]][y + dj[i]] == '.') {
        if (!seen[x + di[i]][y + dj[i]])
          q.emplace_front(make_pair(x + di[i], y + dj[i]));
        if (dp[x + di[i]][y + dj[i]] >= 0)
          dp[x + di[i]][y + dj[i]] = min(dp[x + di[i]][y + dj[i]], dp[x][y]);
        else
          dp[x + di[i]][y + dj[i]] = dp[x][y];
      }
    }
    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        if (i == 0 && abs(j) == 1 || j == 0 && abs(i) == 1 || i == 0 && j == 0)
          continue;
        if (0 <= x + i && 0 <= y + j && h > x + i && w > y + j &&
            s[x + i][y + j] == '.') {

          if (!seen[x + i][y + j]) {
            q.emplace_back(make_pair(x + i, y + j));
            dp[x + i][y + j] = min(dp[x + i][y + j], dp[x][y] + 1);
          }
        }
      }
    }
  }
  if (dp[dh - 1][dw - 1] == INF)
    cout << -1 << endl;
  else
    cout << dp[dh - 1][dw - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INFTY 10000000000
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long int ll;
const int MOD = 1000000007;
const int di[8] = {-1, 0, 1, 0, 2, 0, -2, 0};
const int dj[8] = {0, -1, 0, 1, 0, 2, 0, -2};

int main() {
  int h, w;
  cin >> h >> w;
  int ch, dh, cw, dw;
  cin >> ch >> cw >> dh >> dw;
  char s[1003][1003];
  int dp[1003][1003], seen[1003][1003];
  rep(i, 1003) {
    rep(j, 1003) {
      dp[i][j] = -1;
      seen[i][j] = 0;
    }
  }
  dp[ch - 1][cw - 1] = 0;
  rep(i, h) {
    rep(j, w) { cin >> s[i][j]; }
  }
  queue<pair<int, int>> q;
  q.push(make_pair(ch - 1, cw - 1));
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();
    int x = p.first, y = p.second;
    seen[x][y] = 1;
    rep(i, 4) {
      if (0 <= x + di[i] && 0 <= y + dj[i] && h > x + di[i] && w > y + dj[i] &&
          s[x + di[i]][y + dj[i]] == '.') {
        if (!seen[x + di[i]][y + dj[i]])
          q.push(make_pair(x + di[i], y + dj[i]));
        if (dp[x + di[i]][y + dj[i]] >= 0)
          dp[x + di[i]][y + dj[i]] = min(dp[x + di[i]][y + dj[i]], dp[x][y]);
        else
          dp[x + di[i]][y + dj[i]] = dp[x][y];
      }
    }
    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        if (i == 0 && abs(j) == 1 || j == 0 && abs(i) == 1)
          continue;
        if (0 <= x + i && 0 <= y + j && h > x + i && w > y + j &&
            s[x + i][y + j] == '.') {

          if (!seen[x + i][y + j])
            q.push(make_pair(x + i, y + j));
          if (dp[x + i][y + j] >= 0)
            dp[x + i][y + j] = min(dp[x + i][y + j], dp[x][y] + 1);
          else
            dp[x + i][y + j] = dp[x][y] + 1;
        }
      }
    }

  } /*
   rep(i,h){
       rep(j,w){
           cout<<dp[i][j];
       }
       cout<<endl;
   }*/

  cout << dp[dh - 1][dw - 1] << endl;
  return 0;
}