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

#include <bits/stdc++.h>
using namespace std;

#define ll                                                                     \\
  long long // data types used often, but you don't want to type them time by
            // time
#define ull unsigned long long
#define ui unsigned int
#define mod 1000000007
#define inf 100000000000010
#define us unsigned short
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0); // to synchronize the input of cin and scanf
#define INF LONG_MAX
#define PI 3.1415926535897932384626
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define rep(x, j, n) for (int x = j; x < n; x++)
#define all(x) (x).begin(), (x).end()

typedef vector<pair<ll, ll>> vpi;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pi;

bool isprime(ll n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (ll i = 5; i * i <= n; i += 6) {
    if (n % i == 0 || n % (i + 2) == 0)
      return false;
  }
  return true;
}

ll modexp(ll a, ll b, ll m) {
  ll r = 1;
  a = a % m;
  while (b > 0) {
    if (b & 1)
      r = (r * a) % m;
    b = b >> 1;
    a = (a * a) % m;
  }
  return r % m;
}

struct CustomCompare {
  bool operator()(const int &lhs, const int &rhs) { return lhs < rhs; }
};

bool comp(pair<ll, ll> a, pair<ll, ll> b) { return a.fi >= b.fi; }

ll builtin_popcount(ll x) {
  ll cnt = 0;
  for (ll i = 60; i >= 0; i--) {
    if ((1LL << i) & x)
      cnt++;
  }
  return cnt;
}

ll addmod(ll a, ll b) {
  a %= mod;
  b %= mod;
  return (a + b) % mod;
}
ll mulmod(ll a, ll b) {
  a %= mod;
  b %= mod;
  return (a * b) % mod;
}
/*
const int mxn = 5*(1e6) +10;
vi spf(mxn);
void sieve()
{
    for(ll i =0;i<mxn;i++)spf[i]  = i;
    for(ll i=2;i<mxn;i+=2)spf[i] = 2;
    for(ll i = 3;i*i<mxn;i++)
    {
        if(spf[i]==i)
        {
            for(ll j = i*i;j<mxn;j+=i)
            {
                if(spf[j]==j)spf[j] = i;
            }
        }
    }
}
*/

const int N = 1e3 + 10, M = 1e3 + 10;
ll n, m;
vector<string> arr;
ll dp[N][M];
bool check(ll x, ll y) {
  if (x >= 0 && x < n && y < m && y >= 0 && arr[x][y] == '.')
    return true;
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  arr = vector<string>(n);
  ll x, y, a, b;
  cin >> x >> y >> a >> b;
  x--, y--, a--, b--;
  rep(i, 0, n) cin >> arr[i];

  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j < M; j++)
      dp[i][j] = mod;
  }
  ll dx[] = {-1, 0, 0, 1};
  ll dy[] = {0, -1, 1, 0};
  deque<pair<pi, ll>> q;
  q.push_back({{x, y}, 0});
  dp[x][y] = 0;
  while (!q.empty()) {
    auto tp = q.front();
    q.pop_front();
    rep(i, 0, 4) {
      ll xx = dx[i] + tp.fi.fi, yy = tp.fi.se + dy[i];
      if (check(xx, yy)) {
        if (dp[xx][yy] <= dp[tp.fi.fi][tp.fi.se])
          continue;
        dp[xx][yy] = tp.se;
        q.push_front({{xx, yy}, tp.se});
      }
    }

    for (ll i = -2; i <= 2; i++) {
      for (ll j = -2; j <= 2; j++) {
        ll xx = tp.fi.fi + i, yy = tp.fi.se + j;
        if (check(xx, yy)) {
          if (dp[xx][yy] <= dp[tp.fi.fi][tp.fi.se] + 1)
            continue;
          dp[xx][yy] = tp.se + 1;
          q.push_back({{xx, yy}, tp.se + 1});
        }
      }
    }
  }
  if (dp[a][b] == mod)
    cout << -1 << endl;
  else
    cout << dp[a][b] << endl;
  return 0;
}
/*
1
4
1 2
2 3
3 4
2
2 2
*/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ll                                                                     \\
  long long // data types used often, but you don't want to type them time by
            // time
