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

#include <bits/stdc++.h>

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define dump_1(x1) cerr << #x1 << ": " << x1 << endl
#define dump_2(x1, x2)                                                         \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define dump_3(x1, x2, x3)                                                     \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << endl
#define dump_4(x1, x2, x3, x4)                                                 \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << endl
#define dump_5(x1, x2, x3, x4, x5)                                             \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define dump(...)                                                              \\
  CHOOSE((__VA_ARGS__, dump_5, dump_4, dump_3, dump_2, dump_1, ~))(__VA_ARGS__)
#define check(s) cerr << s << endl

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

using namespace std;

using ll = long long;

vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

const ll LINF = 2e18;
const int INF = 1e9;

int H, W;
int Ch, Cw;
int Dh, Dw;
vector<vector<char>> S;

struct point {
  int y, x;

  bool operator==(const point &rhs) const { return (x == rhs.x && y == rhs.y); }
  bool operator<(const point &rhs) const {
    return (x < rhs.x || (x == rhs.x && y < rhs.y));
  }
};
typedef struct point Point;

void solve() {
  vector<vector<bool>> dist(H, vector<bool>(W, false));
  priority_queue<pair<int, Point>, vector<pair<int, Point>>,
                 greater<pair<int, Point>>>
      q;

  q.push({0, {Ch, Cw}});
  ll result = -1;

  while (!q.empty()) {
    auto v = q.top();
    q.pop();
    int w = v.first; // ワープカウント
    Point p = v.second;

    if (dist.at(p.y).at(p.x))
      continue;
    dist.at(p.y).at(p.x) = true;

    if (p.y == Dh && p.x == Dw) {
      result = w;
      break;
    }

    // Move
    rep(i, 4) {
      int nx = p.x + dx[i];
      int ny = p.y + dy[i];
      if (nx < 0 || ny < 0 || nx >= W || ny >= H)
        continue;
      if (S.at(ny).at(nx) == '#')
        continue;
      if (!dist.at(ny).at(nx)) {
        q.push({w, {ny, nx}});
      }
    }

    // Warp
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = p.x + i;
        int ny = p.y + j;
        if (nx < 0 || ny < 0 || nx >= W || ny >= H)
          continue;
        if (S.at(ny).at(nx) == '#')
          continue;
        if (!dist.at(ny).at(nx)) {
          q.push({w + 1, {ny, nx}});
        }
      }
    }
  }

  cout << result << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  cin >> H >> W;
  cin >> Ch >> Cw;
  cin >> Dh >> Dw;

  // 0-indexed
  Ch--;
  Cw--;
  Dh--;
  Dw--;

  S.resize(H, vector<char>(W));

  rep(i, H) {
    rep(j, W) {
      char c;
      cin >> c;
      S.at(i).at(j) = c;
    }
  }

  solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define dump_1(x1) cerr << #x1 << ": " << x1 << endl
#define dump_2(x1, x2)                                                         \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define dump_3(x1, x2, x3)                                                     \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << endl
#define dump_4(x1, x2, x3, x4)                                                 \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << endl
#define dump_5(x1, x2, x3, x4, x5)                                             \\
  cerr << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#define dump(...)                                                              \\
  CHOOSE((__VA_ARGS__, dump_5, dump_4, dump_3, dump_2, dump_1, ~))(__VA_ARGS__)
#define check(s) cerr << s << endl

#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end());
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

using namespace std;

using ll = long long;

vector<int> dx = {0, 1, 0, -1};
vector<int> dy = {1, 0, -1, 0};

const ll LINF = 2e18;
const int INF = 1e9;

int H, W;
int Ch, Cw;
int Dh, Dw;
vector<vector<char>> S;

struct point {
  int y, x;

  bool operator==(const point &rhs) const { return (x == rhs.x && y == rhs.y); }
  bool operator<(const point &rhs) const {
    return (x < rhs.x || (x == rhs.x && y < rhs.y));
  }
};
typedef struct point Point;

void solve() {
  vector<vector<bool>> dist(H, vector<bool>(W, false));
  priority_queue<pair<int, Point>, vector<pair<int, Point>>,
                 greater<pair<int, Point>>>
      q;

  q.push({0, {Ch, Cw}});
  ll result = -1;

  while (!q.empty()) {
    auto v = q.top();
    q.pop();
    int w = v.first; // ワープカウント
    Point p = v.second;

    dist.at(p.y).at(p.x) = true;

    if (p.y == Dh && p.x == Dw) {
      result = w;
      break;
    }

    // Move
    rep(i, 4) {
      int nx = p.x + dx[i];
      int ny = p.y + dy[i];
      if (nx < 0 || ny < 0 || nx >= W || ny >= H)
        continue;
      if (S.at(ny).at(nx) == '#')
        continue;
      if (!dist.at(ny).at(nx)) {
        q.push({w, {ny, nx}});
      }
    }

    // Warp
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int nx = p.x + i;
        int ny = p.y + j;
        if (nx < 0 || ny < 0 || nx >= W || ny >= H)
          continue;
        if (S.at(ny).at(nx) == '#')
          continue;
        if (!dist.at(ny).at(nx)) {
          q.push({w + 1, {ny, nx}});
        }
      }
    }
  }

  cout << result << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);

  cin >> H >> W;
  cin >> Ch >> Cw;
  cin >> Dh >> Dw;

  // 0-indexed
  Ch--;
  Cw--;
  Dh--;
  Dw--;

  S.resize(H, vector<char>(W));

  rep(i, H) {
    rep(j, W) {
      char c;
      cin >> c;
      S.at(i).at(j) = c;
    }
  }

  solve();

  return 0;
}