Problem p02579 (C++)

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

using namespace std;

#define all(x) (x).begin(), (x).end()

typedef long long ll;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int dst[1010][1010];
string s[1010];

bool good(int nx, int ny, int n, int m) {
  return 1 <= nx && nx <= n && 1 <= ny && ny <= m;
}

void solve() {
  int n, m;
  cin >> n >> m;
  int sx, sy, fx, fy;
  cin >> sx >> sy >> fx >> fy;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    s[i] = "#" + s[i];
  }
  memset(dst, -1, sizeof dst);
  dst[sx][sy] = 0;
  deque<pair<pair<int, int>, int>> q;
  q.push_front({{sx, sy}, 0});
  while (!q.empty()) {
    int x, y;
    tie(x, y) = q.front().first;
    int dist = q.front().second;
    q.pop_front();
    if (dst[x][y] != dist)
      continue;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (good(nx, ny, n, m) && s[nx][ny] == '.' &&
          (dst[nx][ny] == -1 || dst[nx][ny] > dst[x][y])) {
        dst[nx][ny] = dst[x][y];
        q.push_back({{nx, ny}, dst[x][y]});
      }
    }
    for (int mx = -2; mx <= 2; mx++) {
      for (int my = -2; my <= 2; my++) {
        int nx = x + mx, ny = y + my;
        if (good(nx, ny, n, m) && s[nx][ny] == '.' &&
            (dst[nx][ny] == -1 || dst[nx][ny] > dst[x][y])) {
          dst[nx][ny] = dst[x][y] + 1;
          q.push_back({{nx, ny}, dst[x][y] + 1});
        }
      }
    }
  }
  cout << dst[fx][fy];
}

// CHECK LIMITS (n <= 10^5)
// CHECK CORNER CASES (n == 1)

int main() {
  ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
  cout.setf(ios::fixed), cout.precision(20);
  // cout << 1.0 * clock() / CLOCKS_PER_SEC << endl;
  solve();
}


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

using namespace std;

#define all(x) (x).begin(), (x).end()

typedef long long ll;

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
int dst[1010][1010];
string s[1010];

bool good(int nx, int ny, int n, int m) {
  return 1 <= nx && nx <= n && 1 <= ny && ny <= m;
}

void solve() {
  int n, m;
  cin >> n >> m;
  int sx, sy, fx, fy;
  cin >> sx >> sy >> fx >> fy;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    s[i] = "#" + s[i];
  }
  memset(dst, -1, sizeof dst);
  dst[sx][sy] = 0;
  deque<pair<pair<int, int>, int>> q;
  q.push_front({{sx, sy}, 0});
  while (!q.empty()) {
    int x, y;
    tie(x, y) = q.front().first;
    int dist = q.front().second;
    q.pop_front();
    if (dst[x][y] != dist)
      continue;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (good(nx, ny, n, m) && s[nx][ny] == '.' &&
          (dst[nx][ny] == -1 || dst[nx][ny] > dst[x][y])) {
        dst[nx][ny] = dst[x][y];
        q.push_front({{nx, ny}, dst[x][y]});
      }
    }
    for (int mx = -2; mx <= 2; mx++) {
      for (int my = -2; my <= 2; my++) {
        int nx = x + mx, ny = y + my;
        if (good(nx, ny, n, m) && s[nx][ny] == '.' &&
            (dst[nx][ny] == -1 || dst[nx][ny] > dst[x][y])) {
          dst[nx][ny] = dst[x][y] + 1;
          q.push_back({{nx, ny}, dst[x][y] + 1});
        }
      }
    }
  }
  cout << dst[fx][fy];
}

// CHECK LIMITS (n <= 10^5)
// CHECK CORNER CASES (n == 1)

int main() {
  ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
  cout.setf(ios::fixed), cout.precision(20);
  // cout << 1.0 * clock() / CLOCKS_PER_SEC << endl;
  solve();
}