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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 1000 + 7;
int n;
int m;
int r1;
int c1;
int r2;
int c2;
int a[N][N];
int d[N][N];

deque<pair<int, int>> q;

void put(int r, int c, int x, int fi) {
  if (x >= d[r][c])
    return;
  d[r][c] = x;
  if (fi)
    q.push_front({r, c});
  else
    q.push_back({r, c});
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> r1 >> c1 >> r2 >> c2;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= m; j++) {
      if (s[j - 1] == '.') {
        a[i][j] = 1;
        d[i][j] = (int)1e9;
      } else {
        d[i][j] = 9;
      }
    }
  }
  if (a[r1][c1] == 0 || a[r2][c2] == 0) {
    cout << "-1\\n";
    return 0;
  }
  int dr[] = {-1, 0, 1, 0}, dc[] = {0, 1, 0, -1};
  put(r1, c1, 0, 1);
  while (!q.empty()) {
    int r = q.front().first;
    int c = q.front().second;
    q.pop_front();
    for (int k = 0; k < 4; k++) {
      int r2 = r + dr[k];
      int c2 = c + dc[k];
      if (1 <= r2 && 1 <= c2 && r2 <= n && c2 <= m && a[r2][c2]) {
        put(r2, c2, d[r][c], 1);
      }
    }
    for (int dx = -2; dx <= +2; dx++) {
      for (int dy = -2; dy <= +2; dy++) {
        int r2 = r + dx, c2 = c + dy;
        if (1 <= r2 && 1 <= c2 && r2 <= n && c2 <= m && a[r2][c2]) {
          put(r2, c2, d[r][c] + 1, 0);
        }
      }
    }
  }
  if (d[r2][c2] == (int)1e9)
    d[r2][c2] = -1;
  cout << d[r2][c2] << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int N = 1000 + 7;
int n;
int m;
int r1;
int c1;
int r2;
int c2;
int a[N][N];
int d[N][N];
set<int> row[N];
set<int> col[N];

queue<pair<int, int>> q;

void put(int r, int c, int x) {
  if (d[r][c] <= x) {
    return;
  }
  d[r][c] = x;
  row[r].erase(c);
  col[c].erase(r);
  q.push({r, c});
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> r1 >> c1 >> r2 >> c2;
  for (int i = 1; i <= n; i++) {
    string s;
    cin >> s;
    for (int j = 1; j <= m; j++) {
      if (s[j - 1] == '.') {
        a[i][j] = 1;
        d[i][j] = (int)1e9;
      }
    }
  }
  if (a[r1][c1] == 0 || a[r2][c2] == 0) {
    cout << "-1\\n";
    return 0;
  }
  int dr[] = {-1, 0, 1, 0}, dc[] = {0, 1, 0, -1};
  put(r1, c1, 0);
  while (!q.empty()) {
    int r = q.front().first;
    int c = q.front().second;
    q.pop();
    for (int k = 0; k < 4; k++) {
      int r2 = r + dr[k];
      int c2 = c + dc[k];
      if (1 <= r2 && 1 <= c2 && r2 <= n && c2 <= m && a[r2][c2]) {
        put(r2, c2, d[r][c]);
      }
    }
    /**while (!row[r].empty()) {
      int c2 = *row[r].begin();
      upd(r, c2, d[r][c] + 1);
    }
    while (!col[c].empty()) {
      int r2 = *col[c].begin();
      upd(r2, c, d[r][c] + 1);
    }**/
    for (int dx = -2; dx <= +2; dx++) {
      for (int dy = -2; dy <= +2; dy++) {
        int r2 = r + dx, c2 = c + dy;
        if (1 <= r2 && 1 <= c2 && r2 <= n && c2 <= m && a[r2][c2]) {
          put(r2, c2, d[r][c] + 1);
        }
      }
    }
  }
  if (d[r2][c2] == (int)1e9)
    d[r2][c2] = -1;
  cout << d[r2][c2] << "\\n";
  return 0;
}