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

#include <bits/stdc++.h>
#include <queue>
#include <set>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = s; i < (int)(n); i++)
#define ll long long
#define ld long double
#define repl(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repsl(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define outdeb(fir, sec, thr) cout << fir << ":" << sec << ":" << thr << endl
#define readVarll(nam)                                                         \\
  ll nam;                                                                      \\
  cin >> nam
#define INF 99999999999
#define PLL pair<ll, ll>
#define TLLL tuple<ll, ll, ll>

string a[1001];
ll cnt[1001][1001];
ll h, w, ch, cw, dh, dw;
ll magicArea[5][5] = {{1, 1, 1, 1, 1},
                      {1, 1, 0, 1, 1},
                      {1, 0, 2, 0, 1},
                      {1, 1, 0, 1, 1},
                      {1, 1, 1, 1, 1}};

deque<TLLL> mydata;

void useMagic(ll nh, ll nw, ll hsub, ll wsub) {
  if (nh + hsub >= 0 && nh + hsub < h && nw + wsub >= 0 && nw + wsub < w &&
      a[nh + hsub][nw + wsub] == '.' &&
      cnt[nh + hsub][nw + wsub] > cnt[nh][nw] + 1) {
    cnt[nh + hsub][nw + wsub] = cnt[nh][nw] + 1;
    mydata.push_back(TLLL(nh + hsub, nw + wsub, cnt[nh][nw] + 1));
  }
}

void walk(ll nh, ll nw, ll hsub, ll wsub) {
  if (nh + hsub >= 0 && nh + hsub < h && nw + wsub >= 0 && nw + wsub < w &&
      a[nh + hsub][nw + wsub] == '.' &&
      cnt[nh + hsub][nw + wsub] > cnt[nh][nw]) {
    cnt[nh + hsub][nw + wsub] = cnt[nh][nw];
    mydata.push_front(TLLL(nh + hsub, nw + wsub, cnt[nh][nw]));
  }
}

ll calc() {
  while (!mydata.empty()) {
    TLLL x = mydata.front();
    mydata.pop_front();
    ll nh = get<0>(x);
    ll nw = get<1>(x);
    ll np = get<2>(x);

    if (cnt[nh][nw] < np) {
      continue;
    }

    if (nh == dh && nw == dw) {
      return cnt[nh][nw];
    }

    repsl(i, -2, 3) {
      repsl(j, -2, 3) {
        if (magicArea[i + 2][j + 2] == 0) {
          walk(nh, nw, i, j);
        } else if (magicArea[i + 2][j + 2] == 1) {
          useMagic(nh, nw, i, j);
        }
      }
    }
  }

  return cnt[dh][dw];
}

int main() {

  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  repl(i, h) {
    cin >> a[i];
    repl(j, w) { cnt[i][j] = INF; }
  }

  cnt[ch][cw] = 0;
  mydata.push_front(TLLL(ch, cw, 0));
  ll ans = calc();
  if (ans == INF)
    ans = -1;
  cout << fixed << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <queue>
#include <set>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for (int i = s; i < (int)(n); i++)
#define ll long long
#define ld long double
#define repl(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repsl(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define outdeb(fir, sec, thr) cout << fir << ":" << sec << ":" << thr << endl
#define readVarll(nam)                                                         \\
  ll nam;                                                                      \\
  cin >> nam
#define INF 99999999999
#define PLL pair<ll, ll>

string a[1001];
ll cnt[1001][1001];
ll h, w, ch, cw, dh, dw;
ll magicArea[5][5] = {{1, 1, 1, 1, 1},
                      {1, 1, 0, 1, 1},
                      {1, 0, 2, 0, 1},
                      {1, 1, 0, 1, 1},
                      {1, 1, 1, 1, 1}};

queue<PLL> mydata;

void useMagic(ll nh, ll nw, ll hsub, ll wsub) {
  if (nh + hsub >= 0 && nh + hsub < h && nw + wsub >= 0 && nw + wsub < w &&
      a[nh + hsub][nw + wsub] == '.' &&
      cnt[nh + hsub][nw + wsub] > cnt[nh][nw] + 1) {
    cnt[nh + hsub][nw + wsub] = cnt[nh][nw] + 1;
    mydata.push(PLL(nh + hsub, nw + wsub));
  }
}

void walk(ll nh, ll nw, ll hsub, ll wsub) {
  if (nh + hsub >= 0 && nh + hsub < h && nw + wsub >= 0 && nw + wsub < w &&
      a[nh + hsub][nw + wsub] == '.' &&
      cnt[nh + hsub][nw + wsub] > cnt[nh][nw]) {
    cnt[nh + hsub][nw + wsub] = cnt[nh][nw];
    mydata.push(PLL(nh + hsub, nw + wsub));
  }
}

ll calc(ll mtimes) {
  while (!mydata.empty()) {
    PLL x = mydata.front();
    mydata.pop();
    ll nh = x.first;
    ll nw = x.second;

    if (nh == dh && nw == dw) {
      return cnt[nh][nw];
    }

    repsl(i, -1, 2) {
      repsl(j, -1, 2) {
        if (magicArea[i + 2][j + 2] == 0) {
          walk(nh, nw, i, j);
        }
      }
    }
  }

  repl(i, h) repl(j, w) {
    if (cnt[i][j] == mtimes) {
      repsl(k, -2, 3) repsl(l, -2, 3) {
        if (magicArea[k + 2][l + 2] == 1) {
          useMagic(i, j, k, l);
        }
      }
    }
  }

  if (mydata.empty()) {
    return cnt[dh][dw];
  } else {
    return calc(mtimes + 1);
  }
}

int main() {

  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  repl(i, h) {
    cin >> a[i];
    repl(j, w) { cnt[i][j] = INF; }
  }

  cnt[ch][cw] = 0;
  mydata.push(PLL(ch, cw));
  ll ans = calc(0);
  if (ans == INF)
    ans = -1;
  cout << fixed << ans << endl;

  return 0;
}