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

// 有點不耐煩會破壞偉大的計劃

#include <bits/stdc++.h>

#define append push_back

using namespace std;
using ll = long long;

const int N = 1e5 + 3;
const int M = 1e6 + 7;
const long long MOD = 1e9 + 7;
const long long INF = 1e18 + 9;
const int SM = 1e3 + 5;
const int logN = 22, BL = 400;
const double EPS = 1e-9, PI = acos(-1);
const int dx[] = {1, 0, 0, -1, -1, 1, -1, 1};
const int dy[] = {0, 1, -1, 0, -1, 1, 1, -1};

void debug() { cerr << "\\n"; }
template <typename Type1, typename... Type2> void debug(Type1 a, Type2... b) {
  cerr << a << " ";
  debug(b...);
}

long long h, w, d[SM][SM], x[2], y[2];
char board[SM][SM];

bool legal(long long x, long long y) {
  return board[x][y] == '.' && x >= 1 && h >= x && y >= 1 && w >= 1;
}

int main() {
  //    freopen(".in", "r", stdin);
  //    freopen(".out", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);

  cin >> h >> w >> x[0] >> y[0] >> x[1] >> y[1];

  for (long long i = 1; i <= h; i++) {
    for (long long j = 1; j <= w; j++) {
      cin >> board[i][j];
      d[i][j] = INF;
    }
  }

  deque<pair<long long, long long>> q;
  q.push_front({x[0], y[0]});
  d[x[0]][y[0]] = 0;

  while (!q.empty()) {
    long long vx = q.front().first, vy = q.front().second;
    q.pop_front();

    for (long long i = 0; i < 4; i++) {
      long long nx = vx + dx[i], ny = vy + dy[i];
      if (legal(nx, ny) && d[nx][ny] > d[vx][vy]) {
        d[nx][ny] = d[vx][vy];
        q.push_front({nx, ny});
      }
    }

    for (long long i = vx - 2; i <= vx + 2; i++) {
      for (long long j = vy - 2; j <= vy + 2; j++) {
        if (legal(i, j) && d[i][j] > d[vx][vy] + 1) {
          d[i][j] = d[vx][vy] + 1;
          q.push_back({i, j});
        }
      }
    }
  }

  if (d[x[1]][y[1]] >= INF)
    cout << "-1";
  else
    cout << d[x[1]][y[1]];

  return 0;
}


Error: TLE

Fixed submission:

// 有點不耐煩會破壞偉大的計劃

#include <bits/stdc++.h>

#define append push_back

using namespace std;
using ll = long long;

const int N = 1e5 + 3;
const int M = 1e6 + 7;
const long long MOD = 1e9 + 7;
const long long INF = 1e18 + 9;
const int SM = 1e3 + 5;
const int logN = 22, BL = 400;
const double EPS = 1e-9, PI = acos(-1);
const int dx[] = {1, 0, 0, -1, -1, 1, -1, 1};
const int dy[] = {0, 1, -1, 0, -1, 1, 1, -1};

void debug() { cerr << "\\n"; }
template <typename Type1, typename... Type2> void debug(Type1 a, Type2... b) {
  cerr << a << " ";
  debug(b...);
}

long long h, w, d[SM][SM], x[2], y[2];
char board[SM][SM];

bool legal(long long x, long long y) {
  return board[x][y] == '.' && x >= 1 && h >= x && y >= 1 && w >= 1;
}

int main() {
  //    freopen(".in", "r", stdin);
  //    freopen(".out", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);

  cin >> h >> w >> x[0] >> y[0] >> x[1] >> y[1];

  for (long long i = 1; i <= h; i++) {
    for (long long j = 1; j <= w; j++) {
      cin >> board[i][j];
      d[i][j] = INF;
    }
  }

  queue<pair<long long, long long>> q;
  q.push({x[0], y[0]});
  d[x[0]][y[0]] = 0;

  while (!q.empty()) {
    long long vx = q.front().first, vy = q.front().second;
    q.pop();

    if (board[vx][vy] == '#')
      continue;

    for (long long i = 0; i < 4; i++) {

      long long nx = vx + dx[i], ny = vy + dy[i];
      if (legal(nx, ny) && d[nx][ny] > d[vx][vy]) {
        d[nx][ny] = d[vx][vy];
        q.push({nx, ny});
      }
    }

    for (long long i = vx - 2; i <= vx + 2; i++) {
      for (long long j = vy - 2; j <= vy + 2; j++) {
        if (legal(i, j) && d[i][j] > d[vx][vy] + 1) {
          d[i][j] = d[vx][vy] + 1;
          q.push({i, j});
        }
      }
    }
  }

  if (d[x[1]][y[1]] >= INF)
    cout << "-1";
  else
    cout << d[x[1]][y[1]];

  return 0;
}