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

/**
 *    author:  FromDihPout
 *    created: 2020-08-25
 **/

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

const int INF = 1e9;
const int N = 1e3 + 10;
queue<pair<int, int>> q, q2;
int dist[N][N], h, w, sh, sw, eh, ew;
vector<pair<int, int>> ADJ = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
vector<string> a;

void dfs(int r, int c) {
  q2.emplace(r, c);
  for (auto neigh : ADJ) {
    int nr = r + neigh.first, nc = c + neigh.second;
    if (nr < 0 || nr >= h || nc < 0 || nc >= w || a[nr][nc] == '#')
      continue;
    if (dist[r][c] < dist[nr][nc]) {
      dist[nr][nc] = dist[r][c];
      dfs(nr, nc);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> h >> w >> sh >> sw >> eh >> ew;
  sh--, sw--, eh--, ew--;
  a.resize(h);
  for (int i = 0; i < h; i++) {
    cin >> a[i];
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      dist[i][j] = INF;
    }
  }
  q.emplace(sh, sw);
  dist[sh][sw] = 0;
  while (1) {
    bool ok = false;
    while (!q.empty()) {
      auto pos = q.front();
      q.pop();
      int r = pos.first, c = pos.second;
      if (r == eh && c == ew) {
        ok = true;
        break;
      }
      dfs(r, c);
    }
    if (ok)
      break;

    while (!q2.empty()) {
      auto pos = q2.front();
      q2.pop();
      int r = pos.first, c = pos.second;
      if (r == eh && c == ew) {
        ok = true;
        break;
      }

      for (int nr = max(0, r - 2); nr <= min(h - 1, r + 2); nr++) {
        for (int nc = max(0, c - 2); nc <= min(w - 1, c + 2); nc++) {
          if (nr == r && nc == c)
            continue;
          if (nr < 0 || nr >= h || nc < 0 || nc >= w || a[nr][nc] == '#')
            continue;
          if (dist[r][c] + 1 < dist[nr][nc]) {
            dist[nr][nc] = dist[r][c] + 1;
            q.emplace(nr, nc);
          }
        }
      }
    }
    if (ok)
      break;
    if (q.empty())
      break;
  }

  if (dist[eh][ew] < INF) {
    cout << dist[eh][ew] << '\\n';
  } else {
    cout << -1 << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

/**
 *    author:  FromDihPout
 *    created: 2020-08-25
 **/

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

const int INF = 1e9;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  vector<pair<int, int>> ADJ = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

  int h, w, sh, sw, eh, ew;
  cin >> h >> w >> sh >> sw >> eh >> ew;
  sh--, sw--, eh--, ew--;
  vector<string> a(h);
  for (int i = 0; i < h; i++) {
    cin >> a[i];
  }

  vector<vector<int>> dist(h, vector<int>(w, INF));
  queue<pair<int, int>> q;
  q.emplace(sh, sw);
  dist[sh][sw] = 0;
  while (!q.empty()) {
    auto pos = q.front();
    q.pop();
    int r = pos.first, c = pos.second;
    if (r == eh && c == ew)
      break;

    for (auto neigh : ADJ) {
      int nr = r + neigh.first, nc = c + neigh.second;
      if (nr < 0 || nr >= h || nc < 0 || nc >= w || a[nr][nc] == '#')
        continue;
      if (dist[r][c] <= dist[nr][nc]) {
        dist[nr][nc] = dist[r][c];
        q.emplace(nr, nc);
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (i == 0 && j == 0)
          continue;
        int nr = r + i, nc = c + j;
        if (nr < 0 || nr >= h || nc < 0 || nc >= w || a[nr][nc] == '#')
          continue;
        if (dist[r][c] + 1 <= dist[nr][nc]) {
          dist[nr][nc] = dist[r][c] + 1;
          q.emplace(nr, nc);
        }
      }
    }
  }

  if (dist[eh][ew] < INF) {
    cout << dist[eh][ew] << '\\n';
  } else {
    cout << -1 << '\\n';
  }
  return 0;
}