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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define rep(i, n) REP(i, 0, n)
#define rep_rev(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define all(x) (x).begin(), (x).end()
ll t1, t2, t3;
const ll mod = 1000000007;
const int INF = 1e9;
const ll INFLONG = 1e18;

struct Info {
  ll x;
  ll y;
  ll score;
  Info() {}
  Info(ll x, ll y, ll score) : x(x), y(y), score(score) {}
};

ll dx[] = {1, 0, -1, 0};
ll dy[] = {0, -1, 0, 1};

int main() {
  ll h, w;
  cin >> h >> w;
  vector<vector<char>> map(h, vector<char>(w));
  vector<vector<ll>> vec(h, vector<ll>(w, INFLONG));
  ll cx, cy, gx, gy;
  cin >> cx >> cy;
  cx--, cy--;
  cin >> gx >> gy;
  gx--, gy--;
  rep(i, h) {
    rep(j, w) { cin >> map[i][j]; }
  }
  deque<Info> q;
  Info info;
  info.x = cx;
  info.y = cy;
  info.score = 0;
  q.push_front(info);
  ll x, y, score;
  ll tx, ty;
  while (!q.empty()) {
    Info info = q.front();
    q.pop_front();
    x = info.x;
    y = info.y;
    score = info.score;
    if (0 <= x && x < h && 0 <= y && y < w && map[x][y] == '.') {
      if (score < vec[x][y]) {
        vec[x][y] = score;
        rep(i, 4) {
          tx = x + dx[i];
          ty = y + dy[i];
          Info info(tx, ty, score);
          q.push_front(info);
        }
        rep(i, 5) {
          rep(j, 5) {
            tx = x + i - 2;
            ty = y + j - 2;
            Info info(tx, ty, score + 1);
            q.push_back(info);
          }
        }
      }
    }
  }
  // rep(i,h){
  //   rep(j,w){
  //     cout << vec[i][j] << " ";
  //   }
  //   cout << endl;
  // }
  if (vec[gx][gy] == INFLONG) {
    vec[gx][gy] = -1;
  }
  cout << vec[gx][gy] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define rep(i, n) REP(i, 0, n)
#define rep_rev(i, n) for (int(i) = (int)(n)-1; (i) >= 0; --(i))
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define all(x) (x).begin(), (x).end()
ll t1, t2, t3;
const ll mod = 1000000007;
const int INF = 1e9;
const ll INFLONG = 1e18;

struct Info {
  ll x;
  ll y;
  ll score;
  Info() {}
  Info(ll x, ll y, ll score) : x(x), y(y), score(score) {}
};

ll dx[] = {1, 0, -1, 0};
ll dy[] = {0, -1, 0, 1};

int main() {
  ll h, w;
  cin >> h >> w;
  vector<vector<char>> map(h, vector<char>(w));
  vector<vector<ll>> vec(h, vector<ll>(w, INFLONG));
  ll cx, cy, gx, gy;
  cin >> cx >> cy;
  cx--, cy--;
  cin >> gx >> gy;
  gx--, gy--;
  rep(i, h) {
    rep(j, w) { cin >> map[i][j]; }
  }
  queue<Info> q;
  Info info;
  info.x = cx;
  info.y = cy;
  info.score = 0;
  q.push(info);
  ll x, y, score;
  ll tx, ty;
  while (!q.empty()) {
    Info info = q.front();
    q.pop();
    x = info.x;
    y = info.y;
    score = info.score;
    if (0 <= x && x < h && 0 <= y && y < w && map[x][y] == '.') {
      if (score < vec[x][y]) {
        vec[x][y] = score;
        rep(i, 4) {
          tx = x + dx[i];
          ty = y + dy[i];
          Info info(tx, ty, score);
          q.push(info);
        }
        rep(i, 5) {
          rep(j, 5) {
            tx = x + i - 2;
            ty = y + j - 2;
            Info info(tx, ty, score + 1);
            q.push(info);
          }
        }
      }
    }
  }
  // rep(i,h){
  //   rep(j,w){
  //     cout << vec[i][j] << " ";
  //   }
  //   cout << endl;
  // }
  if (vec[gx][gy] == INFLONG) {
    vec[gx][gy] = -1;
  }
  cout << vec[gx][gy] << endl;
}