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

// Created by Nandu Vinodan
#include <bits/stdc++.h>

#define db1(x) cout << #x << "=" << x << '\\n'
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << '\\n'
#define db3(x, y, z)                                                           \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << '\\n'
#define rep(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define pi pair<int, int>
#define INF INT_MAX
#define _INF INT_MIN
#define TC 0

using namespace std;
using ll = long long;

void testcase() {
  int n, m;
  cin >> n >> m;

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;

  --x2;
  --x1;
  --y1;
  --y2;

  vector<string> mat(n);

  for (auto &it : mat) {
    cin >> it;
  }

  vector<vector<int>> dist(n, vector<int>(m, INT_MAX));

  queue<pair<int, int>> q;
  priority_queue<array<int, 3>, vector<array<int, 3>>, greater<array<int, 3>>>
      pq;
  pq.push({x1, y1, 0});
  dist[x1][y1] = 0;

  int X[4] = {-1, 1, 0, 0};
  int Y[4] = {0, 0, -1, 1};
  while (!pq.empty()) {
    auto u = pq.top();
    pq.pop();

    int x = u[0], y = u[1], w = u[2];

    if (dist[x][y] < w)
      continue;

    for (int k = 0; k < 4; k++) {
      int nx = x + X[k];
      int ny = y + Y[k];
      if (nx >= 0 && nx < n && ny >= 0 && ny < m && mat[nx][ny] == '.' &&
          dist[nx][ny] > dist[x][y]) {
        dist[nx][ny] = dist[x][y];
        pq.push({nx, ny});
      }
    }

    for (int nx = x - 2; nx <= x + 2; nx++) {
      for (int ny = y - 2; ny <= y + 2; ny++) {
        if (nx >= 0 && nx < n && ny >= 0 && ny < m && mat[nx][ny] == '.' &&
            dist[nx][ny] > dist[x][y] + 1) {
          dist[nx][ny] = dist[x][y] + 1;
          pq.push({nx, ny});
        }
      }
    }
  }

  cout << (dist[x2][y2] == INT_MAX ? -1 : dist[x2][y2]);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  if (TC)
    cin >> t;
  while (t--) {
    testcase();
  }
  return 0;
}

Error: TLE

Fixed submission:

// Created by Nandu Vinodan
#include <bits/stdc++.h>

#define db1(x) cout << #x << "=" << x << '\\n'
#define db2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << '\\n'
#define db3(x, y, z)                                                           \\
  cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << '\\n'
#define rep(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define vi vector<int>
#define pi pair<int, int>
#define INF INT_MAX
#define _INF INT_MIN
#define TC 0

using namespace std;
using ll = long long;

void testcase() {
  int n, m;
  cin >> n >> m;

  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;

  --x2;
  --x1;
  --y1;
  --y2;

  vector<string> mat(n);

  for (auto &it : mat) {
    cin >> it;
  }

  vector<vector<int>> dist(n, vector<int>(m, INT_MAX));

  queue<pair<int, int>> q;
  dist[x1][y1] = 0;
  q.push({x1, y1});
  int X[4] = {-1, 1, 0, 0};
  int Y[4] = {0, 0, -1, 1};
  while (!q.empty()) {
    auto u = q.front();
    q.pop();

    int x = u.first, y = u.second;

    for (int k = 0; k < 4; k++) {
      int nx = x + X[k];
      int ny = y + Y[k];
      if (nx >= 0 && nx < n && ny >= 0 && ny < m && mat[nx][ny] == '.' &&
          dist[nx][ny] > dist[x][y]) {
        dist[nx][ny] = dist[x][y];
        q.push({nx, ny});
      }
    }

    for (int nx = x - 2; nx <= x + 2; nx++) {
      for (int ny = y - 2; ny <= y + 2; ny++) {
        if (nx >= 0 && nx < n && ny >= 0 && ny < m && mat[nx][ny] == '.' &&
            dist[nx][ny] > dist[x][y] + 1) {
          dist[nx][ny] = dist[x][y] + 1;
          q.push({nx, ny});
        }
      }
    }
  }

  cout << (dist[x2][y2] == INT_MAX ? -1 : dist[x2][y2]);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  if (TC)
    cin >> t;
  while (t--) {
    testcase();
  }
  return 0;
}