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

// Wizard in Maze

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define rep(i, x, y) for (ll i = x; i < y; i++)
#define nvarep(n, a)                                                           \\
  ll n;                                                                        \\
  cin >> n;                                                                    \\
  vector<ll> a(n);                                                             \\
  rep(i, 0, n) cin >> a[i]
#define vecrep(n, a, type)                                                     \\
  vector<type> a(n);                                                           \\
  rep(i, 0, n) cin >> a[i]
#define YN(flag) (flag ? "Yes" : "No")
#define lcm(a, b) (a / __gcd(a, b) * b)
#define range(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define nnn "\\n"
#define spa " "
using P = pair<ll, ll>;
using graph = vector<vector<ll>>;
const int inf = 2147483647;         // 2*10^9
const ll INF = 9223372036854775807; // 9*10^18

int h, w, ch, cw, dh, dw;

bool onboard(int a, int b) {
  if (a < 0 || a >= h || b < 0 || b >= w)
    return false;
  else
    return true;
}

signed main() {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  ch--, cw--, dh--, dw--;
  vecrep(h, a, string);
  int dx[4] = {1, 0, -1, 0};
  int dy[4] = {0, 1, 0, -1};
  int ddx[5] = {-2, -1, 0, 1, 2};
  int ddy[5] = {-2, -1, 0, 1, 2};

  deque<P> q;
  vector<vector<int>> dist(h, vector<int>(w, inf));
  vector<vector<int>> seen(h, vector<int>(w, 0));

  q.push_front(mp(ch, cw));
  dist[ch][cw] = 0;
  seen[ch][cw] = 2;

  while (!q.empty()) {
    P t = q.front();
    q.pop_front();
    rep(i, 0, 4) {
      if (!onboard(t.first + dy[i], t.second + dx[i]))
        continue;
      if (a[t.first + dy[i]][t.second + dx[i]] == '#' ||
          seen[t.first + dy[i]][t.second + dx[i]] == 2)
        continue;
      seen[t.first + dy[i]][t.second + dx[i]] = 2;
      dist[t.first + dy[i]][t.second + dx[i]] =
          min(dist[t.first][t.second], dist[t.first + dy[i]][t.second + dx[i]]);
      q.push_front(mp(t.first + dy[i], t.second + dx[i]));
    }
    rep(i, 0, 5) rep(j, 0, 5) {
      if (!onboard(t.first + ddy[i], t.second + ddx[j]))
        continue;
      if (a[t.first + ddy[i]][t.second + ddx[j]] == '#' ||
          seen[t.first + ddy[i]][t.second + ddx[j]] == 1 ||
          seen[t.first + ddy[i]][t.second + ddx[j]] == 2)
        continue;
      seen[t.first + ddy[i]][t.second + ddx[j]] = 1;
      dist[t.first + ddy[i]][t.second + ddx[j]] = dist[t.first][t.second] + 1;
      q.push_back(mp(t.first + ddy[i], t.second + ddx[j]));
    }
  }
  if (dist[dh][dw] == inf)
    cout << -1 << nnn;
  else
    cout << dist[dh][dw] << nnn;
  return 0;
}


Error: TLE

Fixed submission:

// Wizard in Maze

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define rep(i, x, y) for (ll i = x; i < y; i++)
#define nvarep(n, a)                                                           \\
  ll n;                                                                        \\
  cin >> n;                                                                    \\
  vector<ll> a(n);                                                             \\
  rep(i, 0, n) cin >> a[i]
#define vecrep(n, a, type)                                                     \\
  vector<type> a(n);                                                           \\
  rep(i, 0, n) cin >> a[i]
#define YN(flag) (flag ? "Yes" : "No")
#define lcm(a, b) (a / __gcd(a, b) * b)
#define range(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define nnn "\\n"
#define spa " "
using p = pair<ll, ll>;
using graph = vector<vector<ll>>;
const int inf = 2147483647;         // 2*10^9
const ll INF = 9223372036854775807; // 9*10^18

int h, w;
int ch, cw, dh, dw;
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
int ddx[5] = {-2, -1, 0, 1, 2};
int ddy[5] = {-2, -1, 0, 1, 2};
vector<string> s;
vector<vector<int>> wape;

bool onboard(int a, int b) {
  if (a < 0 || a >= h || b < 0 || b >= w)
    return false;
  else
    return true;
}

void dfs(int r, int gh, int gw) {
  if (!onboard(gh, gw))
    return;
  if (s[gh][gw] == '#')
    return;
  if (wape[gh][gw] <= r)
    return;
  wape[gh][gw] = r;
  rep(i, 0, 4) { dfs(r, gh + dy[i], gw + dx[i]); }
  rep(i, 0, 5) rep(j, 0, 5) { dfs(r + 1, gh + ddy[i], gw + ddx[j]); }
}

signed main() {
  cin >> h >> w;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  rep(i, 0, h) {
    string t;
    cin >> t;
    s.pb(t);
  }
  wape.resize(h);
  rep(i, 0, h) wape[i].resize(w, inf);
  dfs(0, ch, cw);
  if (wape[dh][dw] == inf)
    cout << -1 << nnn;
  else
    cout << wape[dh][dw] << nnn;
  return 0;
}