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

#include <bits/stdc++.h>
#include <math.h>

using namespace std;

template <typename T> long long modpow(const T n, const T p, const T mod);
template <typename T> long long modinv(const T n, const T mod);
template <typename T> bool chmax(T &a, const T &b);
template <typename T> bool chmin(T &a, const T &b);

long long inf = 1000000007;

int main() {
  long long h, w;
  cin >> h >> w;
  long long sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  vector<string> grid(h);
  for (long long i = 0; i < h; i++) {
    cin >> grid.at(i);
  }
  sx--;
  sy--;
  gx--;
  gy--;
  vector<vector<long long>> dis(h, vector<long long>(w, inf));
  dis.at(sy).at(sx) = 0;
  priority_queue<pair<long long, pair<long long, long long>>,
                 vector<pair<long long, pair<long long, long long>>>,
                 greater<pair<long long, pair<long long, long long>>>>
      que;
  que.push(make_pair(0, make_pair(sy, sx)));

  while (!que.empty()) {
    long long ny = que.top().second.first, nx = que.top().second.second;
    long long nowdis = que.top().first;
    que.pop();

    if (dis.at(ny).at(nx) != nowdis)
      continue;

    vector<long long> dy = {1, 0, -1, 0}, dx = {0, 1, 0, -1};
    for (long long i = 0; i < 4; i++) {
      long long py = ny + dy.at(i);
      long long px = nx + dx.at(i);
      if (py < 0 || py >= h || px < 0 || px >= w || grid.at(py).at(px) == '#' ||
          dis.at(py).at(px) <= nowdis)
        continue;

      dis.at(py).at(px) = dis.at(ny).at(nx);
      que.push(make_pair(nowdis, make_pair(py, px)));
    }

    for (long long i = -2; i <= 2; i++) {
      for (long long j = -2; j <= 2; j++) {
        long long py = ny + i;
        long long px = nx + j;
        if (py < 0 || py >= h || px < 0 || px >= w ||
            grid.at(py).at(px) == '#' || dis.at(py).at(px) <= nowdis + 1)
          continue;

        dis.at(py).at(px) = dis.at(ny).at(nx) + 1;
        que.push(make_pair(nowdis + 1, make_pair(py, px)));
      }
    }
  }

  if (dis.at(gy).at(gx) == inf)
    cout << -1 << endl;
  else
    cout << dis.at(gy).at(gx) << endl;

  return 0;
}

template <typename T> long long modpow(const T n, const T p, const T mod) {
  if (p == 0)
    return 1;
  if (p % 2 == 0) {
    long long a = modpow(n, p / 2, mod);
    return a * a % mod;
  }
  if (p % 2 == 1)
    return (modpow(n, p - 1, mod) * n) % mod;
  cerr << "ERROR" << endl;
  return 1;
}

template <typename T> long long modinv(const T n, const T mod) {
  return modpow(n, mod - 2, mod);
}

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

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>

using namespace std;

template <typename T> long long modpow(const T n, const T p, const T mod);
template <typename T> long long modinv(const T n, const T mod);
template <typename T> bool chmax(T &a, const T &b);
template <typename T> bool chmin(T &a, const T &b);

long long inf = 1000000007;

int main() {
  long long h, w;
  cin >> h >> w;
  long long sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  vector<string> grid(h);
  for (long long i = 0; i < h; i++) {
    cin >> grid.at(i);
  }
  sx--;
  sy--;
  gx--;
  gy--;
  vector<vector<long long>> dis(h, vector<long long>(w, inf));
  dis.at(sy).at(sx) = 0;
  queue<pair<long long, long long>> que;
  que.push(make_pair(sy, sx));

  while (!que.empty()) {
    long long ny = que.front().first;
    long long nx = que.front().second;
    que.pop();

    if (grid.at(ny).at(nx) == '#')
      continue;

    vector<long long> dy = {1, 0, -1, 0}, dx = {0, 1, 0, -1};
    for (long long i = 0; i < 4; i++) {
      long long py = ny + dy.at(i);
      long long px = nx + dx.at(i);
      if (py < 0 || py >= h || px < 0 || px >= w || grid.at(py).at(px) == '#' ||
          dis.at(py).at(px) <= dis.at(ny).at(nx))
        continue;

      dis.at(py).at(px) = dis.at(ny).at(nx);
      que.push(make_pair(py, px));
    }

    for (long long i = -2; i <= 2; i++) {
      for (long long j = -2; j <= 2; j++) {
        long long py = ny + i;
        long long px = nx + j;
        if (py < 0 || py >= h || px < 0 || px >= w ||
            grid.at(py).at(px) == '#' ||
            dis.at(py).at(px) <= dis.at(ny).at(nx) + 1)
          continue;

        dis.at(py).at(px) = dis.at(ny).at(nx) + 1;
        que.push(make_pair(py, px));
      }
    }
  }

  long long res = dis.at(gy).at(gx);
  if (res == inf)
    cout << -1 << endl;
  else
    cout << res << endl;

  //	for(long long i=0;i<h;i++){
  //		for(long long j=0;j<w;j++){
  //			cout<<dis.at(i).at(j);
  //		}cout<<endl;
  //	}cout<<gy<<" "<<gx<<endl;

  return 0;
}

template <typename T> long long modpow(const T n, const T p, const T mod) {
  if (p == 0)
    return 1;
  if (p % 2 == 0) {
    long long a = modpow(n, p / 2, mod);
    return a * a % mod;
  }
  if (p % 2 == 1)
    return (modpow(n, p - 1, mod) * n) % mod;
  cerr << "ERROR" << endl;
  return 1;
}

template <typename T> long long modinv(const T n, const T mod) {
  return modpow(n, mod - 2, mod);
}

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

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