#define ull unsigned long long
#define ui unsigned int
#define mod 1000000007
#define inf 100000000000010
#define us unsigned short
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0); // to synchronize the input of cin and scanf
#define INF LONG_MAX
#define PI 3.1415926535897932384626
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define rep(x, j, n) for (int x = j; x < n; x++)
#define all(x) (x).begin(), (x).end()

typedef vector<pair<ll, ll>> vpi;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> pi;

bool isprime(ll n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (ll i = 5; i * i <= n; i += 6) {
    if (n % i == 0 || n % (i + 2) == 0)
      return false;
  }
  return true;
}

ll modexp(ll a, ll b, ll m) {
  ll r = 1;
  a = a % m;
  while (b > 0) {
    if (b & 1)
      r = (r * a) % m;
    b = b >> 1;
    a = (a * a) % m;
  }
  return r % m;
}

struct CustomCompare {
  bool operator()(const int &lhs, const int &rhs) { return lhs < rhs; }
};

bool comp(pair<ll, ll> a, pair<ll, ll> b) { return a.fi >= b.fi; }

ll builtin_popcount(ll x) {
  ll cnt = 0;
  for (ll i = 60; i >= 0; i--) {
    if ((1LL << i) & x)
      cnt++;
  }
  return cnt;
}

ll addmod(ll a, ll b) {
  a %= mod;
  b %= mod;
  return (a + b) % mod;
}
ll mulmod(ll a, ll b) {
  a %= mod;
  b %= mod;
  return (a * b) % mod;
}
/*
const int mxn = 5*(1e6) +10;
vi spf(mxn);
void sieve()
{
    for(ll i =0;i<mxn;i++)spf[i]  = i;
    for(ll i=2;i<mxn;i+=2)spf[i] = 2;
    for(ll i = 3;i*i<mxn;i++)
    {
        if(spf[i]==i)
        {
            for(ll j = i*i;j<mxn;j+=i)
            {
                if(spf[j]==j)spf[j] = i;
            }
        }
    }
}
*/

const int N = 1e3 + 10, M = 1e3 + 10;
ll n, m;
vector<string> arr;
ll dp[N][M];
bool check(ll x, ll y) {
  if (x >= 0 && x < n && y < m && y >= 0 && arr[x][y] == '.')
    return true;
  return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  arr = vector<string>(n);
  ll x, y, a, b;
  cin >> x >> y >> a >> b;
  x--, y--, a--, b--;
  rep(i, 0, n) cin >> arr[i];

  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j < M; j++)
      dp[i][j] = mod;
  }
  ll dx[] = {-1, 0, 0, 1};
  ll dy[] = {0, -1, 1, 0};
  ll cnt = 1e8;
  queue<pair<pi, ll>> q;
  q.push({{x, y}, 0});
  while (!q.empty() && cnt--) {
    auto tp = q.front();
    q.pop();
    // cout<<tp.fi.fi<<" "<<tp.fi.se<<endl;

    if (tp.se >= dp[tp.fi.fi][tp.fi.se])
      continue;
    dp[tp.fi.fi][tp.fi.se] = tp.se;
    // if(tp.fi.fi==a&&tp.fi.se==b)break;
    rep(i, 0, 4) {
      ll xx = dx[i] + tp.fi.fi, yy = tp.fi.se + dy[i];
      if (check(xx, yy)) {
        if (dp[xx][yy] <= tp.se)
          continue;
        // dp[xx][yy] = tp.se;
        q.push({{xx, yy}, tp.se});
      }
    }

    for (ll i = -2; i <= 2; i++) {
      for (ll j = -2; j <= 2; j++) {
        ll xx = tp.fi.fi + i, yy = tp.fi.se + j;
        if (check(xx, yy)) {
          if (dp[xx][yy] <= tp.se + 1)
            continue;
          // dp[xx][yy] = tp.se+1;
          q.push({{xx, yy}, tp.se + 1});
        }
      }
    }
  }
  if (dp[a][b] == mod)
    cout << -1 << endl;
  else
    cout << dp[a][b] << endl;
  return 0;
}
/*
1
4
1 2
2 3
3 4
2
2 2
*/