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

#include <bits/stdc++.h>
#define INF 1e9
#define INFLL 1ull << 60u
using namespace std;

#define REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()
#define endl "\\n"

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;

void print_map(const vector<vector<char>> &vv) {
  for (const auto &str : vv) {
    for (const auto &c : str)
      cout << c;
    cout << endl;
  }
}

int vh[4] = {0, 0, -1, 1};
int vw[4] = {1, -1, 0, 0};

int magic[5] = {-2, -1, 0, 1, 2};

vector<vector<int>> cost_map;
void solve() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> vv(H + 4, vector<char>(W + 4, '#'));

  pair<int, int> start, dest;
  int sh, sw, dh, dw;

  cin >> sh >> sw;
  sh++;
  sw++;
  start = make_pair(sh, sw);

  cin >> dh >> dw;
  dh++;
  dw++;
  dest = make_pair(dh, dw);

  REP(i, H) {
    string s;
    cin >> s;
    REP(j, W) vv[i + 2][j + 2] = s[j];
  }

  cost_map.resize(H + 4, vector<int>(W + 4, INF));

  cost_map[sh][sw] = 0;

  auto bfs = [&]() {
    deque<pair<int, int>> qp;
    qp.push_front(start);

    while (!qp.empty()) {
      auto [h, w] = qp.front();
      qp.pop_front();
      int cnt = 0;

      // 上下左右移動
      REP(i, 4) if (vv[h + vh[i]][w + vw[i]] == '.') {
        cnt++;
        if (chmin(cost_map[h + vh[i]][w + vw[i]], cost_map[h][w])) {
          qp.push_front(make_pair(h + vh[i], w + vw[i]));
        }
      }

      // 上下左右に道があった場合はべつにそこから25マス見る必要はないから。
      if (cnt == 4)
        continue;

      // 25マス移動
      REP(i, 5) REP(j, 5) {
        if (vv[h + magic[i]][w + magic[j]] == '.') {
          if (chmin(cost_map[h + magic[i]][w + magic[j]], cost_map[h][w] + 1))
            qp.push_back(make_pair(h + magic[i], w + magic[j]));
        }
      }
    }
  };
  bfs();

  if (cost_map[dh][dw] != INF)
    cout << cost_map[dh][dw] << endl;
  else
    cout << -1 << endl;
}

int main() {
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 1e9
#define INFLL 1ull << 60u
using namespace std;

#define REPR(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define ALL(a) (a).begin(), (a).end()
#define endl "\\n"

template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;

void print_map(const vector<vector<char>> &vv) {
  for (const auto &str : vv) {
    for (const auto &c : str)
      cout << c;
    cout << endl;
  }
}

int vh[4] = {0, 0, -1, 1};
int vw[4] = {1, -1, 0, 0};

int magic[5] = {-2, -1, 0, 1, 2};

vector<vector<int>> cost_map;
void solve() {
  int H, W;
  cin >> H >> W;
  vector<vector<char>> vv(H + 4, vector<char>(W + 4, '#'));

  pair<int, int> start, dest;
  int sh, sw, dh, dw;

  cin >> sh >> sw;
  sh++;
  sw++;
  start = make_pair(sh, sw);

  cin >> dh >> dw;
  dh++;
  dw++;
  dest = make_pair(dh, dw);

  REP(i, H) {
    string s;
    cin >> s;
    REP(j, W) vv[i + 2][j + 2] = s[j];
  }

  cost_map.resize(H + 4, vector<int>(W + 4, INF));

  cost_map[sh][sw] = 0;

  auto bfs = [&]() {
    queue<pair<int, int>> qp;
    qp.push(start);

    while (!qp.empty()) {
      auto [h, w] = qp.front();
      qp.pop();
      int cnt = 0;

      // 上下左右移動
      REP(i, 4) if (vv[h + vh[i]][w + vw[i]] == '.') {
        cnt++;
        if (chmin(cost_map[h + vh[i]][w + vw[i]], cost_map[h][w])) {
          qp.push(make_pair(h + vh[i], w + vw[i]));
        }
      }

      // 上下左右に道があった場合はべつにそこから25マス見る必要はないから。
      if (cnt == 4)
        continue;

      // 25マス移動
      REP(i, 5) REP(j, 5) {
        if (vv[h + magic[i]][w + magic[j]] == '.') {
          if (chmin(cost_map[h + magic[i]][w + magic[j]], cost_map[h][w] + 1))
            qp.push(make_pair(h + magic[i], w + magic[j]));
        }
      }
    }
  };
  bfs();

  if (cost_map[dh][dw] != INF)
    cout << cost_map[dh][dw] << endl;
  else
    cout << -1 << endl;
}

int main() {
  solve();
  return 0;
}