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

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep01(i, n) for (int i = 0; i < (n) + 1; i++)
#define rep10(i, n) for (int i = 1; i < (n); i++)
#define rep11(i, n) for (int i = 1; i < (n) + 1; i++)
#define all(a) (a).begin(), (a).end()
typedef long long int ll;
typedef struct {
  int x;
  int y;
} Pos;
using namespace std;

int H, W;
int Cw, Ch;
int Dw, Dh;
int cost = 0;
bool reach = false;
vector<vector<char>> S;
vector<vector<bool>> R;
vector<vector<int>> C;
queue<Pos> innerQ;
queue<Pos> jumpQ;
int main() {
  cin >> H >> W;
  cin >> Ch >> Cw;
  cin >> Dh >> Dw;
  S = vector<vector<char>>(W + 1, vector<char>(H + 1));
  R = vector<vector<bool>>(W + 1, vector<bool>(H + 1));
  C = vector<vector<int>>(W + 1, vector<int>(H + 1, INT_MAX));
  rep11(y, H) rep11(x, W) cin >> S[x][y];
  innerQ.push(Pos{Cw, Ch});
  jumpQ.push(Pos{Cw, Ch});
  R[Cw][Ch] = true;
  C[Cw][Ch] = cost;
  while (0 < innerQ.size()) {
    // 内部探索
    while (0 < innerQ.size()) {
      const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
      Pos ref = innerQ.front();
      innerQ.pop();
      rep(i, 4) {
        Pos chk = Pos{ref.x + dx[i], ref.y + dy[i]};
        if (chk.x <= 0 || chk.x > W || chk.y <= 0 || chk.y > H)
          continue;
        if (S[chk.x][chk.y] == '#')
          continue;
        if (R[chk.x][chk.y])
          continue;
        if (C[chk.x][chk.y] <= cost)
          continue;
        R[chk.x][chk.y] = true;
        C[chk.x][chk.y] = cost;
        innerQ.push(Pos{chk.x, chk.y});
        jumpQ.push(Pos{chk.x, chk.y});
      }
    }
    // ジャンプ探索
    int jump = jumpQ.size();
    rep(i, jump) {
      Pos ref = jumpQ.front();
      jumpQ.pop();
      for (int dy = -2; dy < 3; dy++) {
        for (int dx = -2; dx < 3; dx++) {
          if (ref.x + dx <= 0 || ref.x + dx > W || ref.y + dy <= 0 ||
              ref.y + dy > H)
            continue;
          if (!R[ref.x + dx][ref.y + dy] &&
              (S[ref.x + dx][ref.y + dy] == '.')) {
            R[ref.x + dx][ref.y + dy] = true;
            C[ref.x + dx][ref.y + dy] = cost + 1;
            innerQ.push(Pos{ref.x + dx, ref.y + dy});
            jumpQ.push(Pos{ref.x + dx, ref.y + dy});
          }
        }
      }
    }
    if (R[Dw][Dh]) {
      reach = true;
      break;
    }
    cost++;
  }
  if (reach)
    cout << C[Dw][Dh] << endl;
  else
    cout << -1 << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep01(i, n) for (int i = 0; i < (n) + 1; i++)
#define rep10(i, n) for (int i = 1; i < (n); i++)
#define rep11(i, n) for (int i = 1; i < (n) + 1; i++)
#define all(a) (a).begin(), (a).end()
typedef long long int ll;
typedef struct {
  int x;
  int y;
} Pos;
using namespace std;

int H, W;
int Cw, Ch;
int Dw, Dh;
int cost = 0;
bool reach = false;
vector<vector<char>> S;
vector<vector<bool>> R;
vector<vector<int>> C;
queue<Pos> innerQ;
queue<Pos> jumpQ;
void debug() {
  rep11(y, H) {
    rep11(x, W) {
      if (R[x][y])
        cout << C[x][y] << " ";
      else
        cout << "- ";
    }
    cout << endl;
  }
  cout << endl;
}
int main() {
  cin >> H >> W;
  cin >> Ch >> Cw;
  cin >> Dh >> Dw;
  S = vector<vector<char>>(W + 1, vector<char>(H + 1));
  R = vector<vector<bool>>(W + 1, vector<bool>(H + 1));
  C = vector<vector<int>>(W + 1, vector<int>(H + 1, INT_MAX));
  rep11(y, H) rep11(x, W) cin >> S[x][y];
  innerQ.push(Pos{Cw, Ch});
  R[Cw][Ch] = true;
  C[Cw][Ch] = cost;
  while (0 < innerQ.size()) {
    // 内部探索
    while (0 < innerQ.size()) {
      const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
      Pos ref = innerQ.front();
      innerQ.pop();
      rep(i, 4) {
        Pos chk = Pos{ref.x + dx[i], ref.y + dy[i]};
        if (chk.x <= 0 || chk.x > W || chk.y <= 0 || chk.y > H)
          continue;
        if (S[chk.x][chk.y] == '#')
          continue;
        if (R[chk.x][chk.y])
          continue;
        if (C[chk.x][chk.y] <= cost)
          continue;
        R[chk.x][chk.y] = true;
        C[chk.x][chk.y] = cost;
        innerQ.push(Pos{chk.x, chk.y});
      }
    }
    //        debug();
    // ジャンプ探索
    rep11(y, H) {
      rep11(x, W) {
        if (R[x][y] && S[x][y] == '.' && C[x][y] == cost) {
          for (int dy = -2; dy < 3; dy++) {
            for (int dx = -2; dx < 3; dx++) {
              if (x + dx <= 0 || x + dx > W || y + dy <= 0 || y + dy > H)
                continue;
              if (!R[x + dx][y + dy] && (S[x + dx][y + dy] == '.')) {
                R[x + dx][y + dy] = true;
                C[x + dx][y + dy] = cost + 1;
                innerQ.push(Pos{x + dx, y + dy});
              }
            }
          }
        }
      }
    }
    if (R[Dw][Dh]) {
      reach = true;
      break;
    }
    cost++;
  }
  if (reach)
    cout << C[Dw][Dh] << endl;
  else
    cout << -1 << endl;
  return 0;
}