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

#include <bits/stdc++.h>
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define ll long long
#define fr first
#define sc second
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()

int H, W;
int sy, sx, gy, gx;
string arr[1000];
int dist[1000][1000];

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

void Dijkstra() {
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      dist[i][j] = 1e9;

  priority_queue<tuple<int, int, int>, vector<tuple<int, int, int>>,
                 greater<tuple<int, int, int>>>
      pq;
  pq.push({0, sy, sx});
  dist[sy][sx] = 0;

  while (pq.size()) {
    int cury, curx, magicB;
    tie(magicB, cury, curx) = pq.top();
    pq.pop();

    if (magicB > dist[cury][curx])
      continue;

    // 0
    for (int i = 0; i < 4; i++) {
      int ny = cury + dy[i];
      int nx = curx + dx[i];
      if (ny < 0 || nx < 0 || ny >= H || nx >= W)
        continue;
      if (arr[ny][nx] == '#')
        continue;
      if (magicB < dist[ny][nx]) {
        dist[ny][nx] = magicB;
        pq.push({magicB, ny, nx});
      }
    }

    // 1
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        int ny = cury + i;
        int nx = curx + j;
        if (ny < 0 || nx < 0 || ny >= H || nx >= W)
          continue;
        if (arr[ny][nx] == '#')
          continue;
        if (magicB + 1 < dist[ny][nx]) {
          dist[ny][nx] = magicB + 1;
          pq.push({magicB + 1, ny, nx});
        }
      }
  }
}

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> H >> W;
  cin >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  for (int i = 0; i < H; i++)
    cin >> arr[i];

  Dijkstra();
  int ans = dist[gy][gx];

  if (ans == 1e9)
    cout << -1 << '\\n';
  else
    cout << ans << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <random>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define ll long long
#define fr first
#define sc second
#define pll pair<ll, ll>
#define all(v) v.begin(), v.end()

int visited[1000][1000];

int main(int argc, char **argv) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int H, W;
  cin >> H >> W;
  int sy, sx, gy, gx;
  cin >> sy >> sx >> gy >> gx;
  sy--, sx--, gy--, gx--;
  string arr[1000];
  for (int i = 0; i < H; i++)
    cin >> arr[i];

  queue<tuple<int, int, int>> q;
  int dy[] = {0, 0, -1, 1};
  int dx[] = {-1, 1, 0, 0};
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      visited[i][j] = 1e9;

  q.push({sy, sx, 0});
  visited[sy][sx] = 0;

  int ans = 1e9;
  while (q.size()) {
    int cury, curx, magicB;
    tie(cury, curx, magicB) = q.front();
    q.pop();

    if (cury == gy && curx == gx) {
      ans = min(ans, magicB);
      continue;
    }
    for (int i = 0; i < 4; i++) {
      int ny = cury + dy[i];
      int nx = curx + dx[i];
      if (ny < 0 || nx < 0 || ny >= H || nx >= W)
        continue;
      if (visited[ny][nx] <= magicB)
        continue;
      if (arr[ny][nx] == '#')
        continue;
      visited[ny][nx] = magicB;
      q.push({ny, nx, magicB});
    }
    for (int i = -2; i <= 2; i++)
      for (int j = -2; j <= 2; j++) {
        int ny = cury + i;
        int nx = curx + j;
        if (ny < 0 || nx < 0 || ny >= H || nx >= W)
          continue;
        if (visited[ny][nx] <= magicB + 1)
          continue;
        if (arr[ny][nx] == '#')
          continue;
        visited[ny][nx] = magicB + 1;
        q.push({ny, nx, magicB + 1});
      }
  }

  if (ans == 1e9)
    cout << -1 << '\\n';
  else
    cout << ans << '\\n';
  return 0;
}