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

#include <bits/stdc++.h>
using namespace std;

int n, m, d[1001][1001], a[1001][1001], xs, ys, xf, yf;
bool inque[1001][1001];
deque<pair<int, int>> coada;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

bool in(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m; }

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  cin >> xs >> ys;
  cin >> xf >> yf;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= m; j++) {
      d[i][j] = n * m;
      if (s[j - 1] == '.') {
        a[i][j] = 1;
      } else {
        a[i][j] = 0;
      }
    }
  }
  d[xs][ys] = 0;
  inque[xs][ys] = 1;
  coada.push_back(make_pair(xs, ys));
  while (!coada.empty()) {
    int x = coada.front().first;
    int y = coada.front().second;
    inque[x][y] = 0;
    coada.pop_front();
    for (int i = 0; i < 4; i++) {
      int newx = x + dx[i];
      int newy = y + dy[i];
      if (in(newx, newy) && a[newx][newy] && d[newx][newy] > d[x][y]) {
        d[newx][newy] = d[x][y];
        if (!inque[newx][newy]) {
          inque[newx][newy] = 1;
          coada.push_front(make_pair(newx, newy));
        }
      }
    }
    for (int i = max(x - 2, 1); i <= min(x + 2, n); i++) {
      for (int j = max(y - 2, 1); j <= min(y + 2, m); j++) {
        if (abs(i - x) + abs(j - y) > 1 && a[i][j] && d[i][j] > d[x][y] + 1) {
          d[i][j] = d[x][y] + 1;
          if (!inque[i][j]) {
            inque[i][j] = 1;
            coada.push_back(make_pair(i, j));
          }
        }
      }
    }
  }
  if (d[xf][yf] == n * m) {
    cout << -1;
  } else {
    cout << d[xf][yf];
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int n, m, d[1001][1001], a[1001][1001], xs, ys, xf, yf;
bool inque[1001][1001];
queue<pair<int, int>> coada;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

bool in(int x, int y) { return x >= 1 && x <= n && y >= 1 && y <= m; }

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  cin >> xs >> ys;
  cin >> xf >> yf;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= m; j++) {
      d[i][j] = n * m;
      if (s[j - 1] == '.') {
        a[i][j] = 1;
      } else {
        a[i][j] = 0;
      }
    }
  }
  d[xs][ys] = 0;
  inque[xs][ys] = 1;
  coada.push(make_pair(xs, ys));
  while (!coada.empty()) {
    int x = coada.front().first;
    int y = coada.front().second;
    inque[x][y] = 0;
    coada.pop();
    for (int i = 0; i < 4; i++) {
      int newx = x + dx[i];
      int newy = y + dy[i];
      if (in(newx, newy) && a[newx][newy] && d[newx][newy] > d[x][y]) {
        d[newx][newy] = d[x][y];
        if (!inque[newx][newy]) {
          inque[newx][newy] = 1;
          coada.push(make_pair(newx, newy));
        }
      }
    }
    for (int i = max(x - 2, 1); i <= min(x + 2, n); i++) {
      for (int j = max(y - 2, 1); j <= min(y + 2, m); j++) {
        if (abs(i - x) + abs(j - y) > 1 && a[i][j] && d[i][j] > d[x][y] + 1) {
          d[i][j] = d[x][y] + 1;
          if (!inque[i][j]) {
            inque[i][j] = 1;
            coada.push(make_pair(i, j));
          }
        }
      }
    }
  }
  if (d[xf][yf] == n * m) {
    cout << -1;
  } else {
    cout << d[xf][yf];
  }
  return 0;
}