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

#include "bits/stdc++.h"
using namespace std;
using ll = long long;

template <typename T>
void print_vec(const vector<T> &v, bool is_reverse = false, int num = 0) {
  if (num == 0)
    num = (int)v.size();
  if (is_reverse)
    for (int i = num - 1; i >= 0; i--) {
      cout << v[i];
      if (i != 0)
        cout << " ";
    }
  else
    for (int i = 0; i < num; i++) {
      cout << v[i];
      if (i != num - 1)
        cout << " ";
    }
  cout << endl;
}

template <typename T> void print_pairvec(const vector<T> &v, int num = 0) {
  if (num == 0)
    num = (int)v.size();
  cout << endl;
  for (int i = 0; i < num; i++) {
    cout << v[i].first << " " << v[i].second << endl;
  }
}

template <typename T> void print_vec2(const vector<vector<T>> &v) {
  cout << endl;
  cout << "       ";
  for (int i = 0; i < v[0].size(); i++)
    cout << i << "   ";
  cout << endl;
  for (int i = 0; i < v.size(); i++) {
    cout << "i=" << i << ":   ";
    for (int j = 0; j < v[i].size(); j++) {
      if (v[i][j] == 0)
        cout << "\\x1B[0m" << v[i][j] << "   ";
      else
        cout << "\\x1B[31m" << v[i][j]
             << "   "; // https://stackoverrun.com/ja/q/12618775
    }
    cout << "\\x1B[0m" << endl;
  }
}

