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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = (0); i < (n); ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define DEBUG(val) cout << #val << " : " << val << endl
#define MOD 1000000007
#define vi vector<int>
#define vvi vector<vi>

// ref https://atcoder.jp/contests/abc176/submissions/16160744
int walkable(int now_h, int now_w, int max_h, int max_w, int goal_h, int goal_w,
             vector<string> &data, vector<vector<int>> &dist) {
  char forwardable = '.';
  priority_queue<vi, vvi, greater<vi>> q;
  vector<vector<bool>> visited(max_h, vector<bool>(max_w, false));
  // start
  int cnt = 0;
  q.push({0, now_h, now_w});
  dist[now_h][now_w] = 0;
  while (!q.empty()) {
    int nd = q.top().at(0);
    int nh = q.top().at(1);
    int nw = q.top().at(2);
    q.pop();

    FOR(i, -2, 3) {
      FOR(j, -2, 3) {
        int next_h = nh + i;
        int next_w = nw + j;
        int cost = (abs(i) + abs(j) > 1) ? 1 : 0;
        if (next_h < 0 || next_h >= max_h)
          continue;
        if (next_w < 0 || next_w >= max_w)
          continue;
        if (data[next_h][next_w] != forwardable)
          continue;
        if (dist[next_h][next_w] <= nd)
          continue;
        if (dist[next_h][next_w] > dist[nh][nw] + cost) {
          dist[next_h][next_w] = dist[nh][nw] + cost;
          q.push({nd + cost, next_h, next_w});

          /*
          REP(l, max_h) {
            REP(m, max_w) { cout << dist[l][m] << " "; }
            cout << endl;
          }
          cout << endl;
          */
        }
      }
    }
  }
  return dist[goal_h][goal_w];
}

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> data(h);
  REP(i, h) { cin >> data[i]; }
  vector<vector<int>> dist(h, vector<int>(w, INT_MAX));
  int ans = walkable(ch, cw, h, w, dh, dw, data, dist);

  if (ans != INT_MAX) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = (0); i < (n); ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define DEBUG(val) cout << #val << " : " << val << endl
#define MOD 1000000007

// ref https://atcoder.jp/contests/abc176/submissions/16160744
int walkable(int now_h, int now_w, int max_h, int max_w, int goal_h, int goal_w,
             vector<string> &data, vector<vector<int>> &dist) {
  char forwardable = '.';
  deque<pair<int, int>> q;

  // start
  q.push_front(make_pair(now_h, now_w));
  dist[now_h][now_w] = 0;

  while (!q.empty()) {
    pair<int, int> p = q.front();
    q.pop_front();

    FOR(i, -2, 3) {
      FOR(j, -2, 3) {
        int next_h = p.first + i;
        int next_w = p.second + j;
        int cost = (abs(i) + abs(j) > 1) ? 1 : 0;
        if (next_h < 0 || next_h >= max_h)
          continue;
        if (next_w < 0 || next_w >= max_w)
          continue;
        if (data[next_h][next_w] != forwardable)
          continue;
        if (dist[next_h][next_w] > dist[p.first][p.second] + cost) {
          dist[next_h][next_w] = (dist[p.first][p.second] + cost);
          q.push_back(make_pair(next_h, next_w));
        }
      }
    }
  }
  return dist[goal_h][goal_w];
}

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w;
  cin >> ch >> cw;
  cin >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> data(h);
  REP(i, h) { cin >> data[i]; }
  vector<vector<int>> dist(h, vector<int>(w, INT_MAX));
  int ans = walkable(ch, cw, h, w, dh, dw, data, dist);

  if (ans != INT_MAX) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}