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

#include "bits/stdc++.h"
#include <bits/stdc++.h>

using namespace std;

#define int long long

#ifndef bhupixb
#define var(...)
#define stl(...)
#endif

#define single_test

void solve() {
  int n, m;
  cin >> n >> m;
  int sx, sy;
  cin >> sx >> sy;
  int dx, dy;
  cin >> dx >> dy;
  var(sx, sy, dx, dy);
  // return;
  vector<vector<char>> a(n, vector<char>(m));
  for (auto &it : a) {
    for (auto &ch : it) {
      cin >> ch;
    }
  }
  deque<array<int, 3>> q;
  // return;

  --sx, --sy;
  --dx, --dy;
  q.push_back({0, sx, sy});
  vector<vector<bool>> vis(n, vector<bool>(m));
  const int inf = 1e9;
  vector<vector<int>> dist(n, vector<int>(m, inf));
  var(sx, sy);
  while (!q.empty()) {
    auto it = q.front();
    q.pop_front();
    int r = it[1], c = it[2], cost = it[0];
    for (int x = -2; x <= 2; ++x) {
      for (int y = -2; y <= 2; ++y) {
        int new_r = x + r;
        int new_c = y + c;
        if (new_c < 0 or new_c >= m or new_r < 0 or new_r >= n) {
          continue;
        }
        int cost = ((abs(r - new_r) + abs(c - new_c)) > 1);
        int &ref = dist[new_r][new_c];
        if (a[new_r][new_c] == '.' && ref > cost + it[0]) {
          ref = cost + it[0];
          if (cost)
            q.push_back({ref, new_r, new_c});
          else
            q.push_front({ref, new_r, new_c});
        }
      }
    }
  }
  stl(dist);
  assert(dist[sx][sy] == 0);
  cout << (dist[dx][dy] == inf ? -1 : dist[dx][dy]) << '\\n';
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
#ifndef single_test
  cin >> t;
#endif
  for (int i = 1; i <= t; ++i) {
    // cout << "Case #" << i << ": ";
    solve();
  }

  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <bits/stdc++.h>

using namespace std;

#define int long long

#ifndef bhupixb
#define var(...)
#define stl(...)
#endif

#define single_test

void solve() {
  int n, m;
  cin >> n >> m;
  int sx, sy;
  cin >> sx >> sy;
  int dx, dy;
  cin >> dx >> dy;
  var(sx, sy, dx, dy);
  // return;
  vector<vector<char>> a(n, vector<char>(m));
  for (auto &it : a) {
    for (auto &ch : it) {
      cin >> ch;
    }
  }
  queue<array<int, 3>> q;
  // return;

  --sx, --sy;
  --dx, --dy;
  q.push({0, sx, sy});
  vector<vector<bool>> vis(n, vector<bool>(m));
  const int inf = 1e9;
  vector<vector<int>> dist(n, vector<int>(m, inf));
  while (!q.empty()) {
    auto it = q.front();
    q.pop();
    int r = it[1], c = it[2], cost = it[0];
    for (int x = -2; x <= 2; ++x) {
      for (int y = -2; y <= 2; ++y) {
        int new_r = x + r;
        int new_c = y + c;
        if (new_c < 0 or new_c >= m or new_r < 0 or new_r >= n) {
          continue;
        }
        int cost = ((abs(r - new_r) + abs(c - new_c)) > 1);
        int &ref = dist[new_r][new_c];
        if (a[new_r][new_c] == '.' && ref > cost + it[0]) {
          ref = cost + it[0];
          q.push({ref, new_r, new_c});
        }
      }
    }
  }
  stl(dist);
  assert(dist[sx][sy] == 0);
  cout << (dist[dx][dy] == inf ? -1 : dist[dx][dy]) << '\\n';
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
#ifndef single_test
  cin >> t;
#endif
  for (int i = 1; i <= t; ++i) {
    // cout << "Case #" << i << ": ";
    solve();
  }

  return 0;
}