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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(var)                                                             \\
  do {                                                                         \\
    cout << #var << " : ";                                                     \\
    view(var);                                                                 \\
  } while (0)
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
template <class T> void view(T e) { cout << e << endl; }
template <class T> void view(const vector<T> &v) {
  for (const auto &e : v) {
    cout << e << " ";
  }
  cout << endl;
}
template <class T> void view(const vector<vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

// dijkstra
template <class T> struct dijkstra {
  struct edge {
    int to, cost;
  };
  vector<T> d;
  vector<vector<edge>> G;

  dijkstra(int n) { init(n); }

  void init(int n) {
    G.resize(n);
    d.resize(n);
    rep(i, n) d[i] = inf;
  }

  void add_edge(int s, int t, int cost) {
    edge e;
    e.to = t;
    e.cost = cost;
    G[s].push_back(e);
  }

  void run(int s) {
    priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q;
    d[s] = 0;
    q.push(pair<T, int>(0, s));
    while (!q.empty()) {
      pair<T, int> e = q.top();
      q.pop();
      int v = e.second;
      if (d[v] < e.first)
        continue;
      for (auto e : G[v]) {
        if (chmin(d[e.to], d[v] + e.cost)) {
          q.push(pair<T, int>(d[e.to], e.to));
        }
      }
    }
  }
};

int main() {
  int H, W;
  cin >> H >> W;
  int c_h, c_w;
  cin >> c_h >> c_w;
  c_h--;
  c_w--;
  int d_h, d_w;
  cin >> d_h >> d_w;
  d_h--;
  d_w--;
  vector<string> field(H);
  rep(i, H) cin >> field[i];
  dijkstra<int> dj(H * W);
  rep(i, H) rep(j, W) {
    for (int h = -2; h <= 2; h++) {
      for (int w = -2; w <= 2; w++) {
        if (h == 0 && w == 0)
          continue;
        int n_i = i + h;
        int n_j = j + w;
        if (n_i >= 0 && n_i < H && n_j >= 0 && n_j < W) {
          if (field[n_i][n_j] == '#')
            continue;
          if (abs(h) + abs(w) == 1) {
            dj.add_edge(W * i + j, W * n_i + n_j, 0);
          } else {
            dj.add_edge(W * i + j, W * n_i + n_j, 1);
          }
        }
      }
    }
  }
  dj.run(W * c_h + c_w);
  int ans = dj.d[W * d_h + d_w];
  if (ans != inf)
    cout << ans << endl;
  else
    cout << -1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i < (int)(n); i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(var)                                                             \\
  do {                                                                         \\
    cout << #var << " : ";                                                     \\
    view(var);                                                                 \\
  } while (0)
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;
template <class T> void view(T e) { cout << e << endl; }
template <class T> void view(const vector<T> &v) {
  for (const auto &e : v) {
    cout << e << " ";
  }
  cout << endl;
}
template <class T> void view(const vector<vector<T>> &vv) {
  for (const auto &v : vv) {
    view(v);
  }
}
using vint = vector<int>;
using vvint = vector<vector<int>>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<int, int>;
const int inf = 1e9;
const ll inf_l = 1e18;
const int MAX = 1e5;

int h, w;
int dh, dw;
vector<string> field;
int dis[1005][1005];
vint dx = {1, 0, -1, 0};
vint dy = {0, 1, 0, -1};
vector<vector<bool>> used;

void dfs(int sh, int sw) {
  if (sh == dh && sw == dw)
    return;
  used[sh][sw] = true;
  rep(i, 4) {
    int n_y = sh + dy[i];
    int n_x = sw + dx[i];
    if (n_y >= 0 && n_y < h && n_x >= 0 && n_x < w && field[n_y][n_x] != '#') {
      if (dis[n_y][n_x] != dis[sh][sw]) {
        dis[n_y][n_x] = dis[sh][sw];
        dfs(n_y, n_x);
      }
    }
  }
}

bool update(int i, int j, int v) {
  bool res = false;
  for (int k = -2; k <= 2; k++) {
    for (int l = -2; l <= 2; l++) {
      int n_i = i + k;
      int n_j = j + l;
      if (n_i >= 0 && n_i < h && n_j >= 0 && n_j < w &&
          field[n_i][n_j] != '#') {
        if (chmin(dis[n_i][n_j], v)) {
          res = true;
          dfs(n_i, n_j);
        }
      }
    }
  }
  return res;
}

int main() {
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  field.resize(h);
  used.assign(h, vector<bool>(w, false));
  rep(i, h) cin >> field[i];
  rep(i, 1005) rep(j, 1005) dis[i][j] = inf;
  dis[ch][cw] = 0;
  dfs(ch, cw);
  int now = 0;
  int ct = 10000;
  while (ct--) {
    if (dis[dh][dw] != inf) {
      cout << dis[dh][dw] << endl;
      return 0;
    }
    bool ok = false;
    rep(i, h) rep(j, w) {
      if (dis[i][j] == now) {
        ok |= update(i, j, now + 1);
      }
    }
    if (!ok) {
      cout << -1 << endl;
      return 0;
    }
    now++;
  }
  cout << -1 << endl;
}