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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define ar array
#define db double
#define pow pw
const db pi = 3.14159265358979323846;
int pw(int a, int b) {
  int ans = 1;
  while (b) {
    if (b % 2)
      ans *= a;
    a *= a;
    b /= 2;
  }
  return (ans);
}
const int mxn = 1e3 + 7, INF = 1e9 + 7;
int m, n, sx, sy, gx, gy, ans;
char a[mxn][mxn];
int dr[] = {-1, 0, 1, 0};
int dc[] = {0, -1, 0, 1};
int vis[mxn][mxn];
// int vs[mxn][mxn];
vector<vector<int>> dis;
bool check(int u, int v) {
  return (u > 0 && u <= m && v > 0 && v <= n && a[u][v] == '.');
}
void bfs(int U, int V) {
  deque<ar<int, 2>> q;
  q.push_front({U, V});
  dis[U][V] = 0;
  // vs[U][V]=1;
  while (q.size()) {
    int u = q.front()[0], v = q.front()[1];
    q.pop_front();
    if (vis[u][v])
      continue;
    vis[u][v] = 1;
    for (int i = 0; i < 4; i++) {
      int nu = u + dr[i];
      int nv = v + dc[i];
      if (check(nu, nv) && !vis[nu][nv]) {
        q.push_front({nu, nv});
        dis[nu][nv] = dis[u][v];
        // vs[nu][nv]=1;
      }
    }
    for (int i = u - 2; i <= u + 2; i++) {
      for (int j = v - 2; j <= v + 2; j++) {
        if (check(i, j)) {
          if (dis[i][j] > dis[u][v] + 1) {
            dis[i][j] = dis[u][v] + 1;
            q.push_back({i, j});
          }
        }
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> m >> n >> sx >> sy >> gx >> gy;
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++)
      cin >> a[i][j];
  dis.assign(m + 1, vector<int>(n + 1, INF));
  bfs(sx, sy);
  if (dis[gx][gy] != INF)
    cout << dis[gx][gy];
  else
    cout << -1;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define ar array
#define db double
#define pow pw
const db pi = 3.14159265358979323846;
int pw(int a, int b) {
  int ans = 1;
  while (b) {
    if (b % 2)
      ans *= a;
    a *= a;
    b /= 2;
  }
  return (ans);
}
const int mxn = 1e3 + 7, INF = 1e9 + 7;
int m, n, sx, sy, gx, gy, ans;
char a[mxn][mxn];
int dr[] = {-1, 0, 1, 0};
int dc[] = {0, -1, 0, 1};
int vis[mxn][mxn];
vector<vector<int>> dis;
bool check(int u, int v) {
  return (u > 0 && u <= m && v > 0 && v <= n && a[u][v] == '.');
}
void bfs(int U, int V) {
  queue<ar<int, 2>> q;
  q.push({U, V});
  dis[U][V] = 0;
  while (q.size()) {
    int u = q.front()[0], v = q.front()[1];
    q.pop();
    if (vis[u][v] && dis[u][v] == 0 && u != U && v != V)
      continue;
    vis[u][v] = 1;
    for (int i = 0; i < 4; i++) {
      int nu = u + dr[i];
      int nv = v + dc[i];
      if (check(nu, nv) && !vis[nu][nv]) {
        q.push({nu, nv});
        dis[nu][nv] = dis[u][v];
      }
    }
    for (int i = u - 2; i <= u + 2; i++) {
      for (int j = v - 2; j <= v + 2; j++) {
        if (check(i, j) && !vis[i][j] && dis[i][j] >= dis[u][v] + 1) {
          dis[i][j] = dis[u][v] + 1;
          q.push({i, j});
        }
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> m >> n >> sx >> sy >> gx >> gy;
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++)
      cin >> a[i][j];
  dis.assign(m + 1, vector<int>(n + 1, INF));
  bfs(sx, sy);
  if (dis[gx][gy] != INF)
    cout << dis[gx][gy];
  else
    cout << -1;
  return 0;
}