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

#include "bits/stdc++.h"
#define ll long long int
#define ld long double
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr)
#define F first
#define S second
#define INF 100000000

using namespace std;
typedef pair<int, int> pii;

int r, c;
vector<vector<char>> g;
bool isSafe(int row, int col) {
  return row < r && row >= 0 && col < c && col >= 0 && g[row][col] == '.';
}

int main() {
  fastIO;
  int startX, startY, endX, endY;
  cin >> r >> c >> startX >> startY >> endX >> endY;
  startX--, startY--, endX--, endY--;

  g = vector<vector<char>>(r, vector<char>(c));
  int dist[r][c];

  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      dist[i][j] = INF;
      cin >> g[i][j];
    }
  }

  deque<pii> q;
  q.push_back({endX, endY});

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

  dist[endX][endY] = 0;

  while (!q.empty()) {
    pii p = q.front();
    q.pop_front();
    int x = p.F, y = p.S;

    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (isSafe(nx, ny) && dist[x][y] < dist[nx][ny]) {
        dist[nx][ny] = dist[x][y];
        q.push_front({nx, ny});
      }
    }

    for (int i = x - 2; i <= x + 2; i++) {
      for (int j = y - 2; j <= y + 2; j++) {
        if (isSafe(i, j) && dist[x][y] + 1 < dist[i][j]) {
          dist[i][j] = dist[x][y] + 1;
          q.push_back({i, j});
        }
      }
    }
  }

  if (dist[startX][startY] >= 1000000)
    cout << -1;
  else
    cout << dist[startX][startY];
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#define ll long long int
#define ld long double
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(nullptr)
#define F first
#define S second
#define INF 1000000

using namespace std;
typedef pair<int, int> pii;

int r, c;
vector<vector<int>> dp;
vector<vector<bool>> g;
vector<vector<bool>> vis;

bool isSafe(int row, int col) {
  return row < r && row >= 0 && col < c && col >= 0 && g[row][col];
}

// int memo(int row, int col) {
//     if (dp[row][col] != -1) return dp[row][col];
//     if (vis[row][col]) return INF;
//     vis[row][col] = true;
//
//     int best = INF;
//
//     if (isSafe(row + 1, col)) best = min(best, memo(row + 1, col));
//     if (isSafe(row, col + 1)) best = min(best, memo(row, col + 1));
//     if (isSafe(row - 1, col)) best = min(best, memo(row - 1, col));
//     if (isSafe(row, col - 1)) best = min(best, memo(row, col - 1));
//
//
//     for (int i = row - 2; i <= row + 2; i++) {
//         for (int j = col - 2; j <= col + 2; j++) {
//             if (isSafe(i, j)) best = min(best, 1 + memo(i, j));
//         }
//     }
//
//     return dp[row][col] = best;
// }

int main() {
  fastIO;
  int startX, startY, endX, endY;
  cin >> r >> c >> startX >> startY >> endX >> endY;
  startX--, startY--, endX--, endY--;

  g = vis = vector<vector<bool>>(r, vector<bool>(c, false));
  dp = vector<vector<int>>(r, vector<int>(c, 100000));

  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      char temp;
      cin >> temp;
      if (temp == '.')
        g[i][j] = true;
    }
  }

  priority_queue<pair<pii, int>> q;
  q.push({{endX, endY}, 0});

  while (!q.empty()) {
    pair<pii, int> p = q.top();
    q.pop();
    int x = p.F.F, y = p.F.S, score = p.S;

    if (score >= dp[x][y])
      continue;
    dp[x][y] = score;

    if (isSafe(x + 1, y))
      q.push({{x + 1, y}, score});
    if (isSafe(x - 1, y))
      q.push({{x - 1, y}, score});
    if (isSafe(x, y + 1))
      q.push({{x, y + 1}, score});
    if (isSafe(x, y - 1))
      q.push({{x, y - 1}, score});

    for (int i = x - 2; i <= x + 2; i++) {
      for (int j = y - 2; j <= y + 2; j++) {
        if (isSafe(i, j))
          q.push({{i, j}, score + 1});
      }
    }
  }

  if (dp[startX][startY] >= 10000)
    cout << -1;
  else
    cout << dp[startX][startY];

  //    int res = memo(startX, startY);
  //    if (res >= 10000) cout << -1;
  //    else cout << res;
}