Problem p02579 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;
const long long MOD = 998244353;

/// mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N = 1010;
int n, m;
char a[N][N];
int d[N][N];

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

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

void solve() {

  cin >> n >> m;
  int x0, y0, x1, y1;
  cin >> x0 >> y0;
  cin >> x1 >> y1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      d[i][j] = 1e9;
    }
  }

  set<pair<int, pair<int, int>>> s;

  d[x1][y1] = 0;
  s.insert({0, {x1, y1}});

  while (!s.empty()) {
    auto f = s.begin();
    int x = f->second.first;
    int y = f->second.second;
    s.erase(s.begin());
    cerr << x << ' ' << y << endl;

    for (int i = 0; i < 4; i++) {
      int X = x + dx[i];
      int Y = y + dy[i];
      if (valid(X, Y) && a[X][Y] == '.' && d[X][Y] > d[x][y]) {
        s.erase({d[X][Y], {X, Y}});
        d[X][Y] = d[x][y];
        s.insert({d[X][Y], {X, Y}});
      }
    }

    for (int X = x - 2; X <= x + 2; X++) {
      for (int Y = y - 2; Y <= y + 2; Y++) {
        if (valid(X, Y) && a[X][Y] == '.') {
          if (d[X][Y] > d[x][y] + 1) {
            s.erase({d[X][Y], {X, Y}});
            d[X][Y] = d[x][y] + 1;
            s.insert({d[X][Y], {X, Y}});
          }
        }
      }
    }
  }
  if (d[x0][y0] == 1e9)
    cout << -1 << endl;
  else
    cout << d[x0][y0] << endl;
}

int main() {
#ifdef QWERTY
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);

  int t = 1;
  //        cin >> t;

  for (int i = 1; i <= t; i++) {
    //                cout << "Case #" << i << ": ";
    solve();
  }

  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
const long long MOD = 998244353;

/// mt19937 mt(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N = 1010;
int n, m;
char a[N][N];
int d[N][N];

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

int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};

void solve() {

  cin >> n >> m;
  int x0, y0, x1, y1;
  cin >> x0 >> y0;
  cin >> x1 >> y1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
      d[i][j] = 1e9;
    }
  }

  set<pair<int, pair<int, int>>> s;

  d[x1][y1] = 0;
  s.insert({0, {x1, y1}});

  while (!s.empty()) {
    auto f = s.begin();
    int x = f->second.first;
    int y = f->second.second;
    s.erase(s.begin());
    //                cerr << x << ' ' << y << endl;

    for (int i = 0; i < 4; i++) {
      int X = x + dx[i];
      int Y = y + dy[i];
      if (valid(X, Y) && a[X][Y] == '.' && d[X][Y] > d[x][y]) {
        s.erase({d[X][Y], {X, Y}});
        d[X][Y] = d[x][y];
        s.insert({d[X][Y], {X, Y}});
      }
    }

    for (int X = x - 2; X <= x + 2; X++) {
      for (int Y = y - 2; Y <= y + 2; Y++) {
        if (valid(X, Y) && a[X][Y] == '.') {
          if (d[X][Y] > d[x][y] + 1) {
            s.erase({d[X][Y], {X, Y}});
            d[X][Y] = d[x][y] + 1;
            s.insert({d[X][Y], {X, Y}});
          }
        }
      }
    }
  }
  if (d[x0][y0] == 1e9)
    cout << -1 << endl;
  else
    cout << d[x0][y0] << endl;
}

int main() {
#ifdef QWERTY
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);

  int t = 1;
  //        cin >> t;

  for (int i = 1; i <= t; i++) {
    //                cout << "Case #" << i << ": ";
    solve();
  }

  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}