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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(c) begin(c), end(c)
/*

1000*1000*25
*/
struct Z {
  ll y;
  ll x;
  ll depth;
};

ll H, W, y, x, depth, oo = 1e12, xs, ys, xg, yg, ans = oo;
vector<ll> y_vec = {1, 0, -1, 0};
vector<ll> x_vec = {0, 1, 0, -1};
vector<vector<ll>> A;
queue<Z> Q, Q2;

void bfs() {
  while (!Q.empty()) {
    Z now = Q.front();
    Q.pop();
    y = now.y, x = now.x, depth = now.depth;
    if (depth >= ans)
      continue;
    if (y == yg && x == xg) {
      ans = min(ans, A[yg][xg]);
      continue;
    }
    for (ll i = 0; i < 4; i++) {
      Z next = {y + y_vec[i], x + x_vec[i], depth};
      if (A[next.y][next.x] == -1)
        continue;
      if (A[next.y][next.x] <= next.depth)
        continue;
      A[next.y][next.x] = next.depth;
      Q.push(next);
    }
    for (ll i = -2; i <= 2; i++) {
      for (ll j = -2; j <= 2; j++) {
        Z next = {y + i, x + j, depth + 1};
        // cout<<"ny:"<<next.y<<" nx:"<<next.x<<endl;
        if (A[next.y][next.x] == -1)
          continue;
        if (A[next.y][next.x] <= next.depth)
          continue;
        A[next.y][next.x] = next.depth;
        Q2.push(next);
        // cout<<" d:"<<next.depth<<endl;
      }
    }
  }
  if (!Q2.empty()) {
    Q = Q2;
    queue<Z> tmp;
    Q2 = tmp;
    bfs();
  }
}

// A[y][x], A(x,y)として管理
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  cin >> H >> W;
  cin >> ys >> xs >> yg >> xg;
  ys++;
  xs++;
  yg++;
  xg++;
  A.assign(H + 4, vector<ll>(W + 4, -1));
  for (int i = 2; i < H + 2; i++) {
    for (int j = 2; j < W + 2; j++) {
      char a;
      cin >> a;
      if (a == '#')
        continue;
      A[i][j] = oo;
    }
  }
  A[ys][xs] = 0;
  Z start = {ys, xs, 0};
  Q.emplace(start);
  bfs();
  if (ans == oo)
    ans = -1;
  cout << ans << endl;

  /*for(int i=2; i<H+2; i++){
    for(int j=2; j<W+2; j++)
      cout<<A[i][j];
    cout<<endl;
    }*/
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all(c) begin(c), end(c)
/*

1000*1000*25
*/
struct Z {
  ll y;
  ll x;
  ll depth;
};

ll H, W, y, x, depth, oo = 1e12, xs, ys, xg, yg, ans = oo;
vector<ll> y_vec = {1, 0, -1, 0};
vector<ll> x_vec = {0, 1, 0, -1};
vector<vector<ll>> A;
queue<Z> Q;

void bfs() {
  while (!Q.empty()) {
    Z now = Q.front();
    Q.pop();
    y = now.y, x = now.x, depth = now.depth;
    if (depth >= ans)
      continue;
    if (y == yg && x == xg) {
      ans = min(ans, A[yg][xg]);
      continue;
    }
    for (ll i = 0; i < 4; i++) {
      Z next = {y + y_vec[i], x + x_vec[i], depth};
      if (A[next.y][next.x] == -1)
        continue;
      if (A[next.y][next.x] <= next.depth)
        continue;
      A[next.y][next.x] = next.depth;
      Q.push(next);
    }
    for (ll i = -2; i <= 2; i++) {
      for (ll j = -2; j <= 2; j++) {
        Z next = {y + i, x + j, depth + 1};
        // cout<<"ny:"<<next.y<<" nx:"<<next.x<<endl;
        if (A[next.y][next.x] == -1)
          continue;
        if (A[next.y][next.x] <= next.depth)
          continue;
        A[next.y][next.x] = next.depth;
        Q.push(next);
        // cout<<" d:"<<next.depth<<endl;
      }
    }
  }
}

// A[y][x], A(x,y)として管理
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  cin >> H >> W;
  cin >> ys >> xs >> yg >> xg;
  ys++;
  xs++;
  yg++;
  xg++;
  A.assign(H + 4, vector<ll>(W + 4, -1));
  for (int i = 2; i < H + 2; i++) {
    for (int j = 2; j < W + 2; j++) {
      char a;
      cin >> a;
      if (a == '#')
        continue;
      A[i][j] = oo;
    }
  }
  A[ys][xs] = 0;
  Z start = {ys, xs, 0};
  Q.emplace(start);
  bfs();
  if (ans == oo)
    ans = -1;
  cout << ans << endl;

  /*for(int i=2; i<H+2; i++){
    for(int j=2; j<W+2; j++)
      cout<<A[i][j];
    cout<<endl;
    }*/
}