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

#include <bits/stdc++.h>

#define rep(i, x, n) for (int i = (x); i < (n); i++)
#define reps(i, x, n) for (int i = (x); i <= (n); i++)
#define lol long long
#define SUM(n) (((n) + 1) * (n) / 2) // 1〜nまでの総和を求める式
#define mp make_pair
#define fi first
#define se second
#define pu push_back
#define SYOU(x) fixed << setprecision(x + 1) // 小数点桁数を指定する
#define abs(x, y) (max(x, y) - min(x, y))
#define all(v) v.begin(), v.end()
#define UPDigit(a, b) ((a + b - 1) / b) // 小数点切り上げ
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MOD = int(1e9) + 7;
using namespace std;
using pii = pair<lol, lol>;
typedef vector<int> vit;
// 八方向を見るのに使うと便利(楽)
const int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};

// 最大公約数
lol gcd(lol x, lol y) { return (y != 0 ? gcd(y, x % y) : x); }
// 最小公倍数
lol lcm(lol x, lol y) { return ((x * y) / gcd(max(x, y), min(x, y))); }

int h, w;
lol cost[1005][1005];
char c[1005][1005];
priority_queue<tuple<lol, int, int>, vector<tuple<lol, int, int>>,
               greater<tuple<lol, int, int>>>
    que; // <cost, y, x>

void BFS() {
  lol y, x, sum;
  while (!que.empty()) {
    tie(sum, y, x) = que.top();
    que.pop();

    if (cost[y][x] > sum) {
      cost[y][x] = sum;
      rep(i, 0, 4) {
        int ny = y + dy[i];
        int nx = x + dx[i];
        if (c[ny][nx] != '#' && cost[ny][nx] > sum &&
            (ny <= h && 1 <= ny && nx <= w && 1 <= nx)) {
          // cost[ny][nx] = sum;
          que.emplace(sum, ny, nx);
        }
      }

      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          int ny = y + i, nx = x + j;
          if ((ny <= h && 1 <= ny && nx <= w && 1 <= nx)) {
            if (c[ny][nx] != '#') {
              que.emplace(sum + 1, ny, nx);
            }
          }
        }
      }
    }
  }
  return;
}

signed main(void) {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cin >> h >> w;
  reps(i, 1, h) {
    reps(j, 1, w) { cost[i][j] = LINF; }
  }
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  // cost[sy][sx] = 0;
  reps(i, 1, h) {
    reps(j, 1, w) { cin >> c[i][j]; }
  }
  que.emplace(0, sy, sx);
  BFS();
  if (cost[gy][gx] == LINF) {
    cout << "-1\\n";
  } else {
    cout << cost[gy][gx] << '\\n';
  }
  /*reps(i, 1, h){
    reps(j, 1, w){
      cout << cost[i][j] << " ";
    }
    cout << '\\n';
    }*/
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n) for (int i = 1; i <= (n); i++)
#define lol long long
#define SUM(n) ((n) + 1) * (n) / 2 // 1〜nまでの総和を求める式
#define mp make_pair
#define fi first
#define se second
#define pu push_back
#define SYOU(x) fixed << setprecision(x + 1) // 小数点桁数を指定する
#define abs(x, y) max(x, y) - min(x, y)
#define all(v) v.begin(), v.end()
#define UPDigit(a, b) (a + b - 1) / b // 小数点切り上げ
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MOD = int(1e9) + 7;
using namespace std;
using pii = pair<int, int>;
typedef vector<int> vit;

// 最大公約数
lol gcd(lol x, lol y) {
  if (x < y)
    swap(x, y);
  lol r = x % y;
  while (r != 0) {
    x = y;
    y = r;
    r = x % y;
  }
  return y;
}

// 最小公倍数
lol lcm(lol x, lol y) {
  lol a = x * y;
  return (a / gcd(x, y));
}

// 八方向を見るのに使うと便利(楽)
const int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};

int h, w;
lol cost[1005][1005];
char c[1005][1005];
queue<pair<int, int>> que;

void BFS() {
  int y, x;
  while (!que.empty()) {
    tie(y, x) = que.front();
    que.pop();

    rep(i, 4) {
      int ny = y + dy[i];
      int nx = x + dx[i];
      if (c[ny][nx] != '#' && cost[ny][nx] > cost[y][x] &&
          (ny <= h && 1 <= ny && nx <= w && 1 <= nx)) {
        cost[ny][nx] = cost[y][x];
        que.emplace(ny, nx);
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        int ny = y + i, nx = x + j;
        if ((ny <= h && 1 <= ny && nx <= w && 1 <= nx)) {
          if (c[ny][nx] != '#' && cost[ny][nx] > cost[y][x] + 1) {
            cost[ny][nx] = cost[y][x] + 1;
            que.emplace(ny, nx);
          }
        }
      }
    }
  }
  return;
}

signed main(void) {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cin >> h >> w;
  reps(i, h) {
    reps(j, w) { cost[i][j] = LINF; }
  }
  int sx, sy, gx, gy;
  cin >> sy >> sx >> gy >> gx;
  cost[sy][sx] = 0;
  reps(i, h) {
    reps(j, w) { cin >> c[i][j]; }
  }
  que.emplace(sy, sx);
  BFS();
  if (cost[gy][gx] == LINF) {
    cout << "-1\\n";
  } else {
    cout << cost[gy][gx] << '\\n';
  }
  return 0;
}