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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const int INF = 1001001001;
int ddy[] = {-1, 0, 1, 0};
int ddx[] = {0, -1, 0, 1};

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  queue<P> q, q2;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  q.emplace(P(ch, cw));
  dist[ch][cw] = 0;
  // BFS
  while (!q.empty()) {
    while (!q.empty()) {
      int y = q.front().first;
      int x = q.front().second;
      q.pop();
      // 4方向
      rep(dir, 4) {
        int vy = y + ddy[dir];
        int vx = x + ddx[dir];
        if (vy < 0 || vy >= h || vx < 0 || vx >= w)
          continue;
        if (dist[vy][vx] <= dist[y][x])
          continue;
        if (s[vy][vx] == '#') {
          q2.emplace(P(y, x));
          continue;
        }
        dist[vy][vx] = dist[y][x];
        q.emplace(P(vy, vx));
      }
    }
    while (!q2.empty()) {
      // 5*5マス
      int y = q2.front().first;
      int x = q2.front().second;
      q2.pop();
      for (int dy = -2; dy <= 2; ++dy) {
        for (int dx = -2; dx <= 2; ++dx) {
          int vy = y + dy;
          int vx = x + dx;
          if (dy == 0 && dx == 0)
            continue;
          if (vy < 0 || vy >= h || vx < 0 || vx >= w)
            continue;
          if (dist[vy][vx] <= dist[y][x] + 1)
            continue;
          if (s[vy][vx] == '#')
            continue;
          dist[vy][vx] = dist[y][x] + 1;
          q.emplace(P(vy, vx));
        }
      }
    }
  }
  if (dist[dh][dw] != INF)
    cout << dist[dh][dw] << endl;
  else
    cout << -1 << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

const int INF = 1001001001;
int ddy[] = {-1, 0, 1, 0};
int ddx[] = {0, -1, 0, 1};

int main() {
  int h, w, ch, cw, dh, dw;
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  vector<string> s(h);
  rep(i, h) cin >> s[i];
  queue<P> q;
  vector<vector<int>> dist(h, vector<int>(w, INF));
  q.push(P(ch, cw));
  dist[ch][cw] = 0;
  // BFS
  while (!q.empty()) {
    int y = q.front().first;
    int x = q.front().second;
    q.pop();
    // 4方向
    rep(dir, 4) {
      int vy = y + ddy[dir];
      int vx = x + ddx[dir];
      if (vy < 0 || vy >= h || vx < 0 || vx >= w)
        continue;
      if (dist[vy][vx] <= dist[y][x])
        continue;
      if (s[vy][vx] == '#')
        continue;
      dist[vy][vx] = dist[y][x];
      q.push(P(vy, vx));
    }
    // 5*5マス
    for (int dy = -2; dy <= 2; ++dy) {
      for (int dx = -2; dx <= 2; ++dx) {
        int vy = y + dy;
        int vx = x + dx;
        if (dy == 0 && dx == 0)
          continue;
        if (vy < 0 || vy >= h || vx < 0 || vx >= w)
          continue;
        if (dist[vy][vx] <= dist[y][x] + 1)
          continue;
        if (s[vy][vx] == '#')
          continue;
        dist[vy][vx] = dist[y][x] + 1;
        q.push(P(vy, vx));
      }
    }
  }
  if (dist[dh][dw] != INF)
    cout << dist[dh][dw] << endl;
  else
    cout << -1 << endl;
}