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

#include <bits/stdc++.h>

using namespace std;

using usize = ::std::size_t;
// using u64 = ::std::int_least64_t;
using u64 = int;
static constexpr u64 Inf = ::std::numeric_limits<u64>::max() / 2;

const int offset[24][2] = {
    {-1, 0},  {1, 0},   {0, 1},  {0, -1}, {1, 1},  {1, -1},  {-1, -1}, {-1, 1},
    {2, 2},   {2, 1},   {2, 0},  {2, -1}, {2, -2}, {-1, -2}, {0, -2},  {-1, -2},
    {-2, -2}, {-2, -1}, {-2, 0}, {-2, 1}, {-2, 2}, {-1, 2},  {0, 2},   {1, 2}};

struct edge {
  u64 r, c, weight;
  bool operator<(const edge &e) const { return weight > e.weight; }
};

int R, C;
const int MAXV = 1010;
u64 dist[MAXV][MAXV];
char grid[MAXV][MAXV];

void mooreDijkstra(int r, int c) {
  for (int i = 0; i <= R; i++) {
    for (int j = 0; j <= C; j++) {
      dist[i][j] = Inf;
    }
  }

  dist[r][c] = 0;
  priority_queue<edge> q;
  q.push(edge{r, c, dist[r][c]});
  while (!q.empty()) {
    edge e1 = q.top();
    q.pop();
    if (dist[e1.r][e1.c] < e1.weight)
      continue;
    for (int k = 0; k < 4; k++) {
      auto v = edge{e1.r + offset[k][0], e1.c + offset[k][1], 0};
      if (v.r < 0 || v.r >= R || v.c < 0 || v.c >= C) {
        continue;
      }
      if (grid[v.r][v.c] == '.') {
        if (dist[v.r][v.c] > dist[e1.r][e1.c] + v.weight) {
          dist[v.r][v.c] = dist[e1.r][e1.c] + v.weight;
          q.push(edge{v.r, v.c, dist[e1.r][e1.c] + v.weight});
        }
      }
    }

    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        auto v = edge{e1.r + i, e1.c + j, 1};
        if (v.r < 0 || v.r >= R || v.c < 0 || v.c >= C) {
          continue;
        }
        if (grid[v.r][v.c] == '.') {
          if (dist[v.r][v.c] > dist[e1.r][e1.c] + v.weight) {
            dist[v.r][v.c] = dist[e1.r][e1.c] + v.weight;
            q.push(edge{v.r, v.c, dist[e1.r][e1.c] + v.weight});
          }
        }
      }
    }
  }
}

int main(int argc, char *argv[]) {

  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> R >> C;
  int Ch, Cw, Dh, Dw;
  cin >> Ch >> Cw;
  cin >> Dh >> Dw;
  Ch--, Cw--, Dh--, Dw--;

  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      cin >> grid[i][j];
    }
  }

  mooreDijkstra(Ch, Cw);
  dist[Dh][Dw] = dist[Dh][Dw] * 2 >= Inf ? -1 : dist[Dh][Dw];
  cout << dist[Dh][Dw] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using usize = ::std::size_t;
// using u64 = ::std::int_least64_t;
using u64 = int;
static constexpr u64 Inf = ::std::numeric_limits<u64>::max() / 2;

const int offset[24][2] = {
    {-1, 0},  {1, 0},   {0, 1},  {0, -1}, {1, 1},  {1, -1},  {-1, -1}, {-1, 1},
    {2, 2},   {2, 1},   {2, 0},  {2, -1}, {2, -2}, {-1, -2}, {0, -2},  {-1, -2},
    {-2, -2}, {-2, -1}, {-2, 0}, {-2, 1}, {-2, 2}, {-1, 2},  {0, 2},   {1, 2}};

int R, C;
const int MAXV = 1010;
u64 dist[MAXV][MAXV];
char grid[MAXV][MAXV];

struct edge {
  u64 r, c, d;
};

void bfs(int r, int c) {
  // memset(visited, 0, sizeof(visited));
  memset(dist, -1, sizeof(dist));

  queue<edge> q;
  q.push(edge{r, c, 0});
  // visited[r][c] = 1;
  dist[r][c] = 0;

  while (!q.empty()) {
    auto u = q.front();
    q.pop();
    if (u.d > dist[u.r][u.c]) {
      continue;
    }
    for (int k = 0; k < 4; k++) {
      auto v = make_pair(u.r + offset[k][0], u.c + offset[k][1]);
      if (v.first < 0 || v.first >= R || v.second < 0 || v.second >= C) {
        continue;
      }
      if (grid[v.first][v.second] == '.') {
        if (dist[v.first][v.second] == -1 ||
            dist[v.first][v.second] > dist[u.r][u.c]) {
          q.push(edge{v.first, v.second, dist[u.r][u.c]});
          // visited[v.first][v.second] = 1;
          dist[v.first][v.second] = dist[u.r][u.c];
        }
      }
    }
    for (int i = -2; i <= 2; i++) {
      for (int j = -2; j <= 2; j++) {
        auto v = make_pair(u.r + i, u.c + j);
        if (v.first < 0 || v.first >= R || v.second < 0 || v.second >= C) {
          continue;
        }
        if (grid[v.first][v.second] == '.') {
          if (dist[v.first][v.second] == -1 ||
              dist[v.first][v.second] > dist[u.r][u.c] + 1) {
            q.push(edge{v.first, v.second, dist[u.r][u.c] + 1});
            // visited[v.first][v.second] = 1;
            dist[v.first][v.second] = dist[u.r][u.c] + 1;
          }
        }
      }
    }
  }
}

int main(int argc, char *argv[]) {

  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> R >> C;
  int Ch, Cw, Dh, Dw;
  cin >> Ch >> Cw;
  cin >> Dh >> Dw;
  Ch--, Cw--, Dh--, Dw--;

  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      cin >> grid[i][j];
    }
  }

  bfs(Ch, Cw);

  cout << dist[Dh][Dw] << endl;

  return 0;
}