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

#include <bits/stdc++.h>
#define lli long long int
using namespace std;
#define mod 1000000007
#define MOD 1000000037
#define mod1 998244353
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define INF 2e9
#define common cout << "Case #" << w + 1 << ": "

vector<vector<char>> v;
vector<vector<int>> dist;
int n, m;
int ix, iy, tx, ty;
vector<int> vx = {-1, 0, 1, 0}, vy = {0, -1, 0, 1};

bool isin(int x, int y) {
  if (x > -1 and y > -1 and x < n and y < m)
    return true;
  return false;
}

void bfs(int x, int y) {
  deque<pair<pair<int, int>, int>> q;
  q.push_front({{x, y}, 0});
  while (q.size() > 0) {
    pair<pair<int, int>, int> p = q.front();
    q.pop_front();
    int x = p.first.first, y = p.first.second, d = p.second;
    if (dist[x][y] < d or dist[tx][ty] <= d)
      continue;
    dist[x][y] = min(dist[x][y], d);
    for (int i = 0; i < 4; i++) {
      if (isin(x + vx[i], y + vy[i]) and v[x + vx[i]][y + vy[i]] != '#' and
          dist[x + vx[i]][y + vy[i]] > d) {
        dist[x + vx[i]][y + vy[i]] = d;
        q.push_front({{x + vx[i], y + vy[i]}, d});
      }
    }
    for (int i = max(0, x - 2); i <= min(x + 2, n - 1); i++) {
      for (int j = max(0, y - 2); j <= min(y + 2, m - 1); j++) {
        if (v[i][j] != '#' and dist[i][j] > d + 1) {
          dist[i][j] = d + 1;
          q.push_back({{i, j}, d + 1});
        }
      }
    }
  }
}

int main() {
  fastio;
  cin >> n >> m;
  cin >> ix >> iy >> tx >> ty;
  ix -= 1;
  iy -= 1;
  tx -= 1;
  ty -= 1;
  v.resize(n);
  dist.resize(n);
  for (int i = 0; i < n; i++) {
    v[i].resize(m);
    dist[i].resize(m, INF);
    for (int j = 0; j < m; j++)
      cin >> v[i][j];
  }
  bfs(ix, iy);
  if (dist[tx][ty] != INF)
    cout << dist[tx][ty] << endl;
  else
    cout << "-1" << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define lli long long int
using namespace std;
#define mod 1000000007
#define MOD 1000000037
#define mod1 998244353
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define INF 2e9
#define common cout << "Case #" << w + 1 << ": "

vector<vector<char>> v;
vector<vector<int>> dist;
int n, m;
int ix, iy, tx, ty;
vector<int> vx = {-1, 0, 1, 0}, vy = {0, -1, 0, 1};

bool isin(int x, int y) {
  if (x > -1 and y > -1 and x < n and y < m)
    return true;
  return false;
}

void bfs(int x, int y) {
  queue<pair<pair<int, int>, int>> q;
  q.push({{x, y}, 0});
  while (q.size() > 0) {
    pair<pair<int, int>, int> p = q.front();
    q.pop();
    int x = p.first.first, y = p.first.second, d = p.second;
    if (dist[x][y] < d or dist[tx][ty] <= d)
      continue;
    dist[x][y] = min(dist[x][y], d);
    for (int i = 0; i < 4; i++) {
      if (isin(x + vx[i], y + vy[i]) and v[x + vx[i]][y + vy[i]] != '#' and
          dist[x + vx[i]][y + vy[i]] > d) {
        dist[x + vx[i]][y + vy[i]] = d;
        q.push({{x + vx[i], y + vy[i]}, d});
      }
    }
    for (int i = max(0, x - 2); i <= min(x + 2, n - 1); i++) {
      for (int j = max(0, y - 2); j <= min(y + 2, m - 1); j++) {
        if (v[i][j] != '#' and dist[i][j] > d + 1) {
          dist[i][j] = d + 1;
          q.push({{i, j}, d + 1});
        }
      }
    }
  }
}

int main() {
  fastio;
  cin >> n >> m;
  cin >> ix >> iy >> tx >> ty;
  ix -= 1;
  iy -= 1;
  tx -= 1;
  ty -= 1;
  v.resize(n);
  dist.resize(n);
  for (int i = 0; i < n; i++) {
    v[i].resize(m);
    dist[i].resize(m, INF);
    for (int j = 0; j < m; j++)
      cin >> v[i][j];
  }
  bfs(ix, iy);
  if (dist[tx][ty] != INF)
    cout << dist[tx][ty] << endl;
  else
    cout << "-1" << endl;
}