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

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using P = pair<int, int>;
using PP = tuple<lint, int, int>;
using vec = vector<lint>;
using mat = vector<vector<lint>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define endl "\\n"

constexpr int MOD = 1000000007;
const lint INF = 1LL << 60;

const lint COST = 100000000;

int ddx[] = {1, 0, -1, 0};
int ddy[] = {0, 1, 0, -1};

int main() {
  int h, w;
  cin >> h >> w;
  int sy, sx;
  int gy, gx;
  cin >> sy >> sx >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;
  vector<string> grid(h);
  rep(i, h) cin >> grid[i];

  mat dist(h, vec(w, INF));
  dist[sy][sx] = 0;

  priority_queue<PP, vector<PP>, greater<PP>> que;
  que.push({0, sy, sx});

  while (!que.empty()) {
    auto pos = que.top();
    int t_dist = get<0>(pos);
    int y = get<1>(pos);
    int x = get<2>(pos);
    que.pop();

    if (dist[y][x] < t_dist)
      continue;

    rep(i, 4) {
      int ty = y + ddy[i];
      int tx = x + ddx[i];

      if (ty >= 0 && ty < h && tx >= 0 && tx < w) {
        if (grid[ty][tx] == '.' && dist[ty][tx] > dist[y][x] + 1) {
          dist[ty][tx] = dist[y][x] + 1;
          que.push({dist[ty][tx], ty, tx});
        }
      }
    }

    for (int dx = -2; dx <= 2; dx++) {
      for (int dy = -2; dy <= 2; dy++) {
        int ty = y + dy;
        int tx = x + dx;

        if (ty >= 0 && ty < h && tx >= 0 && tx < w) {
          if (grid[ty][tx] == '.' && dist[ty][tx] > dist[y][x] + COST) {
            dist[ty][tx] = dist[y][x] + COST;
            que.push({dist[ty][tx], ty, tx});
          }
        }
      }
    }
  }

  if (dist[gy][gx] == INF)
    cout << "-1" << endl;
  else
    cout << dist[gy][gx] / COST << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using P = pair<int, int>;
using vec = vector<lint>;
using mat = vector<vector<lint>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define endl "\\n"

constexpr int MOD = 1000000007;
const lint INF = 1LL << 60;

const lint COST = 100000000;

int ddx[] = {1, 0, -1, 0};
int ddy[] = {0, 1, 0, -1};

int main() {
  int h, w;
  cin >> h >> w;
  int sy, sx;
  int gy, gx;
  cin >> sy >> sx >> gy >> gx;
  sy--;
  sx--;
  gy--;
  gx--;
  vector<string> grid(h);
  rep(i, h) cin >> grid[i];

  mat dist(h, vec(w, INF));
  dist[sy][sx] = 0;

  queue<P> que;
  que.push({sy, sx});

  while (!que.empty()) {
    auto pos = que.front();
    que.pop();

    rep(i, 4) {
      int ty = pos.first + ddy[i];
      int tx = pos.second + ddx[i];

      if (ty >= 0 && ty < h && tx >= 0 && tx < w) {
        if (grid[ty][tx] == '.' &&
            dist[ty][tx] > dist[pos.first][pos.second] + 1) {
          dist[ty][tx] = dist[pos.first][pos.second] + 1;
          que.push({ty, tx});
        }
      }
    }

    for (int dx = -2; dx <= 2; dx++) {
      for (int dy = -2; dy <= 2; dy++) {
        int ty = pos.first + dy;
        int tx = pos.second + dx;

        if (ty >= 0 && ty < h && tx >= 0 && tx < w) {
          if (grid[ty][tx] == '.' &&
              dist[ty][tx] > dist[pos.first][pos.second] + COST) {
            dist[ty][tx] = dist[pos.first][pos.second] + COST;
            que.push({ty, tx});
          }
        }
      }
    }
  }

  if (dist[gy][gx] == INF)
    cout << "-1" << endl;
  else
    cout << dist[gy][gx] / COST << endl;
  return 0;
}