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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using ll = long long;
const long long INF = 1LL << 60;
const int MOD = 1e9 + 7;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

/* ACL */

// using namespace atcoder;

#define overX INF / 2
#define X overX - 1

int main() {
  int H, W;
  cin >> H >> W;
  int Ch, Cw;
  cin >> Ch >> Cw;
  int Dh, Dw;
  cin >> Dh >> Dw;

  vector<vector<ll>> maze(H, vector<ll>(W, X));
  vector<string> maze_string(H);

  rep(i, H) { cin >> maze_string.at(i); }

  queue<pair<int, int>> que;
  queue<pair<int, int>> highest_cost_area;

  que.push({Ch, Cw});
  // maze.at(Ch-1).at(Cw-1) = 0;

  pair<int, int> now;
  int cost = 0;
#define COST(i, j) maze.at(now.first - 1 + i).at(now.second - 1 + j)
#define MAZE(i, j) maze_string.at(now.first + i - 1)[now.second + j - 1]
  while (que.size() > 0) {
    while (que.size() > 0) {
      now = que.front();
      que.pop();

      COST(0, 0) = cost;
      highest_cost_area.push({now.first, now.second});

      if (now.first != 1 && MAZE(-1, 0) != '#' && COST(-1, 0) != cost) {
        COST(-1, 0) = cost;
        que.push({now.first - 1, now.second});
      }
      if (now.first != H && MAZE(1, 0) != '#' && COST(1, 0) != cost) {
        COST(1, 0) = cost;
        que.push({now.first + 1, now.second});
      }
      if (now.second != 1 && MAZE(0, -1) != '#' && COST(0, -1) != cost) {
        COST(0, -1) = cost;
        que.push({now.first, now.second - 1});
      }
      if (now.second != W && MAZE(0, 1) != '#' && COST(0, 1) != cost) {
        COST(0, 1) = cost;
        que.push({now.first, now.second + 1});
      }
    }

    while (highest_cost_area.size() > 0) {
      now = highest_cost_area.front();
      highest_cost_area.pop();

      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          if (1 <= now.first + i && now.first + i <= H && 1 <= now.second + j &&
              now.second + j <= W && MAZE(i, j) != '#' && COST(i, j) == X) {
            que.push({now.first + i, now.second + j});
            COST(i, j) = cost + 1;
          }
        }
      }
    }
    cost++;
  }

  if (maze.at(Dh - 1).at(Dw - 1) != X) {
    cout << maze.at(Dh - 1).at(Dw - 1) << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using ll = long long;
const long long INF = 1LL << 60;
const int MOD = 1e9 + 7;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

/* ACL */

// using namespace atcoder;

#define overX INF / 2
#define X overX - 1

int main() {
  int H, W;
  cin >> H >> W;
  int Ch, Cw;
  cin >> Ch >> Cw;
  int Dh, Dw;
  cin >> Dh >> Dw;

  vector<vector<ll>> maze(H, vector<ll>(W, X));
  vector<string> maze_string(H);

  rep(i, H) { cin >> maze_string.at(i); }

  queue<pair<int, int>> que;
  queue<pair<int, int>> highest_cost_area;

  que.push({Ch, Cw});
  // maze.at(Ch-1).at(Cw-1) = 0;

  pair<int, int> now;
  int cost = 0;
#define COST(i, j) maze.at(now.first - 1 + i).at(now.second - 1 + j)
#define MAZE(i, j) maze_string.at(now.first + i - 1)[now.second + j - 1]
  while (que.size() > 0) {
    while (que.size() > 0) {
      now = que.front();
      que.pop();

      COST(0, 0) = cost;
      highest_cost_area.push({now.first, now.second});

      if (now.first != 1 && MAZE(-1, 0) != '#' && COST(-1, 0) != cost) {
        que.push({now.first - 1, now.second});
      }
      if (now.first != H && MAZE(1, 0) != '#' && COST(1, 0) != cost) {
        que.push({now.first + 1, now.second});
      }
      if (now.second != 1 && MAZE(0, -1) != '#' && COST(0, -1) != cost) {
        que.push({now.first, now.second - 1});
      }
      if (now.second != W && MAZE(0, 1) != '#' && COST(0, 1) != cost) {
        que.push({now.first, now.second + 1});
      }
    }

    while (highest_cost_area.size() > 0) {
      now = highest_cost_area.front();
      highest_cost_area.pop();

      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          if (1 <= now.first + i && now.first + i <= H && 1 <= now.second + j &&
              now.second + j <= W && MAZE(i, j) != '#' && COST(i, j) > cost) {
            que.push({now.first + i, now.second + j});
            COST(i, j) = cost + 1;
          }
        }
      }
    }
    cost++;
  }

  if (maze.at(Dh - 1).at(Dw - 1) != X) {
    cout << maze.at(Dh - 1).at(Dw - 1) << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}