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

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define vv(T) std::vector<std::vector<T>>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;

typedef struct info {
  ll y;
  ll x;
  ll depth; // 今回は魔法の回数
} t_info;

ll h, w, ch, cw, dh, dw;
vector<vector<ll>> table(1010, vector<ll>(1010, -1)); // 壁は-1で初期化
queue<t_info> Q1;                                     // 歩き
queue<t_info> Q2;                                     // 魔法
vector<ll> y_vec = {1, -1, 0, 0};
vector<ll> x_vec = {0, 0, 1, -1};

void bfs() {
  while (!Q1.empty()) {
    ll y, x, depth;
    t_info now = Q1.front();
    Q1.pop();
    y = now.y;
    x = now.x;
    depth = now.depth;
    // お隣さんチェック
    for (ll i = 0; i < 4; i++) {
      t_info next = {y + y_vec[i], x + x_vec[i], depth};
      if (table[next.y][next.x] != -1 && table[next.y][next.x] > next.depth) {
        table[next.y][next.x] = next.depth;
        Q1.push(next);
      }
    }
    // 魔法
    for (ll i = -2; i <= 2; i++) {
      for (ll j = -2; j <= 2; j++) {
        t_info next = {y + i, x + j, depth + 1};
        if (table[next.y][next.x] != -1 && table[next.y][next.x] > next.depth) {
          table[next.y][next.x] = next.depth;
          Q2.push(next);
        }
      }
    }
  }
  if (!Q2.empty()) // Q1をQ2で上書きしてもう一度
  {
    Q1 = Q2;
    queue<t_info> tmp;
    Q2 = tmp;
    bfs();
  }
}

int main(void) {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch += 1;
  cw += 1;
  dh += 1;
  dw += 1;

  for (ll i = 2; i <= h + 1; i++)
    for (ll j = 2; j <= w + 1; j++) {
      char a;
      cin >> a;
      if (a == '.')
        table[i][j] = LLONG_MAX; // 床はLLONG_MAXで初期化
    }
  table[ch][cw] = 0; // 床を魔法回数で塗り替えていく。スタート地点は0
  Q1.push({ch, cw, 0});
  bfs();
  if (table[dh][dw] == LLONG_MAX)
    table[dh][dw] = -1;
  cout << table[dh][dw] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define vv(T) std::vector<std::vector<T>>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;

int h, w, ch, cw, dh, dw;
ll ans = LLONG_MAX;
vector<vector<char>> table(1010, vector<char>(1010, '#'));

void dfs(vv(char) seen, ll warp, int y, int x) {
  if (seen[y][x] == '#')
    return;
  seen[y][x] = '#';
  if (y == dh && x == dw) {
    ans = min(ans, warp);
    return;
  }
  // for (int i = 0; i <= h+2; i++)
  // {
  // 	for (int j = 0; j <= w+2; j++)
  // 		cout << seen[i][j];
  // 	cout << endl;
  // }
  if (seen[y + 1][x] != '.' && seen[y - 1][x] != '.' && seen[y][x + 1] != '.' &&
      seen[y][x - 1] != '.') {
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        if (seen[y + i][x + j] != '#')
          dfs(seen, warp + 1, y + i, x + j);
      }
    }
    return;
  }
  if (seen[y + 1][x] != '#')
    dfs(seen, warp, y + 1, x);
  if (seen[y - 1][x] != '#')
    dfs(seen, warp, y - 1, x);
  if (seen[y][x + 1] != '#')
    dfs(seen, warp, y, x + 1);
  if (seen[y][x - 1] != '#')
    dfs(seen, warp, y, x - 1);
}

int main(void) {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch += 1;
  cw += 1;
  dh += 1;
  dw += 1;

  for (int i = 2; i <= h + 1; i++)
    for (int j = 2; j <= w + 1; j++)
      cin >> table[i][j];
  vector<vector<char>> seen(1010, vector<char>(1010, '!'));
  seen = table;
  dfs(seen, 0, ch, cw);
  if (ans == LLONG_MAX)
    cout << -1 << endl;
  else
    cout << ans << endl;
}