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

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
using VB = vector<bool>;
using VVB = vector<VB>;
#define REP(i, m, n) for (int i = (m); i < (n); i++)
// i=m -> n-1(昇順)
#define RREP(i, m, n) for (int i = (m); i >= (n); i--)
// i=m -> n(降順)

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

int main() {
  LL h, w, ch, cw, dh, dw, cost = 0;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  VS s(h);
  REP(i, 0, h) { cin >> s[i]; }

  bool finished = false;
  VVLL cost_info(h, VLL(w, -1));
  queue<PII> que;
  queue<PII> warp;

  cost_info[ch][cw] = 0;
  que.push(make_pair(ch, cw));

  while (!que.empty()) {
    while (!que.empty()) {
      int x = que.front().first;
      int y = que.front().second;
      que.pop();
      warp.push(make_pair(x, y));

      REP(dir, 0, 4) {
        int nx = x + dx[dir];
        int ny = y + dy[dir];
        if (nx < 0 || nx >= h || ny < 0 || ny >= w) {
          continue;
        }
        if (s[nx][ny] == '#') {
          continue;
        }

        if (cost_info[nx][ny] == -1) {
          que.push(make_pair(nx, ny));
          cost_info[nx][ny] = cost;
        }
      }
    }

    while (!warp.empty()) {
      int x = warp.front().first;
      int y = warp.front().second;
      warp.pop();

      REP(i, -2, 3) {
        REP(j, -2, 3) {
          int nx = x + i;
          int ny = y + j;
          if (nx < 0 || nx >= h || ny < 0 || ny >= w) {
            continue;
          }
          if (s[nx][ny] == '#') {
            continue;
          }

          if (cost_info[nx][ny] == -1) {
            que.push(make_pair(nx, ny));
            cost_info[nx][ny] = cost + 1;
          }
        }
      }
    }
    cost++;
  }

  cout << cost_info[dh][dw] << endl;
  /*REP(i,0,h){
    REP(j,0,w){
      cout << cost_info[i][j] << " ";
    }
    cout << endl;
  }*/

  return 0;
}


Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int, int>;
using VP = vector<PII>;
using VB = vector<bool>;
using VVB = vector<VB>;
#define REP(i, m, n) for (int i = (m); i < (n); i++)
// i=m -> n-1(昇順)
#define RREP(i, m, n) for (int i = (m); i >= (n); i--)
// i=m -> n(降順)

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int wy[20] = {2, 2,  2,  2,  2,  1,  1,  1,  1,  0,
                    0, -1, -1, -1, -1, -2, -2, -2, -2, -2};
const int wx[20] = {-2, -1, 0,  1, 2, -2, -1, 1, 2, -2,
                    2,  -2, -1, 1, 2, -2, -1, 0, 1, 2};
const int INF = 1 << 30;

int main() {
  LL h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  VS s(h);
  REP(i, 0, h) { cin >> s[i]; }

  VVI cost_info(h, VI(w, INF));
  queue<PII> que;

  cost_info[ch][cw] = 0;
  que.emplace(make_pair(ch, cw));

  while (!que.empty()) {
    int x = que.front().first;
    int y = que.front().second;
    que.pop();
    int cost = cost_info[x][y];

    REP(dir, 0, 4) {
      int next_x = x + dx[dir];
      int next_y = y + dy[dir];
      if (next_x < 0 || next_x >= h || next_y < 0 || next_y >= w) {
        continue;
      }
      if (s[next_x][next_y] == '#') {
        continue;
      }
      if (cost_info[next_x][next_y] <= cost) {
        continue;
      }

      que.emplace(make_pair(next_x, next_y));
      cost_info[next_x][next_y] = cost;
    }

    cost++;
    REP(dir, 0, 20) {
      int next_x = x + wx[dir];
      int next_y = y + wy[dir];
      if (next_x < 0 || next_x >= h || next_y < 0 || next_y >= w) {
        continue;
      }
      if (s[next_x][next_y] == '#') {
        continue;
      }
      if (cost_info[next_x][next_y] <= cost) {
        continue;
      }

      que.emplace(make_pair(next_x, next_y));
      cost_info[next_x][next_y] = cost;
    }
  }

  int ans = cost_info[dh][dw];
  if (ans == INF) {
    ans = -1;
  }
  cout << ans << endl;

  return 0;
}