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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, f, n) for (ll i = (f); i < (n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define repv(v) for (auto it = (v).begin(); it != (v).end(); it++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define INIT                                                                   \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false);

template <class T> inline bool chmax(T &a, T b) { return a = (a < b) ? b : a; }
template <class T> inline bool chmin(T &a, T b) { return a = (a > b) ? b : a; }

ll const INF = 1LL << 60;
ll const MOD = 1000000007;

ll const MAX_N = 1005;
ll const MAX_M = 1005;

typedef pair<ll, ll> P;

ll H, W;
ll ch, cw, dh, dw;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll d[MAX_N][MAX_M];
char maze[MAX_N][MAX_M + 1];

ll bfs(ll sx, ll sy) {
  queue<P> que;
  queue<P> visited;

  rep(i, H) rep(j, W) d[i][j] = INF;

  que.push(P(sx, sy));
  d[sy][sx] = 0;
  ll cnt = 0;

  while (que.size() || visited.size()) {
    if (!que.empty()) {
      P p = que.front();
      que.pop();
      visited.push(p);

      if (p.first == dw && p.second == dh)
        return d[dh][dw];

      for (int i = 0; i < 4; i++) {
        int nx = p.first + dx[i];
        int ny = p.second + dy[i];

        if (0 <= nx && nx < W && 0 <= ny && ny < H && maze[ny][nx] != '#' &&
            d[ny][nx] == INF) {
          que.push(P(nx, ny));
          d[ny][nx] = d[p.second][p.first];
        }
      }
    } else {
      P p = visited.front();
      visited.pop();

      for (ll i = -2; i <= 2; i++) {
        for (ll j = -2; j <= 2; j++) {
          if (i == 0 && j == 0)
            continue;

          int nx = p.first + i;
          int ny = p.second + j;

          if (0 > nx || nx >= W || 0 > ny || ny >= H)
            continue;
          if (maze[ny][nx] == '#')
            continue;
          if (d[ny][nx] != INF)
            continue;

          que.push(P(nx, ny));
          d[ny][nx] = d[p.second][p.first] + 1;
        }
      }
    }
    // cout << "---" << endl;
    // rep(y, H) {
    //     rep(x, W) {
    //         if (d[y][x] >= INF)
    //             cout << "# ";
    //         else
    //             cout << d[y][x] << " ";
    //     }
    //     cout << endl;
    // }
  }

  return -1;
}

int main() {
  INIT;

  cin >> H >> W;
  cin >> ch >> cw >> dh >> dw;

  rep(i, H) {
    rep(j, W) { cin >> maze[i][j]; }
  }

  ch--;
  cw--;
  dh--;
  dw--;

  cout << bfs(cw, ch) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define reps(i, f, n) for (ll i = (f); i < (n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define repv(v) for (auto it = (v).begin(); it != (v).end(); it++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define INIT                                                                   \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false);

template <class T> inline bool chmax(T &a, T b) { return a = (a < b) ? b : a; }
template <class T> inline bool chmin(T &a, T b) { return a = (a > b) ? b : a; }

ll const INF = 1LL << 60;
ll const MOD = 1000000007;

ll const MAX_N = 1005;
ll const MAX_M = 1005;

typedef pair<ll, ll> P;

ll H, W;
ll ch, cw, dh, dw;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

ll d[MAX_N][MAX_M];
char maze[MAX_N][MAX_M + 1];

ll bfs(ll sx, ll sy) {
  queue<P> que;

  rep(i, H) rep(j, W) d[i][j] = INF;

  que.push(P(sx, sy));
  d[sy][sx] = 0;

  for (ll cnt = 0; cnt < 100; cnt++) {
    while (que.size()) {
      P p = que.front();
      que.pop();

      if (p.first == dw && p.second == dh)
        return d[dh][dw];

      for (int i = 0; i < 4; i++) {
        int nx = p.first + dx[i];
        int ny = p.second + dy[i];

        if (0 <= nx && nx < W && 0 <= ny && ny < H && maze[ny][nx] != '#' &&
            d[ny][nx] == INF) {
          que.push(P(nx, ny));
          d[ny][nx] = cnt;
        }
      }
    }

    rep(y, H) {
      rep(x, W) {
        if (d[y][x] == cnt) {
          for (ll i = -2; i <= 2; i++) {
            for (ll j = -2; j <= 2; j++) {
              if (i == 0 && j == 0)
                continue;

              int nx = x + i;
              int ny = y + j;

              if (0 > nx || nx >= W || 0 > ny || ny >= H)
                continue;

              if (maze[ny][nx] == '#')
                continue;
              if (d[ny][nx] != INF)
                continue;

              que.push(P(nx, ny));
              d[ny][nx] = cnt + 1;
            }
          }
        }
      }
    }

    if (que.size() == 0)
      return -1;
  }
}

int main() {
  INIT;

  cin >> H >> W;
  cin >> ch >> cw >> dh >> dw;

  rep(i, H) {
    rep(j, W) { cin >> maze[i][j]; }
  }

  ch--;
  cw--;
  dh--;
  dw--;

  // rep(y, H) {
  //     rep(x, W) {
  //         if (d[y][x] >= INF) {
  //             cout << "# ";
  //         } else {
  //             cout << d[y][x] << " ";
  //         }
  //     }
  //     cout << endl;
  // }
  // cout << "---" << endl;

  cout << bfs(cw, ch) << endl;

  return 0;
}