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

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define fastIO                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int
#define pb push_back
#define ff first
#define ss second
#define all(v) (v).begin(), (v).end()
#define mod (int)(998244353)
#define PI 3.14159265358979323846264338327950L
// ------------------ Debugging ------------------
#define trace(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
    cout << endl;                                                              \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << "[ " << *it << " = " << a << " ] ";
  err(++it, args...);
}
template <typename Tk, typename Tv>
ostream &operator<<(ostream &os, const pair<Tk, Tv> &p) {
  os << "{" << p.first << ',' << p.second << "}";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &p) {
  os << "[ ";
  for (T x : p)
    os << x << " ";
  os << "]" << endl;
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &p) {
  os << "{ ";
  for (T x : p)
    os << x << " ";
  os << "}" << endl;
  return os;
}
template <typename Tk, typename Tv>
ostream &operator<<(ostream &os, const map<Tk, Tv> &p) {
  os << "{ ";
  for (pair<Tk, Tv> x : p)
    os << x << " ";
  os << "}" << endl;
  return os;
}
// -----------------------------------------------
const int INF = 1e9;
int n, m, sx, sy, dx, dy;
vector<vector<int>> grid, dist;
int dxx[] = {0, 0, 1, -1};
int dyy[] = {1, -1, 0, 0};

bool chk(int _i, int _j) { return (_i < 0 || _j < 0 || _i >= n || _j >= m); }
#define pii pair<int, pair<int, int>>
void bfs() {

  priority_queue<pii, vector<pii>, greater<pii>> q;
  dist[sx][sy] = 0;

  q.push(pii(0, {sx, sy}));
  while (!q.empty()) {
    pii p = q.top();
    q.pop();

    int x = p.ss.ff, y = p.ss.ss;
    for (int i = 0; i < 4; i++) {

      int _x = x + dxx[i];
      int _y = y + dyy[i];

      if (chk(_x, _y))
        continue;

      if (grid[_x][_y] != -1) {
        if (dist[_x][_y] > dist[x][y]) {
          dist[_x][_y] = dist[x][y];
          q.push(pii(dist[_x][_y], {_x, _y}));
        }
      } else {
        for (int k = -2; k <= 2; k++) {
          for (int l = -2; l <= 2; l++) {
            if (!chk(x + k, y + l) && grid[x + k][y + l] != -1) {
              if (dist[x + k][y + l] > dist[x][y] + 1) {
                dist[x + k][y + l] = dist[x][y] + 1;
                q.push(pii(dist[x + k][y + l], {x + k, y + l}));
              }
            }
          }
        }
      }
    }
  }
}
signed main() {

  fastIO

          cin >>
      n >> m >> sx >> sy >> dx >> dy;

  --sx;
  --sy;
  --dx;
  --dy;

  string s;
  grid = vector<vector<int>>(n, vector<int>(m));
  dist = vector<vector<int>>(n, vector<int>(m, INF));

  for (int i = 0; i < n; i++) {
    cin >> s;
    for (int j = 0; j < m; j++) {
      grid[i][j] = (s[j] == '.' ? 1 : -1);
    }
  }
  // cout << "grid:\\n";
  // cout << grid;
  //    for (int l = -2; l <= 2; l++) {
  //     for (int ll = -2; ll <= 2; ll++) {
  //         trace(3 + l,3 + ll);
  //     }
  // }

  bfs();

  cout << (dist[dx][dy] == INF ? -1 : dist[dx][dy]) << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
#define fastIO                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int
#define pb push_back
#define ff first
#define ss second
#define all(v) (v).begin(), (v).end()
#define mod (int)(998244353)
#define PI 3.14159265358979323846264338327950L
// ------------------ Debugging ------------------
#define trace(args...)                                                         \\
  {                                                                            \\
    string _s = #args;                                                         \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    err(_it, args);                                                            \\
    cout << endl;                                                              \\
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << "[ " << *it << " = " << a << " ] ";
  err(++it, args...);
}
template <typename Tk, typename Tv>
ostream &operator<<(ostream &os, const pair<Tk, Tv> &p) {
  os << "{" << p.first << ',' << p.second << "}";
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &p) {
  os << "[ ";
  for (T x : p)
    os << x << " ";
  os << "]" << endl;
  return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &p) {
  os << "{ ";
  for (T x : p)
    os << x << " ";
  os << "}" << endl;
  return os;
}
template <typename Tk, typename Tv>
ostream &operator<<(ostream &os, const map<Tk, Tv> &p) {
  os << "{ ";
  for (pair<Tk, Tv> x : p)
    os << x << " ";
  os << "}" << endl;
  return os;
}
// -----------------------------------------------
const int INF = 1e7;
int n, m, sx, sy, dx, dy;
vector<vector<int>> grid, dist, vis;
int dxx[] = {0, 0, 1, -1};
int dyy[] = {1, -1, 0, 0};

bool chk(int _i, int _j) { return (_i < 0 || _j < 0 || _i >= n || _j >= m); }

void bfs() {

  queue<pair<int, int>> q;
  dist[sx][sy] = 0;

  q.push(make_pair(sx, sy));
  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop();

    int x = p.ff, y = p.ss;
    for (int i = 0; i < 4; i++) {

      int _x = x + dxx[i];
      int _y = y + dyy[i];

      if (chk(_x, _y))
        continue;

      if (grid[_x][_y] != -1) {
        if (dist[_x][_y] > dist[x][y]) {
          dist[_x][_y] = dist[x][y];
          q.push(make_pair(_x, _y));
        }
      } else {
        for (int k = -2; k <= 2; k++) {
          for (int l = -2; l <= 2; l++) {
            if (!chk(x + k, y + l) && grid[x + k][y + l] != -1) {
              if (dist[x + k][y + l] > dist[x][y] + 1) {
                dist[x + k][y + l] = dist[x][y] + 1;
                q.push(make_pair(x + k, y + l));
              }
            }
          }
        }
      }
    }
  }
}
signed main() {

  fastIO

          cin >>
      n >> m >> sx >> sy >> dx >> dy;
  --sx;
  --sy;
  --dx;
  --dy;

  string s;
  grid = vector<vector<int>>(n, vector<int>(m));
  dist = vector<vector<int>>(n, vector<int>(m, INF));
  vis = vector<vector<int>>(n, vector<int>(m, false));
  for (int i = 0; i < n; i++) {
    cin >> s;
    for (int j = 0; j < m; j++) {
      grid[i][j] = (s[j] == '.' ? 1 : -1);
    }
  }
  // cout << "grid:\\n";
  // cout << grid;
  //    for (int l = -2; l <= 2; l++) {
  //     for (int ll = -2; ll <= 2; ll++) {
  //         trace(3 + l,3 + ll);
  //     }
  // }

  bfs();

  cout << (dist[dx][dy] == INF ? -1 : dist[dx][dy]) << endl;

  return 0;
}