using P = pair<int, int>;
using LP = pair<ll, P>;
int main() {
  int H, W;
  cin >> H >> W;

  int sh, sw, gh, gw;
  cin >> sh >> sw >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;

  vector<string> maze(H);
  for (int i = 0; i < H; i++)
    cin >> maze[i];

  const int ADD = 10;
  const int INF = 1e8 + 1;
  vector<vector<ll>> field(H + ADD, vector<ll>(W + ADD, INF));
  field[sh][sw] = 0;

  int dh[] = {-1, 0, 1, 0};
  int dw[] = {0, -1, 0, 1};

  // queue<P> qu;
  // qu.push(P(sh, sw));
  priority_queue<LP, vector<LP>, greater<LP>> pri_qu;
  pri_qu.push(LP(0, P(sh, sw)));

  // while(!qu.empty()){
  while (!pri_qu.empty()) {
    // int h = qu.front().first;
    // int w = qu.front().second;
    // qu.pop();
    ll cost = pri_qu.top().first;
    int h = pri_qu.top().second.first;
    int w = pri_qu.top().second.second;
    pri_qu.pop();

    for (int i = 0; i < 4; i++) {
      int nh = h + dh[i];
      int nw = w + dw[i];
      // cout << "  cnt : " << cnt  << "(h, w)= (" << h << ", " << w  << ") (nh,
      // nw) = (" << nh << ", " << nw << ")"<< endl;
      if (nh < 0 || H <= nh)
        continue;
      if (nw < 0 || W <= nw)
        continue;
      if (maze[nh][nw] == '#')
        continue;
      // if(field[nh][nw] <= field[h][w]) continue;
      if (field[nh][nw] <= cost)
        continue;
      field[nh][nw] = field[h][w];
      // cout << "  hhh " << endl;

      // qu.push(P(nh, nw));
      pri_qu.push(LP(cost, P(nh, nw)));
    }

    for (int _h = -2; _h <= 2; _h++) {
      for (int _w = -2; _w <= 2; _w++) {
        int nh = h + _h;
        int nw = w + _w;
        if (nh < 0 || H <= nh)
          continue;
        if (nw < 0 || W <= nw)
          continue;
        if (maze[nh][nw] == '#')
          continue;
        // if(field[nh][nw] <= field[h][w]+1) continue;
        if (field[nh][nw] <= cost + 1)
          continue;
        field[nh][nw] = field[h][w] + 1;

        // qu.push(P(nh, nw));
        pri_qu.push(LP(cost + 1, P(nh, nw)));
      }
    }
  }

  if (field[gh][gw] != INF)
    cout << field[gh][gw] << endl;
  else
    cout << "-1" << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
using ll = long long;

template <typename T>
void print_vec(const vector<T> &v, bool is_reverse = false, int num = 0) {
  if (num == 0)
    num = (int)v.size();
  if (is_reverse)
    for (int i = num - 1; i >= 0; i--) {
      cout << v[i];
      if (i != 0)
        cout << " ";
    }
  else
    for (int i = 0; i < num; i++) {
      cout << v[i];
      if (i != num - 1)
        cout << " ";
    }
  cout << endl;
}

template <typename T> void print_pairvec(const vector<T> &v, int num = 0) {
  if (num == 0)
    num = (int)v.size();
  cout << endl;
  for (int i = 0; i < num; i++) {
    cout << v[i].first << " " << v[i].second << endl;
  }
}

template <typename T> void print_vec2(const vector<vector<T>> &v) {
  cout << endl;
  cout << "       ";
  for (int i = 0; i < v[0].size(); i++)
    cout << i << "   ";
  cout << endl;
  for (int i = 0; i < v.size(); i++) {
    cout << "i=" << i << ":   ";
    for (int j = 0; j < v[i].size(); j++) {
      if (v[i][j] == 0)
        cout << "\\x1B[0m" << v[i][j] << "   ";
      else
        cout << "\\x1B[31m" << v[i][j]
             << "   "; // https://stackoverrun.com/ja/q/12618775
    }
    cout << "\\x1B[0m" << endl;
  }
}

using P = pair<int, int>;
using LP = pair<ll, P>;
int main() {
  int H, W;
  cin >> H >> W;

  int sh, sw, gh, gw;
  cin >> sh >> sw >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;

  vector<string> maze(H);
  for (int i = 0; i < H; i++)
    cin >> maze[i];

  const int ADD = 10;
  const int INF = 1e8 + 1;
  vector<vector<ll>> field(H + ADD, vector<ll>(W + ADD, INF));
  field[sh][sw] = 0;

  int dh[] = {-1, 0, 1, 0};
  int dw[] = {0, -1, 0, 1};

  // queue<P> qu;
  // qu.push(P(sh, sw));
  priority_queue<LP, vector<LP>, greater<LP>> pri_qu;
  pri_qu.push(LP(0, P(sh, sw)));

  // while(!qu.empty()){
  while (!pri_qu.empty()) {
    // int h = qu.front().first;
    // int w = qu.front().second;
    // qu.pop();
    ll cost = pri_qu.top().first;
    int h = pri_qu.top().second.first;
    int w = pri_qu.top().second.second;
    pri_qu.pop();

    for (int i = 0; i < 4; i++) {
      int nh = h + dh[i];
      int nw = w + dw[i];
      // cout << "  cnt : " << cnt  << "(h, w)= (" << h << ", " << w  << ") (nh,
      // nw) = (" << nh << ", " << nw << ")"<< endl;
      if (nh < 0 || H <= nh)
        continue;
      if (nw < 0 || W <= nw)
        continue;
      if (maze[nh][nw] == '#')
        continue;
      if (field[nh][nw] <= field[h][w])
        continue;
      field[nh][nw] = field[h][w];
      // cout << "  hhh " << endl;

      // qu.push(P(nh, nw));
      pri_qu.push(LP(cost, P(nh, nw)));
    }

    for (int _h = -2; _h <= 2; _h++) {
      for (int _w = -2; _w <= 2; _w++) {
        int nh = h + _h;
        int nw = w + _w;
        if (nh < 0 || H <= nh)
          continue;
        if (nw < 0 || W <= nw)
          continue;
        if (maze[nh][nw] == '#')
          continue;
        if (field[nh][nw] <= field[h][w])
          continue;
        field[nh][nw] = field[h][w] + 1;

        // qu.push(P(nh, nw));
        pri_qu.push(LP(cost + 1, P(nh, nw)));
      }
    }
  }

  if (field[gh][gw] != INF)
    cout << field[gh][gw] << endl;
  else
    cout << "-1" << endl;
  return 0;
}