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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
const ll MOD = 1000000007;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//(a+b-1)/b
// priority_queue<ll, vector<ll>, greater<ll>> q;
using pl = pair<ll, ll>;
const ll INF = MOD * 100;
vl dx = {0, 1, 0, -1};
vl dy = {1, 0, -1, 0};

signed main() {
  ll H, W;
  cin >> H >> W;
  pl s, g;
  cin >> s.first >> s.second;
  s.first--;
  s.second--;
  cin >> g.first >> g.second;
  g.first--;
  g.second--;
  vector<vector<char>> table(H, vector<char>(W));
  rep(i, 0, H) rep(j, 0, W) cin >> table[i][j];
  vvl dist(H + 10, vl(W + 10, INF));
  dist[s.first][s.second] = 0;
  deque<pl> q;
  q.push_front(s);
  while (!q.empty()) {
    pl p = q.front();
    q.pop_front();
    ll a, b;
    tie(a, b) = p;
    ll d = dist[a][b];
    // 徒歩
    rep(i, 0, 4) {
      ll x = a + dx[i];
      ll y = b + dy[i];
      if (x < 0 || x >= H || y < 0 || y >= W)
        continue;
      if (table[x][y] == '#')
        continue;
      if (dist[x][y] <= d)
        continue;
      dist[x][y] = d;
      q.emplace_front(x, y);
    }

    // 魔法
    rep(i, max((ll)0, a - 2), min(H, a + 3)) {
      rep(j, max((ll)0, b - 2), min(W, b + 3)) {
        if (table[i][j] == '#')
          continue;
        if (dist[i][j] <= d + 1)
          continue;
        dist[i][j] = d + 1;
        q.emplace_back(i, j);
      }
    }
  }
  if (dist[g.first][g.second] == INF)
    cout << -1 << endl;
  else
    cout << dist[g.first][g.second] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
const ll MOD = 1000000007;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
//(a+b-1)/b
// priority_queue<ll, vector<ll>, greater<ll>> q;
using pl = pair<ll, ll>;
const ll INF = MOD * 100;
vl dx = {0, 1, 0, -1};
vl dy = {1, 0, -1, 0};

signed main() {
  ll H, W;
  cin >> H >> W;
  pl s, g;
  cin >> s.first >> s.second;
  s.first--;
  s.second--;
  cin >> g.first >> g.second;
  g.first--;
  g.second--;
  vector<vector<char>> table(H, vector<char>(W));
  rep(i, 0, H) rep(j, 0, W) cin >> table[i][j];
  vvl vec(H + 10, vl(W + 10, INF));
  vec[s.first][s.second] = 0;
  queue<pl> q;
  q.push(s);
  while (!q.empty()) {
    pl p = q.front();
    q.pop();
    ll a, b;
    tie(a, b) = p;
    rep(i, 0, 4) {
      ll x = a + dx[i];
      ll y = b + dy[i];
      if (x < 0 || x >= H || y < 0 || y >= W)
        continue;
      if (table[x][y] == '#')
        continue;
      if (vec[x][y] > vec[a][b]) {
        q.push(make_pair(x, y));
        chmin(vec[x][y], vec[a][b]);
      }
    }

    // 魔法
    rep(i, max((ll)0, a - 2), min(H, a + 3)) {
      rep(j, max((ll)0, b - 2), min(W, b + 3)) {
        if (table[i][j] == '#')
          continue;
        if (vec[i][j] > vec[a][b] + 1) {
          chmin(vec[i][j], vec[a][b] + 1);
          q.push(make_pair(i, j));
        }
      }
    }
  }
  if (vec[g.first][g.second] == INF)
    cout << -1 << endl;
  else
    cout << vec[g.first][g.second] << endl;

  return 0;
}