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

#include <bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int MOD = 1000000007;
const int INF = 1e12;
using Graph = vector<vector<int>>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

signed main() {
  int H, W;
  cin >> H >> W;
  int SH, SW, GH, GW;
  cin >> SH >> SW >> GH >> GW;
  SH--;
  SW--;
  GH--;
  GW--;

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

  vector<vector<int>> dist(H, vector<int>(W, INF));
  dist[SH][SW] = 0;
  deque<pair<int, pair<int, int>>> que;
  que.push_front(make_pair(0, make_pair(SH, SW)));

  while (!que.empty()) {
    int x = que.front().second.first;
    int y = que.front().second.second;
    que.pop_front();

    // 移動Aで行けるところ
    for (int direction = 0; direction < 4; ++direction) {
      int next_x = x + dx[direction];
      int next_y = y + dy[direction];
      if (next_x < 0 || next_x >= H || next_y < 0 || next_y >= W)
        continue;
      if (S[next_x][next_y] == '#')
        continue;
      if (dist[next_x][next_y] <= dist[x][y])
        continue;
      dist[next_x][next_y] = dist[x][y];
      que.push_front(make_pair(0, make_pair(next_x, next_y)));
    }

    // 移動Bで行けるところ
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int next_x = x + i;
        int next_y = y + j;
        if (next_x < 0 || next_x >= H || next_y < 0 || next_y >= W)
          continue;
        if (S[next_x][next_y] == '#')
          continue;
        if (dist[next_x][next_y] < dist[x][y] + 1)
          continue;
        dist[next_x][next_y] = dist[x][y] + 1;
        que.push_back(make_pair(1, make_pair(next_x, next_y)));
      }
    }
  }

  if (dist[GH][GW] == INF)
    cout << -1 << endl;
  else
    cout << dist[GH][GW] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define double long double
using namespace std;
const int MOD = 1000000007;
const int INF = 1e12;
using Graph = vector<vector<int>>;

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

signed main() {
  int H, W;
  cin >> H >> W;
  int SH, SW, GH, GW;
  cin >> SH >> SW >> GH >> GW;
  SH--;
  SW--;
  GH--;
  GW--;

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

  vector<vector<int>> dist(H, vector<int>(W, INF));
  dist[SH][SW] = 0;
  deque<pair<int, pair<int, int>>> que;
  que.push_front(make_pair(0, make_pair(SH, SW)));

  while (!que.empty()) {
    int cost = que.front().first;
    int x = que.front().second.first;
    int y = que.front().second.second;
    que.pop_front();

    // 移動Aで行けるところ
    for (int direction = 0; direction < 4; ++direction) {
      int next_x = x + dx[direction];
      int next_y = y + dy[direction];
      if (next_x < 0 || next_x >= H || next_y < 0 || next_y >= W)
        continue;
      if (S[next_x][next_y] == '#')
        continue;
      if (dist[next_x][next_y] < dist[x][y])
        continue;
      dist[next_x][next_y] = dist[x][y];
      que.push_front(make_pair(0, make_pair(next_x, next_y)));
    }

    // 移動Bで行けるところ
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int next_x = x + i;
        int next_y = y + j;
        if (next_x < 0 || next_x >= H || next_y < 0 || next_y >= W)
          continue;
        if (S[next_x][next_y] == '#')
          continue;
        if (dist[next_x][next_y] < dist[x][y] + 1)
          continue;
        dist[next_x][next_y] = dist[x][y] + 1;
        que.push_back(make_pair(1, make_pair(next_x, next_y)));
      }
    }
  }

  if (dist[GH][GW] == INF)
    cout << -1 << endl;
  else
    cout << dist[GH][GW] << endl;
}