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

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

struct Data {

  int y, x, c;

  Data(int ny, int nx, int nc) : y(ny), x(nx), c(nc) {}
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  --ch;
  --cw;
  --dh;
  --dw;
  vector<string> s(h);
  rep(i, h) cin >> s[i];

  queue<Data> que, que2;
  vector<vector<int>> v(h, vector<int>(w, -1));

  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  que.emplace(ch, cw, 0);
  while (!que.empty()) {
    // bfs
    while (!que.empty()) {
      Data now = que.front();

      que.pop();
      int y = now.y, x = now.x, c = now.c;
      if ((v[y][x] != -1 && v[y][x] <= c) || s[y][x] == '#')
        continue;
      que2.emplace(y, x, c);
      v[y][x] = c;
      rep(i, 4) {
        int ny = y + dy[i], nx = x + dx[i];
        if (ny < 0 || ny >= h || nx < 0 || nx >= w || s[ny][nx] == '#' ||
            (v[ny][nx] != -1 && v[ny][nx] <= c) || s[ny][nx] == '#')
          continue;
        que.emplace(ny, nx, c);
      }
    }

    // 列挙
    while (!que2.empty()) {
      Data now = que2.front();
      int y = now.y, x = now.x, c = now.c;
      que2.pop();
      for (int i = -2; i <= 2; ++i) {
        for (int j = -2; j <= 2; ++j) {
          int ny = y + i, nx = x + j;
          if (ny < 0 || ny >= h || nx < 0 || nx >= w || s[ny][nx] == '#' ||
              (v[ny][nx] != -1 && v[ny][nx] <= c + 1) || s[ny][nx] == '#')
            continue;
          que.emplace(ny, nx, c + 1);
        }
      }
    }
  }
  cout << v[dh][dw] << endl;
}

Error: TLE

Fixed submission:

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

struct Data {

  int y, x, c;

  Data(int ny, int nx, int nc) : y(ny), x(nx), c(nc) {}
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w;
  cin >> h >> w;
  int ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  --ch;
  --cw;
  --dh;
  --dw;
  vector<string> s(h);
  rep(i, h) cin >> s[i];

  queue<Data> que;
  vector<vector<int>> v(h, vector<int>(w, -1));

  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  que.emplace(ch, cw, 0);
  while (!que.empty()) {
    Data now = que.front();
    que.pop();
    int y = now.y, x = now.x, c = now.c;
    if ((v[y][x] != -1 && v[y][x] <= c) || s[y][x] == '#')
      continue;
    v[y][x] = c;
    rep(i, 4) {
      int ny = y + dy[i], nx = x + dx[i];
      if (ny < 0 || ny >= h || nx < 0 || nx >= w || s[ny][nx] == '#' ||
          (v[ny][nx] != -1 && v[ny][nx] < c) || s[ny][nx] == '#')
        continue;
      que.emplace(ny, nx, c);
    }
    for (int i = -2; i <= 2; ++i)
      for (int j = -2; j <= 2; ++j) {
        if ((i == 0 & j >= -1 && j <= 1) || (j == 0 && i >= -1 && i <= 1))
          continue;
        int ny = y + i, nx = x + j;
        if (ny < 0 || ny >= h || nx < 0 || nx >= w ||
            (v[ny][nx] != -1 && v[ny][nx] < c + 1) || s[ny][nx] == '#')
          continue;
        que.emplace(ny, nx, c + 1);
      }
  }
  cout << v[dh][dw] << endl;
}