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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
const ll INF = 1 << 18;
ll dp[1010][1010];
int dh[] = {-1, 0, 0, 1};
int dw[] = {0, 1, -1, 0};

int main() {
  int h, w;
  cin >> h >> w;
  int sh, sw, gh, gw;
  cin >> sh >> sw >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dp[i][j] = INF;
    }
  }
  vector<string> G(h);
  for (int i = 0; i < h; i++)
    cin >> G[i];

  dp[sh][sw] = 0;
  queue<P> q;
  queue<P> qq;
  q.push(P(sh, sw));
  while (!q.empty()) {
    while (!q.empty()) {
      P p = q.front();
      q.pop();
      int y = p.first;
      int x = p.second;
      qq.push(P(y, x));
      for (int d = 0; d < 4; d++) {
        if (y + dh[d] >= 0 && y + dh[d] < h && x + dw[d] >= 0 &&
            x + dw[d] < w && G[y + dh[d]][x + dw[d]] == '.' &&
            dp[y + dh[d]][x + dw[d]] > dp[y][x]) {
          dp[y + dh[d]][x + dw[d]] = dp[y][x];
          q.push(P(y + dh[d], x + dw[d]));
        }
      }
    }
    while (!qq.empty()) {
      // cout<<"debug"<<endl;
      P p = qq.front();
      qq.pop();
      int y = p.first;
      int x = p.second;
      for (int i = -2; i < 3; i++) {
        for (int j = -2; j < 3; j++) {
          if (y + i >= 0 && y + i < h && x + j >= 0 && x + j < w &&
              G[y + i][x + j] == '.' && dp[y + i][x + j] > dp[y][x] + 1) {
            dp[y + i][x + j] = dp[y][x] + 1;
            q.push(P(y + i, x + j));
          }
        }
      }
    }
  }
  /*
  for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
          cout<<dp[i][j]<<" ";
      }
      cout<<endl;
  }
  */

  if (dp[gh][gw] == INF) {
    cout << -1 << endl;
  } else {
    cout << dp[gh][gw] << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
const ll MOD = 1000000007;
const ll MAX_N = 500010;
const ll INF = 1 << 18;
ll dp[1010][1010];
int dh[] = {-1, 0, 0, 1};
int dw[] = {0, 1, -1, 0};

int main() {
  int h, w;
  cin >> h >> w;
  int sh, sw, gh, gw;
  cin >> sh >> sw >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dp[i][j] = INF;
    }
  }
  vector<string> G(h);
  for (int i = 0; i < h; i++)
    cin >> G[i];

  dp[sh][sw] = 0;
  queue<P> q;
  q.push(P(sh, sw));
  while (!q.empty()) {
    P p = q.front();
    q.pop();
    int y = p.first;
    int x = p.second;
    for (int d = 0; d < 4; d++) {
      if (y + dh[d] >= 0 && y + dh[d] < h && x + dw[d] >= 0 && x + dw[d] < w &&
          G[y + dh[d]][x + dw[d]] == '.' &&
          dp[y + dh[d]][x + dw[d]] > dp[y][x]) {
        dp[y + dh[d]][x + dw[d]] = dp[y][x];
        q.push(P(y + dh[d], x + dw[d]));
      }
    }
    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        if (y + i >= 0 && y + i < h && x + j >= 0 && x + j < w &&
            G[y + i][x + j] == '.' && dp[y + i][x + j] > dp[y][x] + 1) {
          dp[y + i][x + j] = dp[y][x] + 1;
          q.push(P(y + i, x + j));
        }
      }
    }
  }

  if (dp[gh][gw] == INF) {
    cout << -1 << endl;
  } else {
    cout << dp[gh][gw] << endl;
  }
}