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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define IO                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
int n, m;
vector<int> dx = {0, 0, 1, -1};
vector<int> dy = {1, -1, 0, 0};
int main() {
  IO;
  cin >> n >> m;
  int sy, sx, ey, ex;
  cin >> sy >> sx >> ey >> ex;
  sy--, sx--, ey--, ex--;
  vector<string> mat(n);
  for (int i = 0; i < n; i++) {
    cin >> mat[i];
  }
  deque<pair<int, int>> pq;
  pq.emplace_back(sy, sx);
  auto can = [&](int y, int x) {
    return !(y < 0 || y >= n || x < 0 || x >= m || mat[y][x] == '#');
  };
  mat[sy][sx] = '#';
  vector<vector<int>> v(n, vector<int>(m, 1e7));
  v[sy][sx] = 0;
  while (pq.size()) {
    pair<int, int> cur = pq.back();
    pq.pop_back();
    int y = cur.first;
    int x = cur.second;
    int cost = v[y][x];
    if (y == ey && x == ex) {
      cout << cost << endl;
      return 0;
    }
    for (int i = 0; i < 4; i++) {
      int yy = y + dy[i];
      int xx = x + dx[i];
      if (can(yy, xx)) {
        if (v[yy][xx] > cost) {
          v[yy][xx] = cost;
          pq.emplace_back(yy, xx);
        }
      }
    }
    for (int i = y - 2; i <= y + 2; i++) {
      for (int j = x - 2; j <= x + 2; j++) {
        if (can(i, j)) {
          if (v[i][j] > cost + 1) {
            v[i][j] = cost + 1;
            pq.push_front({i, j});
          }
        }
      }
    }
  }
  cout << -1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define IO                                                                     \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
int n, m;
vector<int> dx = {0, 0, 1, -1};
vector<int> dy = {1, -1, 0, 0};
int main() {
  IO;
  cin >> n >> m;
  int sy, sx, ey, ex;
  cin >> sy >> sx >> ey >> ex;
  sy--, sx--, ey--, ex--;
  vector<string> mat(n);
  for (int i = 0; i < n; i++) {
    cin >> mat[i];
  }
  deque<pair<int, int>> pq;
  pq.emplace_back(sy, sx);
  auto can = [&](int y, int x) {
    return !(y < 0 || y >= n || x < 0 || x >= m || mat[y][x] == '#');
  };
  mat[sy][sx] = '#';
  vector<vector<int>> v(n, vector<int>(m, 1e7));
  v[sy][sx] = 0;
  while (pq.size()) {
    pair<int, int> cur = pq.back();
    pq.pop_back();
    int y = cur.first;
    int x = cur.second;
    int cost = v[y][x];
    if (y == ey && x == ex) {
      cout << cost << endl;
      return 0;
    }
    for (int i = 0; i < 4; i++) {
      int yy = y + dy[i];
      int xx = x + dx[i];
      if (can(yy, xx)) {
        mat[yy][xx] = '#';
        v[yy][xx] = cost;
        pq.emplace_back(yy, xx);
      }
    }
    for (int i = y - 2; i <= y + 2; i++) {
      for (int j = x - 2; j <= x + 2; j++) {
        if (can(i, j)) {
          v[i][j] = cost + 1;
          pq.push_front({i, j});
        }
      }
    }
  }
  cout << -1 << endl;
}