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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (ll i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1LL << 60;
const double pi = acos(-1.0);

using tp = tuple<int, int, int>;

int main() {
  int H, W, ch, cw, dh, dw;
  cin >> H >> W >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vector<vector<char>> g(H, vector<char>(W));
  rep(i, H) rep(j, W) cin >> g[i][j];
  vector<vector<int>> seen(H, vector<int>(W, -1));
  priority_queue<tp, vector<tp>, greater<tp>> q;
  q.push(make_tuple(0, ch, cw));
  while (q.size()) {
    auto now = q.top();
    q.pop();
    int nowc = get<0>(now), nowh = get<1>(now), noww = get<2>(now);
    if (seen[nowh][noww] != -1)
      continue;
    seen[nowh][noww] = nowc;
    Rep(i, -2, 3) {
      Rep(j, -2, 3) {
        int nc = nowc;
        int nh = nowh + i;
        int nw = noww + j;
        if (nh < 0 || H <= nh || nw < 0 || W <= nw)
          continue;
        if (seen[nh][nw] != -1)
          continue;
        if (g[nh][nw] == '#')
          continue;
        if (abs(i) + abs(j) > 1)
          nc++;
        q.push(make_tuple(nc, nh, nw));
      }
    }
  }
  // rep(i, H) {rep(j, W) cout << seen[i][j]; cout << endl;}
  cout << seen[dh][dw] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define Rep(i, j, n) for (ll i = j; i < n; i++)
#define all(vec) vec.begin(), vec.end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const ll INF = 1LL << 60;
const double pi = acos(-1.0);

using tp = tuple<int, int, int>;

int main() {
  int H, W, ch, cw, dh, dw;
  cin >> H >> W >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vector<vector<char>> g(H, vector<char>(W));
  rep(i, H) rep(j, W) cin >> g[i][j];
  vector<vector<int>> seen(H, vector<int>(W, -1));
  priority_queue<tp, vector<tp>, greater<tp>> q;
  q.push(make_tuple(0, ch, cw));
  while (q.size()) {
    auto now = q.top();
    q.pop();
    int nowc = get<0>(now), nowh = get<1>(now), noww = get<2>(now);
    if (seen[nowh][noww] == -1)
      seen[nowh][noww] = nowc;
    Rep(i, -2, 3) {
      Rep(j, -2, 3) {
        int nc = nowc;
        int nh = nowh + i;
        int nw = noww + j;
        if (nh < 0 || H <= nh || nw < 0 || W <= nw)
          continue;
        if (seen[nh][nw] != -1)
          continue;
        if (g[nh][nw] == '#')
          continue;
        if (abs(i) + abs(j) > 1)
          nc++;
        q.push(make_tuple(nc, nh, nw));
      }
    }
  }
  // rep(i, H) {rep(j, W) cout << seen[i][j]; cout << endl;}
  cout << seen[dh][dw] << endl;
}