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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define P pair<ll, ll>
#define Graph vector<vector<ll>>
#define fi first
#define se second
constexpr ll mod = 1000000007;
constexpr ll INF = (1ll << 60);
constexpr double pi = 3.14159265358979323846;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll h, w;
  cin >> h >> w;
  ll sy, sx, gy, gx;
  cin >> sy >> sx >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;
  vector<vector<char>> s(h, vector<char>(w));
  rep(i, h) rep(j, w) cin >> s[i][j];

  deque<P> que;
  que.push_front({sy, sx});
  vector<vector<ll>> dist(h, vector<ll>(w, INF));
  dist[sy][sx] = 0;

  while (que.size()) {
    P now = que.front();
    que.pop_front();

    // 隣接マスへの移動
    for (ll dy = -1; dy <= 1; dy++) {
      for (ll dx = -1; dx <= 1; dx++) {
        if (dy == 0 && dx == 0)
          continue;
        if (dy * dx != 0)
          continue;

        ll ny = now.fi + dy, nx = now.se + dx;
        if (!(0 <= ny && ny < h && 0 <= nx && nx < w))
          continue;
        if (s[ny][nx] == '#')
          continue;

        if (dist[ny][nx] > dist[now.fi][now.se]) {
          dist[ny][nx] = dist[now.fi][now.se];
          que.push_front({ny, nx});
        }
      }
    }

    // 魔法での移動
    for (ll dy = -2; dy <= 2; dy++) {
      for (ll dx = -2; dx <= 2; dx++) {
        if (dy == 0 && dx == 0)
          continue;

        ll ny = now.fi + dy, nx = now.se + dx;
        if (!(0 <= ny && ny < h && 0 <= nx && nx < w))
          continue;
        if (s[ny][nx] == '#')
          continue;

        if (dist[ny][nx] > dist[now.fi][now.se] + 1) {
          que.push_back({ny, nx});
          dist[ny][nx] = dist[now.fi][now.se] + 1;
        }
      }
    }
  }

  /*rep(i,h){
      rep(j,w){
          if(dist[i][j]!=INF) cout<<dist[i][j]<<" ";
          else cout<<-1<<" ";
      }
      cout<<endl;
  }*/

  if (dist[gy][gx] != INF)
    cout << dist[gy][gx] << endl;
  else
    cout << -1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define P pair<ll, ll>
#define Graph vector<vector<ll>>
#define fi first
#define se second
constexpr ll mod = 1000000007;
constexpr ll INF = (1ll << 60);
constexpr double pi = 3.14159265358979323846;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

/*maze_version*/
vector<vector<ll>> score;
vector<vector<char>> maze;
ll h, w;
ll dy[24] = {0, 0, 1, -1, 1, 1,  -1, -1, 1,  1, -1, -1,
             2, 2, 2, 2,  2, -2, -2, -2, -2, 0, 0};
ll dx[24] = {1, -1, 0,  0, 1,  -1, 1, -1, 2, -2, 2, -2,
             0, 1,  -1, 2, -2, 0,  1, -1, 2, -2, 2, -2};

int main() {
  cin >> h >> w;
  ll ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;

  maze.assign(h, vector<char>(w));
  score.assign(h, vector<ll>(w, INF));
  rep(i, h) rep(j, w) cin >> maze[i][j];

  queue<P> que;
  que.push({ch, cw});
  vector<vector<bool>> visit(h, vector<bool>(w, false));

  score[ch][cw] = 0;
  while (que.size()) {
    P now = que.front();
    que.pop();
    visit[now.fi][now.se] = true;

    bool can_move = false;
    rep(i, 4) {
      ll ny = now.fi + dy[i], nx = now.se + dx[i];
      if (ny < 0 || h <= ny || nx < 0 || w <= nx)
        continue;
      if (visit[ny][nx] == true)
        continue;
      if (maze[ny][nx] == '#')
        continue;
      visit[ny][nx] = true;

      chmin(score[ny][nx], score[now.fi][now.se]);
      que.push({ny, nx});
      can_move = true;
    }

    if (can_move == false) {
      rep(i, 24) {
        ll ny = now.fi + dy[i], nx = now.se + dx[i];
        if (ny < 0 || h <= ny || nx < 0 || w <= nx)
          continue;
        if (visit[ny][nx] == true)
          continue;
        if (maze[ny][nx] == '#')
          continue;

        chmin(score[ny][nx], score[now.fi][now.se] + 1);
        que.push({ny, nx});
      }
    }
  }

  /*rep(i, h)
  {
      rep(j, w)
      {
          if (score[i][j] != INF)
              cout << score[i][j] << " ";
          else
              cout << -1 << " ";
      }
      cout << endl;
  }*/

  if (score[dh][dw] != INF)
    cout << score[dh][dw] << endl;
  else
    cout << -1 << endl;
  return 0;
}