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

#include <bits/stdc++.h>
#include <iomanip>
#define fast                                                                   \\
  {                                                                            \\
    ios_base::sync_with_stdio(false);                                          \\
    cin.tie(NULL);                                                             \\
  }
#define mxn 2000
#define mod 1000000007
#define ld long double
#define pb push_back
#define inf 1e18
#define fi first
#define se second
#define ld long double
typedef long long int ll;
using namespace std;
string s[mxn];
ll ans[mxn][mxn], n, m;
bool vis[mxn][mxn];
bool inside(ll x, ll y) {
  if (x >= 0 && x < n && y >= 0 && y < m && vis[x][y] == 0 && s[x][y] == '.')
    return 1;
  else
    return 0;
}
void bfs(ll x, ll y) {
  //	cout<<x<<" "<<y<<" "<<cr<<endl;
  deque<pair<ll, ll>> q;
  q.pb({x, y});
  vis[x][y] = 1;
  while (!q.empty()) {
    pair<ll, ll> k = q.front();
    q.pop_front();
    vis[k.fi][k.se] = 1;
    for (ll i = -1; i <= 1; i++) {
      for (ll j = -1; j <= 1; j++) {
        if ((abs(i) + abs(j)) != 1)
          continue;
        if (inside(k.fi + i, k.se + j)) {
          vis[k.fi + i][k.se + j] = 1;
          q.push_front({k.fi + i, k.se + j});
          ans[k.fi + i][k.se + j] =
              min(ans[k.fi + i][k.se + j], ans[k.fi][k.se]);
        }
      }
    }
    for (ll i = -2; i <= 2; i++) {
      for (ll j = -2; j <= 2; j++) {
        if (abs(i) + abs(j) == 1 || abs(i) + abs(j) == 0)
          continue;
        if (inside(k.fi + i, k.se + j)) {
          // vis[k.fi+i][k.se+j]=1;
          q.pb({k.fi + i, k.se + j});
          ans[k.fi + i][k.se + j] =
              min(ans[k.fi + i][k.se + j], ans[k.fi][k.se] + 1LL);
        }
      }
    }
  }
}
int main(void) {
#ifndef ONLINE_JUDGE
  freopen("input1.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast;
  ll i, j, si, sj, ei, ej;
  cin >> n >> m;
  cin >> si >> sj >> ei >> ej;
  for (i = 0; i < n; i++)
    cin >> s[i];
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++)
      ans[i][j] = inf;
  }
  ans[si - 1][sj - 1] = 0;
  bfs(si - 1, sj - 1);
  if (!vis[ei - 1][ej - 1])
    cout << -1 << endl;
  else
    cout << ans[ei - 1][ej - 1];
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iomanip>
#define fast                                                                   \\
  {                                                                            \\
    ios_base::sync_with_stdio(false);                                          \\
    cin.tie(NULL);                                                             \\
  }
#define mxn 2000
#define mod 1000000007
#define ld long double
#define pb push_back
#define inf 1e18
#define fi first
#define se second
#define ld long double
typedef long long int ll;
using namespace std;
string s[mxn];
ll ans[mxn][mxn], n, m;
bool vis[mxn][mxn];
bool inside(ll x, ll y) {
  if (x >= 0 && x < n && y >= 0 && y < m && vis[x][y] == 0 && s[x][y] == '.')
    return 1;
  else
    return 0;
}
void bfs(ll x, ll y) {
  //	cout<<x<<" "<<y<<" "<<cr<<endl;
  queue<pair<ll, ll>> q;
  q.push({x, y});
  vis[x][y] = 1;
  while (!q.empty()) {
    pair<ll, ll> k = q.front();
    q.pop();
    vis[k.fi][k.se] = 1;
    for (ll i = -1; i <= 1; i++) {
      for (ll j = -1; j <= 1; j++) {
        if ((abs(i) + abs(j)) != 1)
          continue;
        if (inside(k.fi + i, k.se + j)) {
          vis[k.fi + i][k.se + j] = 1;
          q.push({k.fi + i, k.se + j});
          ans[k.fi + i][k.se + j] =
              min(ans[k.fi + i][k.se + j], ans[k.fi][k.se]);
        }
      }
    }
    for (ll i = -2; i <= 2; i++) {
      for (ll j = -2; j <= 2; j++) {
        if (abs(i) + abs(j) == 1 || abs(i) + abs(j) == 0)
          continue;
        if (inside(k.fi + i, k.se + j)) {
          // vis[k.fi+i][k.se+j]=1;
          q.push({k.fi + i, k.se + j});
          ans[k.fi + i][k.se + j] =
              min(ans[k.fi + i][k.se + j], ans[k.fi][k.se] + 1LL);
        }
      }
    }
  }
}
int main(void) {
#ifndef ONLINE_JUDGE
  freopen("input1.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast;
  ll i, j, si, sj, ei, ej;
  cin >> n >> m;
  cin >> si >> sj >> ei >> ej;
  for (i = 0; i < n; i++)
    cin >> s[i];
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++)
      ans[i][j] = inf;
  }
  ans[si - 1][sj - 1] = 0;
  bfs(si - 1, sj - 1);
  if (!vis[ei - 1][ej - 1])
    cout << -1 << endl;
  else
    cout << ans[ei - 1][ej - 1];
}