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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>

#include <array>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

#define PI 3.14159265358979323846
#define int64 long long
#define uint64 unsigned long long
#define coutfix(i)                                                             \\
  cout << fixed << setprecision(i) // coutの浮動小数出力の桁数設定
using namespace std;

int main() {
  int64 hh, ww, cx, cy, dx, dy, tmpx, tmpy;
  int64 xfil[] = {-1, 1, 0, 0}, yfil[] = {0, 0, -1, 1};
  cin >> hh >> ww >> cx >> cy >> dx >> dy;
  cx--;
  cy--;
  dx--;
  dy--;
  int64 table[hh][ww];
  bool memo[hh][ww];
  string ss;
  for (int64 ii = 0; ii < hh; ii++) {
    cin >> ss;
    for (int64 jj = 0; jj < ww; jj++) {
      memo[ii][jj] = false;
      table[ii][jj] = 100000000;
      if (ss[jj] == '.') {
        memo[ii][jj] = true;
      }
    }
  }

  deque<pair<int64, int64>> deq;
  deq.push_back(make_pair(cx, cy));
  table[cx][cy] = 0;
  while (!deq.empty()) {
    tmpx = deq.front().first;
    tmpy = deq.front().second;
    deq.pop_front();
    for (int64 ii = 0; ii < 4; ii++) {
      if (tmpx + xfil[ii] >= 0 && tmpx + xfil[ii] < hh &&
          tmpy + yfil[ii] >= 0 && tmpy + yfil[ii] < ww) {
        if (table[tmpx][tmpy] < table[tmpx + xfil[ii]][tmpy + yfil[ii]] &&
            memo[tmpx + xfil[ii]][tmpy + yfil[ii]]) {
          deq.push_front(make_pair(tmpx + xfil[ii], tmpy + yfil[ii]));
          table[tmpx + xfil[ii]][tmpy + yfil[ii]] = table[tmpx][tmpy];
        }
      }
    }
    for (int64 ii = -2; ii <= 2; ii++) {
      for (int64 jj = -2; jj <= 2; jj++) {
        if (ii == 0 && jj == 0) {
          continue;
        }
        if (tmpx + ii >= 0 && tmpx + ii < hh && tmpy + jj >= 0 &&
            tmpy + jj < ww) {
          if (table[tmpx][tmpy] + 1 < table[tmpx + ii][tmpy + jj] &&
              memo[tmpx + ii][tmpy + jj]) {
            deq.push_back(make_pair(tmpx + ii, tmpy + jj));
            table[tmpx + ii][tmpy + jj] = table[tmpx][tmpy] + 1;
          }
        }
      }
    }
  }
  if (table[dx][dy] == 100000000) {
    cout << -1 << endl;
  } else {
    cout << table[dx][dy] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>

#include <array>
#include <deque>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>

#define PI 3.14159265358979323846
#define int64 long long
#define uint64 unsigned long long
#define coutfix(i)                                                             \\
  cout << fixed << setprecision(i) // coutの浮動小数出力の桁数設定
using namespace std;

bool table[1010][1010];
int64 fil1[5] = {-2, -1, 0, 1, 2};
int64 fil2[2] = {-1, 1};
int64 memo[1010][1010];
int64 mapping[1010][1010];
int64 ans = 0;
int64 now = 0;

void sol(int64 xx, int64 yy) {
  for (int64 ii = 0; ii < 2; ii++) {
    if (memo[xx + fil2[ii]][yy] > memo[xx][yy] && table[xx + fil2[ii]][yy]) {
      memo[xx + fil2[ii]][yy] = memo[xx][yy];
      mapping[xx + fil2[ii]][yy] = mapping[xx][yy];
      sol(xx + fil2[ii], yy);
    }
    if (memo[xx][yy + fil2[ii]] > memo[xx][yy] && table[xx][yy + fil2[ii]]) {
      memo[xx][yy + fil2[ii]] = memo[xx][yy];
      mapping[xx][yy + fil2[ii]] = mapping[xx][yy];
      sol(xx, yy + fil2[ii]);
    }
  }
  for (int64 ii = 0; ii < 5; ii++) {
    for (int64 jj = 0; jj < 5; jj++) {
      if (ii == 2 && jj == 2) {
        continue;
      } else {
        if (xx + fil1[ii] < 0 || yy + fil1[jj] < 0) {
          continue;
        }
        if ((ii == 1 && jj == 2) || (ii == 3 && jj == 2) ||
            (ii == 2 && jj == 1) || (ii == 2 && jj == 3)) {
          continue;
        }
        if (memo[xx + fil1[ii]][yy + fil1[jj]] > memo[xx][yy] + 1 &&
            table[xx + fil1[ii]][yy + fil1[jj]] &&
            mapping[xx + fil1[ii]][yy + fil1[jj]] != mapping[xx][yy]) {
          now++;
          memo[xx + fil1[ii]][yy + fil1[jj]] = memo[xx][yy] + 1;
          mapping[xx + fil1[ii]][yy + fil1[jj]] = now;
          sol(xx + fil1[ii], yy + fil1[jj]);
        }
      }
    }
  }
  return;
}

int main() {
  for (int64 ii = 0; ii < 1010; ii++) {
    for (int64 jj = 0; jj < 1010; jj++) {
      table[ii][jj] = false;
      memo[ii][jj] = 1000000000;
      mapping[ii][jj] = -1;
    }
  }
  int64 hh, ww;
  char tmp;
  cin >> hh >> ww;
  int64 cx, cy, dx, dy;
  cin >> cx >> cy >> dx >> dy;
  for (int64 ii = 1; ii <= hh; ii++) {
    for (int64 jj = 1; jj <= ww; jj++) {
      cin >> tmp;
      if (tmp == '.') {
        table[ii][jj] = true;
      }
    }
  }
  memo[cx][cy] = 0;
  mapping[cx][cy] = now;
  sol(cx, cy);
  if (memo[dx][dy] != 1000000000) {
    cout << memo[dx][dy] << endl;
  } else {
    puts("-1");
  }
  return 0;
}