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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, j, n) for (int i = j; i < (int)(n); i++)
#define brep(i, n) for (int i = n; i > 0; i--)
#define BREP(i, j, n) for (int i = n; i > j; i--)
#define TEST cout << "PASS" << endl
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9 + 1e6 + 1e3 + 1;
const ll MOD = 1e9 + 7;
const double PI = 3.141592653589793;

int main() {
  int h, w;
  cin >> h >> w;
  int cx, cy, dx, dy;
  cin >> cy >> cx >> dy >> dx;
  cy--;
  cx--;
  dy--;
  dx--;
  bool board[1000][1000];
  int d[1000][1000];
  rep(i, 1000) rep(j, 1000) d[i][j] = INF;
  rep(i, h) {
    rep(j, w) {
      char c;
      cin >> c;
      board[i][j] = (c == '.');
    }
  }
  deque<P> q;
  q.push_front(P(cy, cx));
  d[cy][cx] = 0;
  int cnt = 0;
  while (!q.empty()) {
    int x = q.front().second, y = q.front().first;
    q.pop_front();
    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        if (i == 0 && j == 0)
          continue;
        if (x + i < 0 || x + i >= w || y + j < 0 || y + j >= h)
          continue;
        if (!board[y + j][x + i])
          continue;
        if (d[y + j][x + i] <= d[y][x])
          continue;
        if ((i == 1 && j == 0) || (i == 0 && j == 1) || (i == 0 && j == -1) ||
            (i == -1 && j == 0)) {
          d[y + j][x + i] = d[y][x];
          q.push_front(P(y + j, x + i));
        } else {
          if (d[y + j][x + i] <= d[y][x] + 1)
            continue;
          d[y + j][x + i] = d[y][x] + 1;
          q.push_back(P(y + j, x + i));
        }

        cnt++;
        // printf("%d :: x : %d, y : %d\\n", cnt, x+i, y+j);
      }
    }
  }
  if (d[dy][dx] == INF)
    cout << -1 << endl;
  else
    cout << d[dy][dx] << endl;

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, j, n) for (int i = j; i < (int)(n); i++)
#define brep(i, n) for (int i = n; i > 0; i--)
#define BREP(i, j, n) for (int i = n; i > j; i--)
#define TEST cout << "PASS" << endl
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9 + 1e6 + 1e3 + 1;
const ll MOD = 1e9 + 7;
const double PI = 3.141592653589793;

int main() {
  int h, w;
  cin >> h >> w;
  int cx, cy, dx, dy;
  cin >> cy >> cx >> dy >> dx;
  cy--;
  cx--;
  dy--;
  dx--;
  bool board[1000][1000];
  int d[1000][1000];
  rep(i, 1000) rep(j, 1000) d[i][j] = INF;
  rep(i, h) {
    rep(j, w) {
      char c;
      cin >> c;
      board[i][j] = (c == '.');
    }
  }
  queue<P> q;
  q.push(make_pair(cy, cx));
  d[cy][cx] = 0;
  while (!q.empty()) {
    int x = q.front().second, y = q.front().first;
    q.pop();
    for (int i = -2; i < 3; i++) {
      for (int j = -2; j < 3; j++) {
        if (i == j && i == 0)
          continue;
        if (x + i < 0 || x + i >= w || y + j < 0 || y + j >= h)
          continue;
        if (!board[y + j][x + i])
          continue;
        if (d[y + j][x + i] <= d[y][x])
          continue;
        if ((i == 1 && j == 0) || (i == 0 && j == 1) || (i == 0 && j == -1) ||
            (i == -1 && j == 0)) {
          d[y + j][x + i] = d[y][x];
        } else {
          d[y + j][x + i] = min(d[y + j][x + i], d[y][x] + 1);
        }
        q.push(P(y + j, x + i));
      }
    }
  }
  if (d[dy][dx] == INF)
    cout << -1 << endl;
  else
    cout << d[dy][dx] << endl;

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